-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebmention_static_kappa.py
296 lines (267 loc) · 11.3 KB
/
webmention_static_kappa.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
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from pelican import signals, contents
import os, urllib.request, datetime, sys
import json
from collections import OrderedDict
from urllib.parse import urlparse
#from django.core.paginator import Paginator
import django.core.paginator
"""
This plugin is for using with webmention.io jf2 API
"""
#PROCESS = ['articles', 'pages', 'drafts']
#PROCESS = ['articles']
def article_url(content):
#return content.settings[SITEURL]+'/'+content.url
return WEBMENTION_SITEURL + "/" + content.url
def initialize_module(pelican):
global WEBMENTION_IO_JF2_URL, WEBMENTION_SITEURL, WEBMENTION_IO_MAX_ITEMS, WEBMENTION_IO_API_KEY, WEBMENTION_IO_CACHE_FILENAME, WEBMENTION_IO_DOMAIN, WEBMENTION_IO_OVERWRITE_INITIAL_CACHE, WEBMENTION_IO_UPDATE_INITIAL_CACHE, WEBMENTION_IO_UPDATE_CACHE, WEBMENTION_IO_REPLIED_PAGINATION_SIZE
for parameter in [ 'WEBMENTION_IO_JF2_URL', 'WEBMENTION_SITEURL',
'WEBMENTION_IO_MAX_ITEMS', 'WEBMENTION_IO_API_KEY',
'WEBMENTION_IO_CACHE_FILENAME', 'WEBMENTION_IO_DOMAIN',
'WEBMENTION_IO_OVERWRITE_INITIAL_CACHE', 'WEBMENTION_IO_UPDATE_INITIAL_CACHE',
'WEBMENTION_IO_UPDATE_CACHE', 'WEBMENTION_IO_REPLIED_PAGINATION_SIZE', ]:
if not parameter in pelican.settings.keys():
print ("webmention_static error: no " + parameter + "defined in settings")
else:
globals()[parameter] = pelican.settings.get(parameter)
##globals()[parameter] = pelican.settings[parameter]
##print (globals()[parameter])
global rsvpIcon
rsvpIcon = {
'yes': '✅',
'no': '❌',
'interested': '💡',
'maybe': '❔',
}
if WEBMENTION_IO_OVERWRITE_INITIAL_CACHE:
overwrite_initial_cache ()
if WEBMENTION_IO_UPDATE_INITIAL_CACHE:
update_initial_cache ()
def overwrite_initial_cache ():
try:
##https://webmention.io/api/mentions.jf2?target=
# FIXME, if not specify per-page, it only get a few recent records for a domain
# Here, hardcoded to a large value but will have problem if the no of items get past this value
response = urllib.request.urlopen(WEBMENTION_IO_JF2_URL
+ "?domain=" + WEBMENTION_IO_DOMAIN
+ "&token=" + WEBMENTION_IO_API_KEY
+ "&per-page=9999")
data = response.read().decode("utf-8")
##data = response.get()
file = open(WEBMENTION_IO_CACHE_FILENAME, "w+")
file.write(data)
file.close()
except:
raise
def update_initial_cache ():
try:
file = open(WEBMENTION_IO_CACHE_FILENAME, "r")
#cached_json = json.load(file, object_pairs_hook=OrderedDict)
cached_json = json.load(file)
file.close()
except:
raise
if cached_json is None:
cached_json = {}
try:
##https://webmention.io/api/mentions.jf2?target=
# FIXME, if not specify per-page, it only get a few recent records for a domain
# Here, hardcoded to a large value but will have problem if the no of items get past this value
response = urllib.request.urlopen(WEBMENTION_IO_JF2_URL
+ "?domain=" + WEBMENTION_IO_DOMAIN
+ "&token=" + WEBMENTION_IO_API_KEY
+ "&per-page=9999")
data = response.read().decode("utf-8")
#incoming_json = json.loads(data, object_pairs_hook=OrderedDict)
incoming_json = json.loads(data)
except:
raise
cached_json.update(incoming_json)
#print (cached_json)
try:
file = open(WEBMENTION_IO_CACHE_FILENAME, "w+")
json.dump(cached_json, file)
file.close()
except:
raise
"""
# incomplete, not working
def updateItem(cached_items, incoming_items):
new = []
seen = set()
for x in cached_items:
tupleOfItem = tuple(x.items())
if tupleOfItem not in seen:
yield x
seen.add(tupleOfItem)
new.append (x)
"""
class Discussion(object):
def __init__(self):
self.liked = []
self.mentioned = []
self.replied = []
self.replied_paged = dict()
self.replied_num_pages = 0
self.reposted = []
self.bookmarked = []
self.followed = []
self.rsvp = []
self.unclassified = []
def setup_webmentions(generator, metadata):
metadata['webmentions'] = Discussion()
def fetch_webmentions(generator, content):
if not WEBMENTION_IO_JF2_URL: return
print ("Fetching webmentions from URL:", WEBMENTION_IO_JF2_URL)
## Get all links "mentioning" this url
target_url = article_url(content)
print ("Fetching webmentions for local article:", target_url)
try:
file = open(WEBMENTION_IO_CACHE_FILENAME, "r")
j = json.load(file)
file.close()
"""
# use this section if query webmention.io directly
##https://webmention.io/api/mentions.jf2?target=
response = urllib.request.urlopen(WEBMENTION_IO_JF2_URL
+ "?per-page=" + str(WEBMENTION_IO_MAX_ITEMS)
+ "&target=" + target_url)
data = response.read().decode("utf-8")
j = json.loads(data)
"""
except:
raise
current_Item_Count = 0
##print (j['children'])
##for x in j:
#for x in j['children']:
for x in j['children']:
if ( x.get("wm-target", "") == target_url ):
if (current_Item_Count >= WEBMENTION_IO_MAX_ITEMS) : break
wm = {
"wm-id": x.get("wm-id", ""),
"wm-property": x.get("wm-property", ""),
"published": x.get("published", ""),
"wm-received": x.get("wm-received", ""),
"name": x.get("name", "Untitled"),
"summary": x.get("summary", ""),
"text": x.get("content", {"text": ""}).get("text", ""),
"author_name": x.get("author", {"name": "An unnamed person"}).get("name", "An unnamed person"),
"author_photo": x.get("author", {"photo": "https://www.gravatar.com/avatar/no?d=mm"}).get("photo", "https://www.gravatar.com/avatar/no?d=mm"),
"author_url": x.get("author", {"url": ""}).get("url", ""),
"wm-source": x.get("wm-source", ""),
"wm-target": x.get("wm-target", ""),
"url": x.get("url", ""),
"rsvp": x.get("rsvp", ""),
}
if wm["author_photo"] is None:
wm["author_photo"] = "https://www.gravatar.com/avatar/no?d=mm"
if wm["url"]:
##wm["parsed_url"] = urlparse.urlparse(wm["url"])
wm["parsed_url"] = urlparse(wm["url"])
else:
wm["parsed_url"] = None
if wm["published"] is None:
wm["published"] = wm["wm-received"]
print (wm["wm-property"], wm["name"], wm["author_name"], wm["author_url"], wm["wm-source"] , wm["published"])
comment = {
'wm-id': wm["wm-id"],
'wm-property': wm["wm-property"],
'published': wm["published"],
'wm-received': wm["wm-received"],
'name': wm["name"],
'summary': wm["summary"],
'text': wm["text"],
'author_name': wm["author_name"],
'author_photo': wm["author_photo"],
'author_url': wm["author_url"],
'wm-source': wm["wm-source"],
'wm-target': wm["wm-target"],
'url': wm["url"],
'rsvp': wm["rsvp"],
}
if wm["wm-property"] == 'like-of':
comment["reaction"] = 'liked'
comment["icon"] = '❤️'
content.webmentions.liked.append(comment)
elif wm["wm-property"] == 'mention-of':
comment["reaction"] = 'mentioned'
comment["icon"] = '💬'
content.webmentions.mentioned.append(comment)
elif wm["wm-property"] == 'in-reply-to':
comment["reaction"] = 'replied'
comment["icon"] = '📩'
content.webmentions.replied.append(comment)
elif wm["wm-property"] == 'repost-of':
comment["reaction"] = 'reposted'
comment["icon"] = '🔁'
content.webmentions.reposted.append(comment)
elif wm["wm-property"] == 'bookmark-of':
comment["reaction"] = 'bookmarked'
comment["icon"] = '⭐️'
content.webmentions.bookmarked.append(comment)
elif wm["wm-property"] == 'followed-of':
comment["reaction"] = 'followed'
comment["icon"] = '👣'
content.webmentions.followed.append(comment)
elif wm["wm-property"] == 'rsvp':
comment["reaction"] = wm["rsvp"]
comment["icon"] = rsvpIcon[wm["rsvp"]]
content.webmentions.rsvp.append(comment)
else:
print(f'Unrecognized comment type: {wm["wm-property"]}')
comment["reaction"] = 'unclassified'
comment["icon"] = '❔'
content.webmentions.unclassified.append(comment)
current_Item_Count += 1
#def final_update(generator, content):
if content.webmentions.replied and WEBMENTION_IO_REPLIED_PAGINATION_SIZE > 0:
# Get all attributes from the generator that are articles or pages
#posts = [
# getattr(generator, attr, None) for attr in PROCESS
# if getattr(generator, attr, None) is not None]
page_size = WEBMENTION_IO_REPLIED_PAGINATION_SIZE
paginator = django.core.paginator.Paginator(content.webmentions.replied, page_size)
content.webmentions.replied_num_pages = paginator.num_pages
print ("DEBUG, paginator count : ", paginator.count)
page_no = 0
current_item_no = 0
for i in range (paginator.num_pages):
# passing page_no, starting with 1 instead of 0
page_no = i + 1
for j in range (page_size):
if page_no * (j+1) > paginator.count : break
## if use pop, the original content.webmentions.replied list would be empty
##content.webmentions.replied_paged.setdefault(page_no, []).append(content.webmentions.replied.pop())
content.webmentions.replied_paged.setdefault(page_no, []).append(content.webmentions.replied[current_item_no])
current_item_no += 1
##content.webmentions.replied_paged.append([content.webmentions.replied[0]])
paged_json_file_path = os.path.join(generator.output_path, "wm-replied-"+str(page_no)+".json" )
#print ("DEBUG2: ", paged_json_file_path)
try:
# Get the full path to the original source file
source_out = os.path.join(
content.settings['OUTPUT_PATH'], content.save_as
)
# Get the path to the original source file
source_out_path = os.path.split(source_out)[0]
# Create 'copy to' destination for writing later
paged_json_file_path = os.path.join(
source_out_path, "wm-replied-"+str(page_no)+".json"
)
os.makedirs(source_out_path, 0o775, exist_ok=True)
file = open(paged_json_file_path, "w+")
json.dump(content.webmentions.replied_paged.setdefault(page_no, []), file)
file.close()
except:
raise
## start with 1 (page no)
##print ("DEBUG3: ", content.webmentions.replied_paged[1]);
def register():
signals.initialized.connect(initialize_module)
signals.article_generator_context.connect(setup_webmentions)
signals.article_generator_write_article.connect(fetch_webmentions)
##signals.article_writer_finalized.connect(final_update)
##signals.article_generator_finalized.connect(final_update)