-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgateway.py
More file actions
executable file
·279 lines (230 loc) · 8.66 KB
/
gateway.py
File metadata and controls
executable file
·279 lines (230 loc) · 8.66 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/python3
#
# based on https://gist.github.com/PSingletary/2396707785834418dab00e3d7a5c822f
# https://github.com/bluesky-social/atproto-website/blob/main/examples/create_bsky_post.py
import feedparser
import requests
import sqlite3
import re
from datetime import datetime, timedelta, timezone
import os
import sys
import json
from typing import Dict, List
from bs4 import BeautifulSoup
bsky_site = os.environ['bsky_site']
bsky_user = os.environ['bsky_user']
bsky_password = os.environ['bsky_password']
feed_url = os.environ['feed_url']
def bsky_login_session(pds_url: str, handle: str, password: str) -> Dict:
resp = requests.post(
pds_url + "/xrpc/com.atproto.server.createSession",
json={"identifier": handle, "password": password},
)
resp.raise_for_status()
return resp.json()
def upload_file(pds_url, access_token, filename, img_bytes) -> Dict:
suffix = filename.split(".")[-1].lower()
mimetype = "application/octet-stream"
if suffix in ["png"]:
mimetype = "image/png"
elif suffix in ["jpeg", "jpg"]:
mimetype = "image/jpeg"
elif suffix in ["webp"]:
mimetype = "image/webp"
# WARNING: a non-naive implementation would strip EXIF metadata from JPEG files here by default
resp = requests.post(
pds_url + "/xrpc/com.atproto.repo.uploadBlob",
headers={
"Content-Type": mimetype,
"Authorization": "Bearer " + access_token,
},
data=img_bytes,
)
resp.raise_for_status()
return resp.json()["blob"]
def fetch_embed_url_card(pds_url: str, access_token: str, url: str) -> Dict:
# the required fields for an embed card
card = {
"uri": url,
"title": "",
"description": "",
}
# fetch the HTML
try:
resp = requests.get(url)
resp.raise_for_status()
except requests.exceptions.ConnectionError:
return # just don't use an embed card
except requests.exceptions.HTTPError:
return # just don't use an embed card
soup = BeautifulSoup(resp.text, "html.parser")
title_tag = soup.find("meta", property="og:title")
if title_tag:
card["title"] = title_tag["content"]
description_tag = soup.find("meta", property="og:description")
if description_tag:
card["description"] = description_tag["content"]
max_image_file_size = 950000
image_tag = soup.find("meta", property="og:image")
if image_tag:
img_url = image_tag["content"]
if "http://localhost" in img_url:
return # can't use this image, don't use a card
if "://" not in img_url:
img_url = url + img_url
try:
resp = requests.get(img_url)
resp.raise_for_status()
if len(resp.content) > max_image_file_size:
return # just don't use an embed card
card["thumb"] = upload_file(pds_url, access_token, img_url, resp.content)
except requests.exceptions.HTTPError:
return # just don't use an embed card
return {
"$type": "app.bsky.embed.external",
"external": card,
}
def parse_urls(text: str) -> List[Dict]:
spans = []
# partial/naive URL regex based on: https://stackoverflow.com/a/3809435
# tweaked to disallow some training punctuation
url_regex = rb"[$|\W](https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*[-a-zA-Z0-9@%_\+~#//=])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(url_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"url": m.group(1).decode("UTF-8"),
}
)
return spans
def parse_mentions(text: str) -> List[Dict]:
spans = []
# regex based on: https://atproto.com/specs/handle#handle-identifier-syntax
mention_regex = rb"[$|\W](@([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)"
text_bytes = text.encode("UTF-8")
for m in re.finditer(mention_regex, text_bytes):
spans.append(
{
"start": m.start(1),
"end": m.end(1),
"handle": m.group(1)[1:].decode("UTF-8"),
}
)
return spans
def parse_facets(post: Dict, pds_url: str, text: str, access_token: str) -> Dict:
"""
parses post text and returns a list of app.bsky.richtext.facet objects for any mentions (@handle.example.com) or URLs (https://example.com)
indexing must work with UTF-8 encoded bytestring offsets, not regular unicode string offsets, to match Bluesky API expectations
"""
facets = []
for m in parse_mentions(text):
resp = requests.get(
pds_url + "/xrpc/com.atproto.identity.resolveHandle",
params={"handle": m["handle"]},
)
# if handle couldn't be resolved, just skip it! will be text in the post
if resp.status_code == 400:
continue
did = resp.json()["did"]
facets.append(
{
"index": {
"byteStart": m["start"],
"byteEnd": m["end"],
},
"features": [{"$type": "app.bsky.richtext.facet#mention", "did": did}],
}
)
link = ''
for u in parse_urls(text):
link = u["url"]
facets.append(
{
"index": {
"byteStart": u["start"],
"byteEnd": u["end"],
},
"features": [
{
"$type": "app.bsky.richtext.facet#link",
# NOTE: URI ("I") not URL ("L")
"uri": u["url"],
}
],
}
)
if facets:
post["facets"] = facets
embed = fetch_embed_url_card(pds_url, access_token, link)
if embed:
post["embed"] = embed
return post
def create_post(text, link):
session = bsky_login_session(bsky_site, bsky_user, bsky_password)
# trailing "Z" is preferred over "+00:00"
now = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
# these are the required fields which every post must include
post = {
"$type": "app.bsky.feed.post",
"text": text,
"createdAt": now,
}
# parse out mentions and URLs as "facets"
# post text string always ends with the link
if len(text) > 0:
post = parse_facets(post, bsky_site, post["text"], session["accessJwt"])
print("creating post:", file=sys.stderr)
print(json.dumps(post, indent=2), file=sys.stderr)
resp = requests.post(
bsky_site + "/xrpc/com.atproto.repo.createRecord",
headers={"Authorization": "Bearer " + session["accessJwt"]},
json={
"repo": session["did"],
"collection": "app.bsky.feed.post",
"record": post,
},
)
print("createRecord response:", file=sys.stderr)
print(json.dumps(resp.json(), indent=2))
resp.raise_for_status()
# Parse the RSS feed
feed = feedparser.parse(feed_url)
one_week_ago = datetime.now() - timedelta(weeks=1)
# Connect to the SQLite database
conn = sqlite3.connect('rss_feed_tracker.db')
c = conn.cursor()
# Create a table to store processed items if it doesn't exist
c.execute('''CREATE TABLE IF NOT EXISTS processed_items
(link TEXT PRIMARY KEY)''')
# Loop through each entry in the feed
for entry in reversed(feed.entries):
# Extract the title, link, and description
title = entry.title
link = entry.link
description = entry.description
# strip HTML tags
description = re.sub(r'</?blockquote>', '\"', description)
description = re.sub(r'</?[A-Za-z]*>', '', description)
# Check if the item has already been processed
c.execute('SELECT * FROM processed_items WHERE link = ?', (link,))
date = datetime(*entry.updated_parsed[:6])
if c.fetchone() is None and date > one_week_ago:
# Print the extracted information
print(f"Title: {title} Link: {link}")
extralen = len(link) + 4
#extralen = len(title) + len(link) + 4
if len(description) > 296-extralen:
shortdesc = description[:293-extralen]
shortdesc = re.sub(r'\w+$', '', shortdesc.rstrip()).rstrip()
description = shortdesc + " [\u2026]"
create_post(f"{description}\n\n{link}", link)
#create_post(f"{title} \u2014 {description}\n\n{link}", link)
# Check the response status code
print("Successfully posted")
# Mark the item as processed
c.execute('INSERT INTO processed_items (link) VALUES (?)', (link,))
conn.commit()
conn.close()