Skip to content

Instantly share code, notes, and snippets.

@balbinus
Created December 3, 2018 21:13
Show Gist options
  • Save balbinus/54a04582f364915dc68fbe4a5311b246 to your computer and use it in GitHub Desktop.
Save balbinus/54a04582f364915dc68fbe4a5311b246 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
'''
This script downloads your likes from your Tumblr blog.
It needs *BOTH* a valid OAuth key + secret *AND* a user token (obtained through OAuth).
Obtain the former here: https://www.tumblr.com/oauth/apps
Obtain the latter through API Explorer: https://api.tumblr.com/console/calls/user/info
(or through the nonsense of an OAuth dance if you'd like, this is Tumblr anyway,
I won't kink-shame you. But seriously.)
Don't forget to set your blog name below.
The script will create .json files with the API responses, and .urls lists with the
corresponding media that should be downloaded (JPG/PNG/GIF/MP4 hosted on domains
matching <what.ever>.tumblr.com).
Use wget -xi <(cat *.urls) to download the bulk of media.
As of today (2018-12-03), the pytumblr library can be obtained with pip install pytumblr.
I guess you could also get it from GitHub: https://github.com/tumblr/pytumblr.
Copyright (C) 2018, balbinus
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
'''
import pytumblr, json, re
BLOG_NAME='<YOURBLOG>.tumblr.com'
def file_put_contents(filename, contents):
f = open(filename, 'w')
f.write(contents)
f.close()
# Authenticate via OAuth
client = pytumblr.TumblrRestClient(
'<consumer_key>',
'<consumer_secret>',
'<oauth_token>',
'<oauth_secret>'
)
# Make the request
client_info = client.info()
file_put_contents("client_info.json", json.dumps(client_info, indent=True))
match_http = re.compile('(?<=")https?://[^"]+(?=")', flags=re.IGNORECASE)
match_http_tumblr = re.compile('https?://([a-z0-9\.-]+\.)?tumblr\.com/', flags=re.IGNORECASE)
match_extension = re.compile('\.(png|gif|jpg|mp4)$', flags=re.IGNORECASE)
def is_tumblr_url(url):
if match_http_tumblr.match(url):
if match_extension.search(url):
return True
else:
print("Tumblr URL, but not media '%s'" % (url))
return False
else:
print("External URL? '%s'" % (url))
return False
before_val = 0
while (before_val >= 0):
if before_val != 0:
likes = client.blog_likes(BLOG_NAME, before=before_val)
else:
likes = client.blog_likes(BLOG_NAME)
file_put_contents("client_likes_%u.json" % (int(before_val)), json.dumps(likes, indent=True))
urls = []
for post in likes['liked_posts']:
if post['type'] == 'video':
if post['video_type'] == 'tumblr':
try:
#print("[VIDEO] %s | %s" % (post['video_url'], post['thumbnail_url']))
urls.extend([post['video_url'], post['thumbnail_url']])
except KeyError:
print("Missing keys in video post")
print(post)
else:
print("Non-Tumblr video post '%s'" % (post["video_type"]))
elif post['type'] == 'photo':
try:
photo_urls = [photo['original_size']['url'] for photo in post["photos"]]
#print("[PHOTO] %s" % (' | '.join(photo_urls)))
urls.extend(photo_urls)
except KeyError:
print("Missing keys in photo post")
print(post)
elif post['type'] == 'text':
try:
text_urls = filter(is_tumblr_url, set(match_http.findall(post["body"])))
#print("[TEXT] %s" % (' | '.join(text_urls)))
urls.extend(text_urls)
except KeyError:
print("Missing keys in text post")
print(post)
else:
print("Unknown post type '%s'!" % (post['type']))
file_put_contents("client_likes_%u.urls" % (int(before_val)), "\n".join(urls))
before_val = likes['_links']['next']['query_params']['before']
print(before_val)
#~ break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment