Skip to content

Instantly share code, notes, and snippets.

@Hrxn
Created June 4, 2018 05:26
Show Gist options
  • Save Hrxn/130577a0989a689af8be5010380fc451 to your computer and use it in GitHub Desktop.
Save Hrxn/130577a0989a689af8be5010380fc451 to your computer and use it in GitHub Desktop.
Python: Tumblr Stats Info
import argparse, time, re
import pytumblr
ap = argparse.ArgumentParser(description='tumblr.py: Process a text file of Tumblr Blog URLs.')
ap.add_argument('input_file', metavar='FILE', type=str, help='specifies the text file to process')
ap.add_argument('-v', '--values', action='store_true', help='prints only the user-ids in FILE')
args = ap.parse_args()
# Use your account credentials here for API access (OAuth)
client = pytumblr.TumblrRestClient(
'<consumer_key>',
'<consumer_secret>',
'<oauth_token>',
'<oauth_secret>',
)
outp_head = (
'URL' ',' 'Title' ',' 'Tumblr ID' ',' 'Description' ',' 'Total Post Count' ','
'Post Count (Photo)' ',' 'Post Count (Video)' ',' 'Post Count (The Rest)' ',' 'Last Updated'
)
rx_uid = re.compile(r"(?:(?:https?\:\/\/)(?P<id>.*\.[a-z]{2,16})(?:\/)?.*)")
def tumblr_id(s):
return re.sub(rx_uid, '\g<id>', s)
def clean_csv(s):
if isinstance(s, str):
return s.replace(',', '')
else:
return str(s).replace(',', '')
with open(args.input_file) as file:
lines = filter(None, (line.rstrip() for line in file))
if args.values:
for v in lines:
uid = tumblr_id(v)
print(uid)
else:
print(outp_head)
for v in lines:
if not v[0] == '#':
uid = tumblr_id(v)
res = client.blog_info(uid)
res_p = client.posts(uid, type='photo', limit=1)
res_v = client.posts(uid, type='video', limit=1)
try:
tburl = clean_csv(res['blog']['url'])
title = clean_csv(res['blog']['title'])
posts = res['blog']['total_posts']
photo = res_p['total_posts']
video = res_v['total_posts']
other = posts - (photo + video)
bdesc = re.sub(r"\n" , ' ', clean_csv(res['blog']['description']))
utime = time.strftime('%Y-%m-%d', time.localtime(res['blog']['updated']))
except KeyError:
tstr = 'ERROR:{s}Something is wrong with the following user id:{s}{id}'
output = tstr.format(s=',', id=uid)
else:
outl = tburl + ',' + title + ',' + uid + ',' + bdesc + ',' + str(posts) + ','
output = outl + str(photo) + ',' + str(video) + ',' + str(other) + ',' + utime
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment