13
13
14
14
15
15
FORUM = 'codebender-cc'
16
- AUTHOR_NAME = 'codebender'
17
16
AUTHOR_URL = 'https://codebender.cc/user/codebender'
18
17
DISQUS_REQUESTS_PER_HOUR = 1000
19
18
DISQUS_WAIT = (DISQUS_REQUESTS_PER_HOUR / 60 ) / 60
@@ -34,7 +33,9 @@ def __init__(self, log_time):
34
33
'email' : os .getenv ('DISQUS_SSO_EMAIL' , None ),
35
34
}
36
35
self .SSO_KEY = self .get_disqus_sso (self .user )
37
- self .disqus = disqusapi .DisqusAPI (api_secret = self .DISQUS_API_SECRET , public_key = self .DISQUS_API_PUBLIC , remote_auth = self .SSO_KEY )
36
+ self .disqus = disqusapi .DisqusAPI (api_secret = self .DISQUS_API_SECRET ,
37
+ public_key = self .DISQUS_API_PUBLIC ,
38
+ remote_auth = self .SSO_KEY )
38
39
self .change_log = {}
39
40
self .last_post = None
40
41
self .last_library = None
@@ -61,16 +62,23 @@ def update_comment(self, sketch, results, current_date, log_entry, openFailFlag,
61
62
if not openFailFlag :
62
63
log_entry = self .handle_example_comment (sketch , results , current_date , log_entry )
63
64
64
- # Comment libraries when finished with the examples
65
+ # Comment libraries when finished commenting the examples
65
66
library_match = re .match (r'.+\/example\/(.+)\/.+' , sketch )
66
67
library = None
67
68
if library_match :
68
69
library = library_match .group (1 )
69
70
if not self .last_library :
70
71
self .last_library = library
71
- if library and library != self .last_library and (library not in self .examples_without_library or counter >= total_sketches - 1 ):
72
- log_entry = self .handle_library_comment (library , current_date , log_entry )
73
- self .last_library = library
72
+
73
+ library_to_comment = None
74
+ if library and library not in self .examples_without_library and library != self .last_library :
75
+ library_to_comment = self .last_library
76
+ if library and library not in self .examples_without_library and counter >= total_sketches - 1 :
77
+ library_to_comment = library
78
+ if library_to_comment :
79
+ log_entry = self .handle_library_comment (library_to_comment , current_date , log_entry )
80
+
81
+ self .last_library = library
74
82
75
83
return log_entry
76
84
@@ -80,77 +88,131 @@ def handle_library_comment(self, library, current_date, log):
80
88
if url not in log :
81
89
log [url ] = {}
82
90
try :
83
- paginator = disqusapi .Paginator (self .disqus .api .threads .list , forum = FORUM , thread = identifier , method = 'GET' )
91
+ log [url ]['comment' ] = False
92
+ paginator = disqusapi .Paginator (self .disqus .api .threads .list ,
93
+ forum = FORUM ,
94
+ thread = identifier , method = 'GET' )
84
95
if paginator :
96
+ comment_updated = False
97
+ new_message = self .messages ['library' ].replace ('TEST_DATE' , current_date )
85
98
for page in paginator :
86
- post_id , existing_message = self .get_posts (page ['id' ])
87
- if post_id and existing_message :
88
- new_message = self .messages ['library' ].replace ('TEST_DATE' , current_date )
89
- log [url ]['comment' ] = self .update_post (post_id , new_message )
90
- else :
91
- log [url ]['comment' ] = False
99
+ post_id , existing_message = self .get_posts (page ['id' ])
100
+ if post_id and existing_message :
101
+ log [url ]['comment' ] = self .update_post (post_id , new_message )
102
+ comment_updated = True
103
+ break
104
+
105
+ if not comment_updated :
106
+ log [url ]['comment' ] = self .create_post (identifier , new_message )
92
107
except Exception as error :
93
108
print 'Error:' , error
94
109
log [url ]['comment' ] = False
110
+
95
111
return log
96
112
97
113
def handle_example_comment (self , url , results , current_date , log ):
98
114
identifier = url .replace ('https://codebender.cc' , '' )
99
115
identifier = 'ident:' + identifier
100
116
try :
101
- paginator = disqusapi .Paginator (self .disqus .api .threads .list , forum = FORUM , thread = identifier , method = 'GET' )
117
+ log [url ]['comment' ] = False
118
+ paginator = disqusapi .Paginator (self .disqus .api .threads .list ,
119
+ forum = FORUM ,
120
+ thread = identifier , method = 'GET' )
102
121
if paginator :
122
+ comment_updated = False
123
+ boards = []
124
+ unsupportedFlag = False
125
+ for result in results :
126
+ if result ['status' ] == 'success' :
127
+ board = result ['board' ]
128
+ if re .match (r'Arduino Mega.+' , board ):
129
+ board = 'Arduino Mega'
130
+ boards .append (board )
131
+ elif result ['status' ] == 'unsupported' :
132
+ unsupportedFlag = True
133
+
134
+ new_message = self .messages ['example_fail' ].replace ('TEST_DATE' , current_date )
135
+ if len (boards ) > 0 :
136
+ new_message = self .messages ['example_success' ].replace ('TEST_DATE' , current_date ).replace ('BOARDS_LIST' , ', ' .join (boards ))
137
+ elif unsupportedFlag :
138
+ new_message = self .messages ['example_unsupported' ].replace ('TEST_DATE' , current_date )
139
+
103
140
for page in paginator :
104
- post_id , existing_message = self .get_posts (page ['id' ])
105
- if post_id and existing_message :
106
- boards = []
107
- unsupportedFlag = False
108
- for result in results :
109
- if result ['status' ] == 'success' :
110
- board = result ['board' ]
111
- if re .match (r'Arduino Mega.+' , board ):
112
- board = 'Arduino Mega'
113
- boards .append (board )
114
- elif result ['status' ] == 'unsupported' :
115
- unsupportedFlag = True
116
-
117
- new_message = self .messages ['example_fail' ].replace ('TEST_DATE' , current_date )
118
- if len (boards ) > 0 :
119
- new_message = self .messages ['example_success' ].replace ('TEST_DATE' , current_date ).replace ('BOARDS_LIST' , ', ' .join (boards ))
120
- elif unsupportedFlag :
121
- new_message = self .messages ['example_unsupported' ].replace ('TEST_DATE' , current_date )
122
- log [url ]['comment' ] = self .update_post (post_id , new_message )
123
- break
124
- else :
125
- log [url ]['comment' ] = False
141
+ post_id , existing_message = self .get_posts (page ['id' ])
142
+ if post_id and existing_message :
143
+ log [url ]['comment' ] = self .update_post (post_id , new_message )
144
+ comment_updated = True
145
+ break
146
+
147
+ if not comment_updated :
148
+ log [url ]['comment' ] = self .create_post (identifier , new_message )
126
149
except Exception as error :
127
150
print 'Error:' , error
128
151
log [url ]['comment' ] = False
152
+
129
153
return log
130
154
131
155
def get_posts (self , thread_id ):
132
156
post_id = None
133
157
raw_message = None
134
- paginator = disqusapi .Paginator (self .disqus .api .posts .list , forum = FORUM , thread = thread_id , order = 'asc' , method = 'GET' )
135
- if paginator :
136
- for result in paginator :
137
- if result ['author' ]['name' ] == AUTHOR_NAME and result ['author' ]['url' ] == AUTHOR_URL :
138
- post_id = result ['id' ]
139
- raw_message = result ['raw_message' ]
140
- break
158
+ try :
159
+ paginator = disqusapi .Paginator (self .disqus .api .posts .list ,
160
+ forum = FORUM ,
161
+ thread = thread_id ,
162
+ order = 'asc' , method = 'GET' )
163
+ if paginator :
164
+ for result in paginator :
165
+ if result ['author' ]['name' ] == self .user ['username' ] and result ['author' ]['url' ] == AUTHOR_URL :
166
+ post_id = result ['id' ]
167
+ raw_message = result ['raw_message' ]
168
+ break
169
+ except Exception as error :
170
+ print 'Error:' , error
171
+
141
172
return post_id , raw_message
142
173
174
+ def create_post (self , thread_id , message ):
175
+ if not self .last_post :
176
+ self .last_post = message
177
+ elif re .match (r'^.+\.$' , self .last_post ):
178
+ message = message [:- 1 ]
179
+ self .last_post = message
180
+
181
+ comment_status = False
182
+
183
+ try :
184
+ response = self .disqus .threads .list (api_secret = self .DISQUS_API_SECRET ,
185
+ forum = FORUM ,
186
+ thread = thread_id , method = 'GET' )
187
+ response = self .disqus .posts .create (api_secret = self .DISQUS_API_SECRET ,
188
+ remote_auth = self .SSO_KEY ,
189
+ thread = response [0 ]['id' ],
190
+ message = message , method = 'POST' )
191
+ if response ['raw_message' ] == message :
192
+ comment_status = True
193
+ except Exception as error :
194
+ print 'Error:' , error
195
+
196
+ return comment_status
197
+
143
198
def update_post (self , post_id , message ):
144
199
if not self .last_post :
145
200
self .last_post = message
146
201
elif re .match (r'^.+\.$' , self .last_post ):
147
202
message = message [:- 1 ]
148
203
self .last_post = message
204
+
205
+ comment_status = False
206
+
149
207
try :
150
- response = self .disqus .posts .update (api_secret = self .DISQUS_API_SECRET , api_key = self .DISQUS_API_PUBLIC , remote_auth = self .SSO_KEY , access_token = self .DISQUS_ACCESS_TOKEN , post = post_id , message = message , method = 'POST' )
208
+ response = self .disqus .posts .update (api_secret = self .DISQUS_API_SECRET ,
209
+ access_token = self .DISQUS_ACCESS_TOKEN ,
210
+ remote_auth = self .SSO_KEY ,
211
+ post = post_id ,
212
+ message = message , method = 'POST' )
151
213
if response ['raw_message' ] == message :
152
- return True
153
- return False
214
+ comment_status = True
154
215
except Exception as error :
155
216
print 'Error:' , error
156
- return False
217
+
218
+ return comment_status
0 commit comments