· 7 years ago · Aug 30, 2018, 06:50 AM
1# get all of the relevant meta stats for tumblr account
2# save all information to a class
3# print the class to a text file or something
4import pytumblr
5import os
6
7
8# Create our Tumblr User Class
9class tumblr_user:
10 def __init__(self, name, posts, likes, following_count, follower_count, message_count, blog_link, all_blog_links):
11 self.name = name
12 self.posts = posts
13 self.likes = likes
14 self.following_count = following_count
15 self.follower_count = follower_count
16 self.message_count = message_count
17 self.blog_link = blog_link
18 self.all_blog_links = all_blog_links
19
20
21try:
22 # Authenticate via OAuth
23 client = pytumblr.TumblrRestClient(
24 '<consumer_key>',
25 '<consumer_secret>',
26 '<oauth_token>',
27 '<oauth_secret>',
28 )
29
30 def get_blog_urls(blogs):
31 url_list = []
32 for blog in blogs:
33 url_list.append(blog['url'])
34 return url_list
35
36 # Make the request
37 my_info = client.info()
38 # print(my_info)
39 my_blog = tumblr_user(
40 my_info['user']['name'],
41 my_info['user']['blogs'][0]['posts'],
42 my_info['user']['likes'],
43 my_info['user']['following'],
44 my_info['user']['blogs'][0]['followers'],
45 my_info['user']['blogs'][0]['messages'],
46 my_info['user']['blogs'][0]['url'],
47 get_blog_urls(my_info['user']['blogs'])
48 )
49
50 for_print = [
51 "Your Tumblr name: " + str(my_blog.name),
52 "Your total amount of posts: " + str(my_blog.posts),
53 "Your total amount of likes: " + str(my_blog.likes),
54 "You follow " + str(my_blog.following_count) + " blogs",
55 str(my_blog.follower_count) + " blogs are following you",
56 "You have " + str(my_blog.message_count) + " messages",
57 "All blogs: " + str(my_blog.all_blog_links),
58 "Your url: " + str(my_blog.blog_link)
59 ]
60
61 tumblr_file_name = "tumblr_stats.txt"
62
63 if os.path.isfile(tumblr_file_name):
64 print("File already exists: " + tumblr_file_name)
65 print("Saving stats...\n")
66 tumblr_file = open(tumblr_file_name, "a")
67 i: int = 0
68
69 while i < len(for_print):
70 tumblr_file.write(for_print[i] + "\n")
71 i += 1
72
73 tumblr_file.close() # close file when done using it
74 else:
75 print("File doesn't exist: " + tumblr_file_name)
76 print("Creating file: " + tumblr_file_name)
77 print("Saving stats...\n")
78
79 tumblr_file = open(tumblr_file_name, "a")
80 i: int = 0
81
82 while i < len(for_print):
83 tumblr_file.write(for_print[i] + "\n")
84 i += 1
85
86 tumblr_file.close() # close file when done using it
87
88 print("Saved your stats. Check the " + tumblr_file_name + " file for your information.")
89except:
90 print('Something went wrong')