-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdisqus.py
More file actions
250 lines (211 loc) · 10.7 KB
/
disqus.py
File metadata and controls
250 lines (211 loc) · 10.7 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from codebender_testing.config import get_path
import disqusapi
import simplejson
import base64
import hashlib
import hmac
import time
import os
import re
FORUM = os.getenv('DISQUS_FORUM', 'codebender-cc')
AUTHOR_URL = os.getenv('AUTHOR_URL', 'https://codebender.cc/user/codebender')
DISQUS_REQUESTS_PER_HOUR = 1000
DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60) / 60
CHANGE_LOG = 'examples_compile_log.json'
DISQUS_COMMENTS = 'disqus_comments.json'
EXAMPLES_WITHOUT_LIBRARY_DB = 'examples_without_library.json'
class DisqusWrapper:
def __init__(self, log_time):
self.log_time = log_time
self.DISQUS_API_SECRET = os.getenv('DISQUS_API_SECRET', None)
self.DISQUS_API_PUBLIC = os.getenv('DISQUS_API_PUBLIC', None)
self.DISQUS_ACCESS_TOKEN = os.getenv('DISQUS_ACCESS_TOKEN', None)
self.user = {
'id': os.getenv('DISQUS_SSO_ID', None),
'username': os.getenv('DISQUS_SSO_USERNAME', None),
'email': os.getenv('DISQUS_SSO_EMAIL', None),
}
self.SSO_KEY = self.get_disqus_sso(self.user)
self.disqus = disqusapi.DisqusAPI(api_secret=self.DISQUS_API_SECRET,
public_key=self.DISQUS_API_PUBLIC,
remote_auth=self.SSO_KEY)
self.change_log = {}
self.last_post = None
self.last_library = None
with open(get_path('data', DISQUS_COMMENTS)) as f:
self.messages = simplejson.loads(f.read())
with open(get_path('data', EXAMPLES_WITHOUT_LIBRARY_DB)) as f:
self.examples_without_library = simplejson.loads(f.read())
def get_disqus_sso(self, user):
# create a JSON packet of our data attributes
data = simplejson.dumps(user)
# encode the data to base64
message = base64.b64encode(data)
# generate a timestamp for signing the message
timestamp = int(time.time())
# generate our hmac signature
sig = hmac.HMAC(self.DISQUS_API_SECRET, '%s %s' % (message, timestamp), hashlib.sha1).hexdigest()
return "{0} {1} {2}".format(message, sig, timestamp)
def update_comment(self, sketch, results, current_date, log_entry, openFailFlag, total_sketches):
"""A comment is added to the library as soon as its first example is compiled.
`library`: The library in which belongs the currently compiled example.
`self.last_library`: The library in which belongs the previously compiled example.
`library_to_comment`: The library in which a comment should be added.
"""
library_match = re.match(r'.+\/example\/(.+)\/.+', sketch)
library = None
library_to_comment = None
# Set the library in which belongs the currently compiled example.
if library_match:
library = library_match.group(1)
#Check if the currently compiled example belongs to the same library as the previous one.
# To do so we check if value of library is the same with self.last library value which is updated
# every time that we log the results of a compiled example.
if library != self.last_library:
library_to_comment = library
#Check if we should add a comment to the library.
if library_to_comment and library not in self.examples_without_library:
log_entry = self.handle_library_comment(library_to_comment, current_date, log_entry)
self.last_library = library
#Add a comment to the currently compiled library example.
if not openFailFlag:
log_entry = self.handle_example_comment(sketch, results, current_date, log_entry)
return log_entry
def handle_library_comment(self, library, current_date, log, examples=True):
url = '/library/' + library
identifier = 'ident:' + url
if url not in log:
log[url] = {}
try:
log[url]['comment'] = False
""" Returns a Paginator object that matches the desired criteria:
`self.disqus.api.threads.list`: Returns a list containg all urls in which Disqus loaded.
`forum`: Looks up a forum by short name.
`thread`: Looks up a thread by ID BUT you may pass us the 'ident' query type instead of
an ID by including 'forum'. Filters results returned from `self.disqus.api.threads.list`
and returns only those which match `forum` and `thread`.
IMPORTANT: If `thread` is not found, all threads are returned!!!
"""
paginator = disqusapi.Paginator(self.disqus.api.threads.list,
forum=FORUM,
thread=identifier)
if paginator:
comment_updated = False
new_message = self.messages['library'].replace('TEST_DATE', current_date)
if examples==False:
new_message = self.messages['library_no_examples'].replace('TEST_DATE', current_date)
for thread in paginator:
# Check if library has already a comment.
post_id, existing_message = self.get_posts(thread['id'])
#If library already has a comment, update it.
if post_id and existing_message:
log[url]['comment'] = self.update_post(post_id, new_message)
comment_updated = True
break
#If library doesn't have a comment, create it.
if not comment_updated:
log[url]['comment'] = self.create_post(identifier, new_message)
except Exception as error:
print 'Error:', error
log[url]['comment'] = False
return log
def handle_example_comment(self, url, results, current_date, log):
identifier = re.sub(r'https*://.*codebender.cc', '', url)
identifier = 'ident:' + identifier
try:
log[url]['comment'] = False
paginator = disqusapi.Paginator(self.disqus.api.threads.list,
forum=FORUM,
thread=identifier, method='GET')
if paginator:
comment_updated = False
boards = []
unsupportedFlag = False
for result in results:
if result['status'] == 'success':
board = result['board']
if re.match(r'Arduino Mega.+', board):
board = 'Arduino Mega'
boards.append(board)
elif result['status'] == 'unsupported':
unsupportedFlag = True
new_message = self.messages['example_fail'].replace('TEST_DATE', current_date)
if len(boards) > 0:
new_message = self.messages['example_success'].replace('TEST_DATE', current_date).replace('BOARDS_LIST', ', '.join(boards))
elif unsupportedFlag:
new_message = self.messages['example_unsupported'].replace('TEST_DATE', current_date)
for page in paginator:
post_id, existing_message = self.get_posts(page['id'])
if post_id and existing_message:
log[url]['comment'] = self.update_post(post_id, new_message)
comment_updated = True
break
if not comment_updated:
log[url]['comment'] = self.create_post(identifier, new_message)
except Exception as error:
print 'Error:', error
log[url]['comment'] = False
return log
def get_posts(self, thread_id):
post_id = None
raw_message = None
try:
""" Returns a Paginator object that matches the desired criteria:
`self.disqus.api.posts.list`: Returns a list of posts ordered by the date created.
`forum`: Looks up a forum by short name.
`thread`: Looks up a thread by ID. Filters results returned from `self.disqus.api.posts.list`
and returns only those which match `forum` and `thread`.
"""
paginator = disqusapi.Paginator(self.disqus.api.posts.list,
forum=FORUM,
thread=thread_id,
order='asc')
if paginator:
for post in paginator:
if post['author']['name'] == self.user['username'] and post['author']['url'] == AUTHOR_URL:
post_id = post['id']
raw_message = post['raw_message']
break
except Exception as error:
print 'Error:', error
return post_id, raw_message
def create_post(self, thread_id, message):
if not self.last_post:
self.last_post = message
elif re.match(r'^.+\.$', self.last_post):
message = message[:-1]
comment_status = False
try:
response = self.disqus.threads.list(api_secret=self.DISQUS_API_SECRET,
forum=FORUM,
thread=thread_id, method='GET')
response = self.disqus.posts.create(api_secret=self.DISQUS_API_SECRET,
remote_auth=self.SSO_KEY,
thread=response[0]['id'],
message=message, method='POST')
if response['raw_message'] == message:
self.last_post = message
comment_status = True
except Exception as error:
print 'Error:', error
return comment_status
def update_post(self, post_id, message):
if not self.last_post:
self.last_post = message
elif re.match(r'^.+\.$', self.last_post):
message = message[:-1]
comment_status = False
try:
response = self.disqus.posts.update(api_secret=self.DISQUS_API_SECRET,
access_token=self.DISQUS_ACCESS_TOKEN,
remote_auth=self.SSO_KEY,
post=post_id,
message=message, method='POST')
if response['raw_message'] == message:
self.last_post = message
comment_status = True
except Exception as error:
print 'Error:', error
return comment_status