Skip to content

Tumblr backup updated for Python 3#96

Open
heatherbmayer wants to merge 1 commit intobbolli:masterfrom
heatherbmayer:possibilityleft
Open

Tumblr backup updated for Python 3#96
heatherbmayer wants to merge 1 commit intobbolli:masterfrom
heatherbmayer:possibilityleft

Conversation

@heatherbmayer
Copy link
Copy Markdown

Use this version for Python 3. It was converted first by hand and then I also ran 2to3 on it just to be safe. It has been tested for both full and incremental backup which was successful.

Comment thread tumblr_backup_python3.py

def pages_per_month(y, m):
posts = len(self.index[y][m])
return posts / posts_page + bool(posts % posts_page)
Copy link
Copy Markdown

@wcfr wcfr Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return posts / posts_page + bool(posts % posts_page)
return posts // posts_page + bool(posts % posts_page)

use floor division for an integer result

Comment thread tumblr_backup_python3.py
def save_year(self, idx, year):
idx.write('<h3>%s</h3>\n<ul>\n' % year)
for month in sorted(list(self.index[year].keys()), reverse=options.reverse_index):
tm = time.localtime(time.mktime(time.struct_time([year, month, 3, 0, 0, 0, 0, 0, -1])))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for time.struct_time([year, month, 3, 0, 0, 0, 0, 0, -1]), we can simply use a tuple (year, month, 3, 0, 0, 0, 0, 0, -1)

Comment thread tumblr_backup_python3.py
See https://groups.google.com/d/msg/tumblr-api/f-rRH6gOb6w/sAXZIeYx5AUJ"""
try:
resp = urlopen('http://%s/' % blog_name)
page_data = resp.read()
Copy link
Copy Markdown

@wcfr wcfr Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
page_data = resp.read()
page_data = resp.read().decode("utf-8")

can restore lines 559-561 by committing this change

Comment thread tumblr_backup_python3.py

# ensure the right date/time format
try:
locale.setlocale(locale.LC_TIME, '')
Copy link
Copy Markdown

@wcfr wcfr Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
locale.setlocale(locale.LC_TIME, '')
locale.setlocale(locale.LC_TIME, '')
locale.setlocale(locale.LC_CTYPE, '')

also need to set locale.LC_CTYPE

@bbolli
Copy link
Copy Markdown
Owner

bbolli commented Jul 4, 2018

Thanks!

Is it possible to have a single script that works with both Python 2 and Python 3? Otherwise, the installation and setup gets too complicated for the average user.

@bbolli bbolli mentioned this pull request Aug 12, 2018
@GiovanH
Copy link
Copy Markdown

GiovanH commented Dec 3, 2018

Python2 is depreciated, and so the average user should only be using Python3 anyway.

@thatcatlooksstrong
Copy link
Copy Markdown

I have a problem running this version (I am using 3.5 with the Mac OS X command line)

python tumblr_backup_python3.py -O /Volumes/G-DRIVE\ ev/Tumblr blogname
Traceback (most recent call last):on
File "tumblr_backup_python3.py", line 1087, in
tb.backup(account)
File "tumblr_backup_python3.py", line 473, in backup
soup = apiparse(base, 1)
File "tumblr_backup_python3.py", line 216, in apiparse
doc = json.loads(data)
File "//anaconda/lib/python3.5/json/init.py", line 312, in loads
s.class.name))
TypeError: the JSON object must be str, not 'bytes'

Comment thread tumblr_backup_python3.py
if len(options.period) == 8:
i = 2
tm[2] = int(options.period[6:8])
options.p_start = time.mktime(tm)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options.p_start = time.mktime(tm)
options.p_start = time.mktime(tuple(tm))

a tuple or time.struct_time object is required

Comment thread tumblr_backup_python3.py
tm[2] = int(options.period[6:8])
options.p_start = time.mktime(tm)
tm[i] += 1
options.p_stop = time.mktime(tm)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
options.p_stop = time.mktime(tm)
options.p_stop = time.mktime(tuple(tm))

Comment thread tumblr_backup_python3.py
for _ in range(10):
try:
resp = urllib.request.urlopen(url)
data = resp.read()
Copy link
Copy Markdown

@wcfr wcfr Dec 4, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data = resp.read()
data = resp.read().decode("utf-8")

for compatibility with python version <3.6

@bbolli
Copy link
Copy Markdown
Owner

bbolli commented Dec 4, 2018

Python2 is depreciated, and so the average user should only be using Python3 anyway.

That's premature. macOS High Sierra only comes with Python 2, e.g.

@cebtenzzre
Copy link
Copy Markdown
Collaborator

@bbolli My local fork of tumblr-utils (with features from a lot of my PRs) is fully compatible with both Python 2 and Python 3 in a single script. So, it's possible, it just requires a little more care.

@joesmork
Copy link
Copy Markdown

You might want to remove that API key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants