-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
194 lines (163 loc) · 6.25 KB
/
main.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Read home timeline into RSS"""
import base64
import logging
import os
from functools import lru_cache
from io import BytesIO
from pathlib import Path
import cherrypy
import requests
from feedgen.feed import FeedGenerator
from mastodon import Mastodon
from PIL import Image
from prometheus_client import start_http_server
from prometheus_client.core import (REGISTRY, CounterMetricFamily,
GaugeMetricFamily)
from prometheus_client.registry import Collector
from yarl import URL
logging.basicConfig(level=logging.DEBUG)
SCOPES = ["read:statuses", "read:accounts"]
OWN_MASTODON_INSTANCE = os.environ["OWN_MASTODON_INSTANCE"]
PUBLIC_URL = URL(os.environ["PUBLIC_URL"])
def get_smaller_image(url):
return str(
PUBLIC_URL / "image" % {"id": base64.urlsafe_b64encode(url.encode("utf-8")).decode("utf-8")}
)
@lru_cache(maxsize=10000)
def small_image(id):
response = requests.get(base64.urlsafe_b64decode(id))
img = Image.open(BytesIO(response.content))
img.thumbnail((32, 32))
buffer = BytesIO()
img.save(buffer, "PNG")
return buffer.getvalue()
class CustomCollector(Collector):
def collect(self):
data = small_image.cache_info()
yield GaugeMetricFamily(
"mastodon2rss_image_cache_maxsize", "maxsize of the image cache", value=data.maxsize
)
yield GaugeMetricFamily(
"mastodon2rss_image_cache_hits", "hits of the image cache", value=data.hits
)
yield GaugeMetricFamily(
"mastodon2rss_image_cache_misses", "misses of the image cache", value=data.misses
)
yield GaugeMetricFamily(
"mastodon2rss_image_cache_currsize", "currsize of the image cache", value=data.currsize
)
REGISTRY.register(CustomCollector())
def render_post(post):
yield from (
"<div>",
f"""<img src="{get_smaller_image(post['account']['avatar'])}"/>""",
f"""<a href="{post.get("uri")}">{post['account']['username']}</a>"""
if post["uri"]
else f"""{post['account']['username']}""",
"</div>",
)
yield from ("<div>", post.get("content"), "</div>")
for ma in post["media_attachments"]:
if ma["type"] == "image":
yield from (
"<div>",
f'<img src="{ma["url"]}"/>',
"</div>",
)
elif ma["type"] in ("video", "gifv"):
yield from ("<div>", f'<video src="{ma["url"]}"/>', "</div>")
else:
yield from ("<div>", ma["type"], "</div>")
if post["card"] is not None:
if post["card"]["type"] == "link":
img = (
f"""<div><img src="{get_smaller_image(post['card']['image'])}"/></div>"""
if post["card"]["image"] is not None
else ""
)
yield f"""<a href="{post['card']['url']}" target="_blank" rel="noopener noreferrer">{img}<div><strong>{post['card']['title']}</strong></div></a>"""
if post["reblog"] is not None:
yield from ("""<div style="margin-left: 40px">""", *render_post(post["reblog"]), "</div>")
def get_app():
"""Return client_id, secret, api_base_url, maybe loaded from fs, maybe newly created"""
app = Path("./app")
if not app.exists():
api_base_url = f"https://{OWN_MASTODON_INSTANCE}"
client_id, client_secret = Mastodon.create_app(
"mastodon2rss",
api_base_url=api_base_url,
to_file=app,
scopes=SCOPES,
redirect_uris=[str(PUBLIC_URL / "login_token")],
)
else:
client_id, client_secret, api_base_url, _ = (
x.strip() for x in app.open("r", encoding="utf-8").readlines()
)
return client_id, client_secret, api_base_url
class MastodonFeed:
def _try_login(self):
creds = Path("./creds")
if creds.exists():
self._mastodon = Mastodon(access_token=creds)
self.logged_in = True
def __init__(self):
client_id, client_secret, api_base_url = get_app()
self._mastodon = Mastodon(
client_id=client_id, client_secret=client_secret, api_base_url=api_base_url
)
self.logged_in = False
self._try_login()
@cherrypy.expose
def image(self, id):
cherrypy.response.headers["Content-Type"] = "image/png"
return small_image(id)
@cherrypy.expose
def index(self):
if self.logged_in:
yield "<html><body>"
for post in self._mastodon.timeline():
yield "<div>"
yield from render_post(post)
yield "</div>"
yield "</body></html>"
return
raise cherrypy.HTTPRedirect(
self._mastodon.auth_request_url(scopes=SCOPES, redirect_uris=str(PUBLIC_URL / "login_token"))
)
@cherrypy.expose
def feed(self):
cherrypy.response.headers["Content-Type"] = "application/atom+xml"
if self.logged_in:
fg = FeedGenerator()
fg.title("Mastodon home Feed")
fg.language("en")
fg.id(f"https://{OWN_MASTODON_INSTANCE}/")
for post in self._mastodon.timeline():
fe = fg.add_entry()
fe.id(post["uri"])
if post["url"] is not None:
fe.link(href=post["url"])
else:
if post["reblog"]["url"] is not None:
fe.link(href=post["reblog"]["url"])
fe.id(post["reblog"]["uri"])
fe.title(f"Post by {post['account']['username']} at {post['created_at']}")
fe.content(content="".join(render_post(post)), type="CDATA")
return fg.atom_str()
raise cherrypy.HTTPError("403" "login first")
@cherrypy.expose
def login_token(self, code, **_):
creds = Path("./creds")
self._mastodon.log_in(
code=code,
to_file=creds,
scopes=SCOPES,
redirect_uri=str(PUBLIC_URL / "login_token"),
)
self.logged_in = True
raise cherrypy.HTTPRedirect(str(PUBLIC_URL))
if __name__ == "__main__":
cherrypy.config.update({"server.socket_host": "0.0.0.0", "server.socket_port": 12345})
start_http_server(8000)
cherrypy.quickstart(MastodonFeed())