-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate_backup.py
313 lines (249 loc) · 9.66 KB
/
validate_backup.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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import datetime
import random
import sqlite3
import string
from typing import ItemsView
import indieweb_utils
import mf2py
import mf2util
import requests
from bs4 import BeautifulSoup
from config import (CLIENT_ID, ME, RSS_DIRECTORY, WEBHOOK_API_KEY,
WEBHOOK_SERVER, WEBHOOK_URL)
from create_rss_feed import generate_feed
from send import send_function
def final_checks(cursor, entry, url):
# if item in db, don't add again
in_db = cursor.execute(
"SELECT * FROM sent_webmentions WHERE source = ? and target = ?",
(entry["properties"]["url"][0], url),
).fetchone()
if in_db:
return
_, item = send_function.send_webmention(
entry["properties"]["url"][0], url, is_validating=True
)
if len(item) == 0:
return
# Add webmentions to sent_webmentions table
cursor.execute(
"""
INSERT INTO sent_webmentions (
source,
target,
sent_date,
status_code,
response,
webmention_endpoint,
location_header,
vouch,
approved_to_show
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
tuple(item),
)
print("Webmention sent to " + item[0] + " from " + item[1])
def process_pending_webmention(item, cursor):
feed_url = item[0]
last_url_sent = cursor.execute(
"SELECT feed_url, last_url_sent FROM webhooks WHERE feed_url = ?;", (feed_url,)
).fetchone()
try:
parsed = mf2py.Parser(url=feed_url).to_dict()
except:
return
# find h_feed item
h_feed = None
for item in parsed["items"]:
if item["type"] == ["h-feed"]:
h_feed = item
entries = []
# get all h-entry objects
if h_feed:
for item in h_feed["children"]:
if item["type"] == ["h-entry"]:
entries.append(item)
else:
for item in parsed["items"]:
if item["type"] == ["h-entry"]:
entries.append(item)
domain = feed_url.split("/")[2]
if len(entries) > 0:
if last_url_sent is not None:
last_url_sent = last_url_sent[1]
else:
last_url_sent = ""
last_url_sent = ""
if (
entries[0]
and entries[0].get("properties", {}).get("url")
and last_url_sent != entries[0]["properties"]["url"][0]
):
for entry in entries:
if last_url_sent == entry["properties"]["url"][0]:
break
try:
canonicalized_url = indieweb_utils.canonicalize_url(
entry["properties"]["url"][0],
domain,
entry["properties"]["url"][0],
)
get_page = requests.get(canonicalized_url)
except:
continue
if get_page.status_code == 200:
soup = BeautifulSoup(get_page.text, "lxml")
if soup.select(".e-content"):
soup = soup.select(".e-content")[0]
else:
continue
links = [
link.get("href")
for link in soup.find_all("a")
if link.get("href")
and not link.get("href").startswith("https://" + domain)
and not link.get("href").startswith("http://" + domain)
and not link.get("href").startswith("/")
and not link.get("href").startswith("#")
and not link.get("href").startswith("javascript:")
]
links = list(set(links))
for url in links:
url = indieweb_utils.canonicalize_url(url, domain, url)
entry["properties"]["url"][0] = indieweb_utils.canonicalize_url(
entry["properties"]["url"][0],
domain,
entry["properties"]["url"][0],
)
final_checks(cursor, entry, url)
if not last_url_sent and entries[0]:
cursor.execute(
"INSERT INTO webhooks (feed_url, last_url_sent) VALUES (?, ?)",
(feed_url, entries[0]["properties"]["url"][0]),
)
else:
cursor.execute(
"UPDATE webhooks SET last_url_sent = ? WHERE feed_url = ?",
(entries[0]["properties"]["url"][0], feed_url),
)
def validate_webmentions():
connection = sqlite3.connect(RSS_DIRECTORY + "webmentions.db")
cursor = connection.cursor()
with connection:
get_pending_webmentions = cursor.execute(
"SELECT to_check FROM pending_webmentions;"
).fetchall()
for item in get_pending_webmentions:
process_pending_webmention(item, cursor)
cursor.execute("DELETE FROM pending_webmentions;")
get_webmentions_for_url = cursor.execute(
"SELECT source, target, vouch FROM webmentions WHERE status = 'validating';"
).fetchall()
vouch_list = cursor.execute("SELECT domain FROM vouch;").fetchall()
for item in get_webmentions_for_url:
source = item[0]
target = item[1]
vouch = item[2]
print(f"processing webmention from {source} to {target}")
is_valid, moderate = indieweb_utils.validate_webmention(
source, target, vouch, vouch_list
)
if is_valid == False:
cursor.execute(
"UPDATE webmentions SET status = ? WHERE source = ? and target = ?",
("invalid", source, target),
)
print(f"invalid webmention from {source} to {target}")
parse = mf2py.Parser(url=source)
parsed_h_entry = mf2util.interpret_comment(parse.to_dict(), source, target)
# Convert webmention published date to a readable timestamp rather than a datetime object
# per default (returns error and causes malformed parsing)
if parsed_h_entry.get("published"):
parsed_h_entry["published"] = parsed_h_entry["published"].strftime(
"%m/%d/%Y %H:%M:%S"
)
else:
now = datetime.datetime.now()
parsed_h_entry["published"] = now.strftime("%m/%d/%Y %H:%M:%S")
if parsed_h_entry.get("author"):
author_photo = parsed_h_entry["author"].get("photo")
author_url = parsed_h_entry["author"].get("url")
author_name = parsed_h_entry["author"].get("name")
else:
author_photo = None
author_url = None
author_name = None
if author_photo:
try:
r = requests.get(author_photo)
random_letters = "".join(
random.choice(string.ascii_lowercase) for i in range(8)
)
if "jpg" in author_photo:
extension = "jpg"
elif "png" in author_photo:
extension = "png"
elif "gif" in author_photo:
extension = "gif"
else:
extension = "jpeg"
if r.status_code == 200:
with open(
RSS_DIRECTORY
+ f"/static/images/{random_letters + '.' + extension}",
"wb+",
) as f:
f.write(r.content)
author_photo = (
CLIENT_ID + "/static/images/" + random_letters + "." + extension
)
except:
pass
if parsed_h_entry.get("content-plain"):
content = parsed_h_entry["content-plain"]
else:
content = None
if parsed_h_entry.get("content"):
content_html = parsed_h_entry["content"]
parsed_h_entry["content"] = parsed_h_entry["content"].replace(
"<a ", "<a rel='nofollow'>"
)
else:
content_html = None
h_entry = [
item for item in parse.to_dict()["items"] if item["type"] == ["h-entry"]
]
if len(h_entry) > 0:
post_type = indieweb_utils.get_post_type(h_entry[0])
else:
post_type = "reply"
cursor.execute(
"""
UPDATE webmentions SET contents = ?,
property = ?,
author_name = ?,
author_photo = ?,
author_url = ?,
content_html = ?,
status = ?,
approved_to_show = ?
WHERE source = ? AND target = ?""",
(
content,
post_type,
author_name,
author_photo,
author_url,
content_html,
"valid",
moderate,
source,
target,
),
)
connection.commit()
print(f"done with {source}")
print(f"{len(get_webmentions_for_url)} Webmentions processed.")
validate_webmentions()
generate_feed()