-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
167 lines (135 loc) · 4.62 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import pytumblr
import sys
import logging
logging.basicConfig(
filename="logfile.log",
level=logging.INFO,
format="%(asctime)s.%(msecs)03d %(levelname)s %(module)s - %(funcName)s: %(message)s Line: %(lineno)d",
datefmt="%Y-%m-%d %H:%M:%S",
)
logging.getLogger().addHandler(logging.StreamHandler())
from configs import (
CONSUMER_KEY,
CONSUMER_SECRET,
OAUTH_SECRET,
OAUTH_TOKEN,
BLOG_NAME,
LIMIT,
TIMESTAMP_FILE,
)
def get_timestamp_from_file(file_name):
"""Read the saved timestamp from file
Parameters
----------
file_name : str
Name of the file
Returns
-------
int
Last saved timestamp on file or 0 if no data on file
"""
timestamp = 0
try:
with open(file_name, "r") as timestamp_file:
timestamp_string = timestamp_file.read().strip()
if timestamp_string:
try:
timestamp = int(timestamp_string)
except ValueError:
logging.error(
"Timestamp format not valid. Timestamp should be in UNIX timestamp format."
)
logging.info(
"For more about UNIX timestamp, please check - https://en.wikipedia.org/wiki/Unix_time"
)
sys.exit(1)
if len(timestamp_string) > 10:
logging.info("Are you from heaven? Is Tumblr still around?")
input("Press Enter to continue or CTRL + C to get back to earth.")
except FileNotFoundError:
logging.error("Timestamp file not found! Existing..")
sys.exit(1)
return timestamp
def write_timestamp_to_file(file_name, timestamp):
"""Save last timestamp to file
Parameters
----------
file_name : str
Name of the file
timestamp: int
Timestamp to save
Returns
-------
None
"""
with open(file_name, "w") as timestamp_file:
timestamp_file.write(str(timestamp))
def get_likes_list(client, timestamp, limit=50):
"""Get data of 50 likes that were liked after the timstamp from Tumblr API
Parameters
----------
client : TumblrRestClient object
Handles the connection with Tumblr API
timestamp : int
Data will be fetched of posts that were liked after this timestamp
limit : int, optional
Number (maximum) of posts that to be fetched in a batch.
Returns
-------
dict
A dict containing the liked posts data.
Or emtpy in case of errors with connection.
"""
if limit > 50:
logging.warning(
f"Provided limit is {limit}. Tumblr defaults limit to 50 for any values over 50."
)
try:
# get the data
likes_list = client.likes(after=timestamp, limit=50)
ret_data = likes_list["liked_posts"]
ret_data.reverse()
return ret_data
except Exception as tumblr_api_connection_error:
logging.error(tumblr_api_connection_error)
logging.info("Something went wrong with connecting to Tumblr API")
return {}
def main():
"""Run this thing"""
timestamp = get_timestamp_from_file(TIMESTAMP_FILE)
# create tumblr client
client = pytumblr.TumblrRestClient(
CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET
)
# This is flood bot
# Tries to post upto 250 posts if available
for x in range(5):
if x == 0:
timestamp = timestamp
else:
timestamp = liked_posts[-1]["liked_timestamp"]
# get the posts
liked_posts = get_likes_list(client, timestamp, limit=LIMIT)
if liked_posts:
for index, post in enumerate(liked_posts):
last_post = client.reblog(
BLOG_NAME,
id=post["id"],
reblog_key=post["reblog_key"],
)
logging.info(
f"{x*50 + index} - {post['blog_name']}: {post['post_url']} -- {post['liked_timestamp']}"
)
try:
post_id = last_post["id"]
except Exception as cant_post:
logging.error("Failed to post")
logging.error(cant_post)
timestamp = liked_posts[-1]["liked_timestamp"]
else:
logging.warning("No new liked posts")
break
logging.info(f"Last timestamp after this run - {timestamp}")
write_timestamp_to_file(TIMESTAMP_FILE, timestamp)
if __name__ == "__main__":
main()