Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tumblr backup updated for Python 3 #96

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

heatherbmayer
Copy link

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.


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

@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

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

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)

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

@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


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

@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
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

GiovanH commented Dec 3, 2018

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

@thatcatlooksstrong
Copy link

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'

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

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

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

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))

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

@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
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
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

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