Created
October 18, 2015 19:20
-
-
Save trzecieu/07fb7893d386a8c05056 to your computer and use it in GitHub Desktop.
Fetch devopreaction.tumbrl.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
api_key = "" #API KEY | |
blog = "devopsreactions.tumblr.com" | |
url_info = "https://api.tumblr.com/v2/blog/{blog}/info?api_key={api_key}" | |
url_posts = "https://api.tumblr.com/v2/blog/{blog}/posts/text?api_key={api_key}&offset={offset}" | |
def get_info(): | |
return url_info.format(api_key=api_key, blog=blog) | |
def get_posts(offset=0): | |
return url_posts.format(api_key=api_key, blog=blog, offset=offset) | |
import json | |
import urllib2 | |
import re | |
import os.path | |
import urllib | |
offset = 0 | |
while(True): | |
data = json.load(urllib2.urlopen(get_posts(offset))) | |
if data["meta"]["msg"].encode("utf-8") != "OK": | |
print("Error: ") | |
print(data) | |
break | |
response = data["response"] | |
print("[{p}%]".format(p = 100 * offset / response["blog"]["posts"])) | |
for post in response["posts"]: | |
body = post["body"].encode('utf-8') | |
p = re.compile(ur'src="(http.+\.gif)"') | |
imgs = re.findall(p, body) | |
if len(imgs) == 0 or not post["title"]: | |
continue | |
title = post["title"].encode('utf-8') | |
title = title.replace("/", " - ") | |
name = blog + "/" + title + ".gif" | |
if os.path.isfile(name): | |
print("[OK]: {name}".format(name=name)) | |
else: | |
urllib.urlretrieve (imgs[0], name) | |
print("[OK: {size}b] {name}".format(name=name, size=os.path.getsize(name))) | |
if os.path.getsize(name) < 3500: | |
print("[WARNING] Suspected size, will be removed") | |
os.remove(name) | |
if response["blog"]["posts"] > offset: | |
offset += len(response["posts"]) | |
else: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment