This repository has been archived by the owner on Jan 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtumblr.py
75 lines (59 loc) · 2.37 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import json
import os
import pytumblr
tumblrTag = os.environ.get("TUMBLR_TAG")
if (tumblrTag == None):
tumblrTag = "txt"
print("defaulting tumblrTag to txt")
tumblrKey = os.environ.get("TUMBLR_KEY")
if (tumblrKey == None):
raise EnvironmentError("TUMBLR_KEY is not defined")
client = pytumblr.TumblrRestClient(tumblrKey)
class TumblrBlog:
def __init__(self, tumblrUrl):
self.tumblrUrl = tumblrUrl
self.refresh()
def refresh(self):
if (tumblrTag != ""):
self.profile = client.posts(self.tumblrUrl, tag=tumblrTag)
else:
self.profile = client.posts(self.tumblrUrl)
if self.profile.get("meta", {}).get("status") == 404:
raise AttributeError("\"{}\" is not a valid tumblr URL - make sure you didn't include https://".format(self.tumblrUrl))
@property
def posts(self):
return self.profile.get("posts")
@property
def latestPost(self):
p = self.posts
if len(p) == 0:
return None
return p[0]
@property
def latestPostId(self):
if self.latestPost == None:
return "-1"
return self.latestPost["id_string"]
@property
def latestPostAsHTML(self):
post = self.latestPost
if post == None:
return ""
return post["body"].replace("\n", "") # Cohost"s styling will handle line breaks for us, we"re good.
@property
def latestPostWithCohostStyling(self):
latestPost = self.latestPostAsHTML
if latestPost == "":
return latestPost
latestPost = latestPost.replace('<p>', '<p style="color:white;">')
for x in range(1, 7):
latestPost = latestPost.replace('<h{}>'.format(x), '<h{} style="color:white;">'.format(x))
latestPost = '<div style="border:5px;padding:15px;background-color:#34526f;border-radius:5px;">{}</div>'.format(latestPost)
latestPost += '<small style="color: rgba(1,1,1,0.5);">synced from Tumblr with <a href=\"https://github.com/valknight/Hellbug\">Hellbug</a> - made with <a href=\"https://github.com/valknight/cohost.py\">cohost.py</a></small>'
return latestPost
@property
def uuid(self):
return self.profile.get("blog", {}).get("uuid")
if __name__ == "__main__":
blog = TumblrBlog("tumblr.valknight.xyz")
print(blog.latestPostWithCohostStyling)