Skip to content

Commit

Permalink
remove newline when iterating post photos (fixes #3178)
Browse files Browse the repository at this point in the history
  • Loading branch information
dfrankow committed Oct 25, 2023
1 parent fab6e1a commit 8aad252
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 19 deletions.
54 changes: 53 additions & 1 deletion pelican/tests/test_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import re
from posixpath import join as posix_join
from unittest.mock import patch

from pelican.settings import DEFAULT_CONFIG
from pelican.tests.support import (mute, skipIfNoExecutable, temporary_folder,
Expand All @@ -10,7 +11,8 @@
build_markdown_header,
decode_wp_content,
download_attachments, fields2pelican,
get_attachments, wp2fields)
get_attachments, wp2fields,
tumblr2fields)
from pelican.utils import path_to_file_url, slugify

CUR_DIR = os.path.abspath(os.path.dirname(__file__))
Expand Down Expand Up @@ -477,3 +479,53 @@ def test_download_attachments(self):
self.assertTrue(
directory.endswith(posix_join('content', 'article.rst')),
directory)


class TestTumblrImporter(unittest.TestCase):
def setUp(self):
self.old_locale = locale.setlocale(locale.LC_ALL)
locale.setlocale(locale.LC_ALL, 'C')

def tearDown(self):
locale.setlocale(locale.LC_ALL, self.old_locale)

@patch("pelican.tools.pelican_import._get_tumblr_posts")
def test_posts(self, get):
def get_posts(api_key, blogname, offset=0):
if offset > 0:
return []

return [
{
"type": "photo",
"blog_name": "testy",
"date": "2019-11-07 21:26:40 GMT",
"timestamp": 1573162000,
"format": "html",
"slug": "a-slug",
"tags": [
"economics"
],
"state": "published",

"photos": [
{
"caption": "",
"original_size": {
"url": "https://..fccdc2360ba7182a.jpg",
"width": 634,
"height": 789
},
}]
}
]
get.side_effect = get_posts

posts = list(tumblr2fields("api_key", "blogname"))
self.assertEqual(
[('Photo',
'<img alt="" src="https://..fccdc2360ba7182a.jpg" />\n',
'2019-11-07-a-slug', '2019-11-07 15:26:40', 'testy', ['photo'],
['economics'], 'published', 'article', 'html')],
posts,
posts)
3 changes: 3 additions & 0 deletions pelican/tools/RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: patch

Remove addition of newline when iterating posts of type 'photo' (fixes #3178)
34 changes: 16 additions & 18 deletions pelican/tools/pelican_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,22 +390,22 @@ def dc2fields(file):
post_format)


def tumblr2fields(api_key, blogname):
""" Imports Tumblr posts (API v2)"""
def _get_tumblr_posts(api_key, blogname, offset=0):
import json
import urllib.request as urllib_request
url = ("https://api.tumblr.com/v2/blog/%s.tumblr.com/"
"posts?api_key=%s&offset=%d&filter=raw") % (
blogname, api_key, offset)
request = urllib_request.Request(url)
handle = urllib_request.urlopen(request)
posts = json.loads(handle.read().decode('utf-8'))
return posts.get('response').get('posts')

def get_tumblr_posts(api_key, blogname, offset=0):
url = ("https://api.tumblr.com/v2/blog/%s.tumblr.com/"
"posts?api_key=%s&offset=%d&filter=raw") % (
blogname, api_key, offset)
request = urllib_request.Request(url)
handle = urllib_request.urlopen(request)
posts = json.loads(handle.read().decode('utf-8'))
return posts.get('response').get('posts')

def tumblr2fields(api_key, blogname):
""" Imports Tumblr posts (API v2)"""
offset = 0
posts = get_tumblr_posts(api_key, blogname, offset)
posts = _get_tumblr_posts(api_key, blogname, offset)
subs = DEFAULT_CONFIG['SLUG_REGEX_SUBSTITUTIONS']
while len(posts) > 0:
for post in posts:
Expand All @@ -428,12 +428,10 @@ def get_tumblr_posts(api_key, blogname, offset=0):
fmtstr = '![%s](%s)'
else:
fmtstr = '<img alt="%s" src="%s" />'
content = ''
for photo in post.get('photos'):
content += '\n'.join(
fmtstr % (photo.get('caption'),
photo.get('original_size').get('url')))
content += '\n\n' + post.get('caption')
content = '\n'.join(
fmtstr % (photo.get('caption'),
photo.get('original_size').get('url'))
for photo in post.get('photos'))
elif type == 'quote':
if format == 'markdown':
fmtstr = '\n\n&mdash; %s'
Expand Down Expand Up @@ -483,7 +481,7 @@ def get_tumblr_posts(api_key, blogname, offset=0):
tags, status, kind, format)

offset += len(posts)
posts = get_tumblr_posts(api_key, blogname, offset)
posts = _get_tumblr_posts(api_key, blogname, offset)


def feed2fields(file):
Expand Down

0 comments on commit 8aad252

Please sign in to comment.