-
Notifications
You must be signed in to change notification settings - Fork 0
/
tumblr.py
54 lines (47 loc) · 1.63 KB
/
tumblr.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
import pytumblr
import secret
from datelib import *
consumer = 'h93vg3vWvfWJKloahZ6EoJzVwv9D2FhORp3degELUdk4WK6puF'
app = 'OLXURhKe7gUw8dE1A142VQjHDYoxNDMGHYncQSGHiL5eoxGq4v'
# Authenticate via OAuth
client = pytumblr.TumblrRestClient(
consumer,
secret.tumblr_consumer,
app,
secret.tumblr_app,
)
def process_post(post):
out = {}
# timestamp will be stored in seconds since the epoch
out["timestamp"] = post["timestamp"]
out["origin"] = "tumblr"
post_type = post["type"]
# text, quote, link, answer, video, audio, photo, chat
if post_type == "photo" or post_type == "video" or post_type == "audio":
text = post["caption"]
elif post_type == "text" or post_type == "chat":
text = post["body"]
elif post_type == "quote":
text = post["text"]
elif post_type == "link":
text = post["description"]
elif post_type == "answer":
text = post["answer"]
# add the text from tags
tags = " ".join([a+"." for a in post["tags"]])
text = text + ".\n " + tags
if text == ".\n ":
return None
out["text"] = text
out["url"] = post["post_url"] # for reference
return out
# returns an array of formatted posts
# from day of timestamp in UTC, no DST
def get_posts(month, day, year, tag="lol"):
end = end_timestamp(month, day, year)
begin = begin_timestamp(month, day, year)
response = client.tagged(tag, filter="text", before=end)
within_day = [item for item in response if item["timestamp"] >= begin]
out = [process_post(post) for post in within_day]
out = [post for post in out if post is not None]
return out