-
Notifications
You must be signed in to change notification settings - Fork 3
/
astrobot.py
executable file
·549 lines (444 loc) · 19 KB
/
astrobot.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#!/usr/bin/env python
# encoding=utf8
# -*- coding: utf-8 -*-
import client # Astrometry.net API
import praw # Reddit API
import pyimgur # Imgur API
import math
import time
import io
import sys
import subprocess
import argparse
import urlparse
from string import Template
from collections import deque
import logging
import urllib2
import requests
from lxml import etree
from PIL import Image
import ssl
import credentials
NEW_POSTS = 100 # number of new posts to go through
MAX_SKIPPED = 1000 # remember only last n skipped posts
MAX_TAGS = 8 # when there's more than n tags, filter out the stars
REST_TIME = 180 # time to rest between each loop
ERROR_TIME = 60 # time to rest on API error
TTL = 10 # number of tries for every post
reload(sys)
sys.setdefaultencoding('utf8')
class AstroBot:
def __init__(self):
# Imgur API
self.imgur = pyimgur.Imgur(credentials.IMGUR_CLIENT_ID, \
client_secret=credentials.IMGUR_CLIENT_SECRET)
self.context = ssl._create_unverified_context()
authorized = False
# TODO: automatize this
while (not authorized):
sys.stdout.flush()
print "Get PIN on", credentials.IMGUR_AUTH_URL
pin = raw_input("Enter PIN: ")
try:
self.imgur.exchange_pin(pin)
authorized = True
except:
print "[ERROR]:", "Wrong PIN or other error. Authorize again."
# Astrometry API
self.astrometry = client.client.Client()
self.astrometry.login(credentials.ASTROMETRY_ID)
# Reddit API
self.praw = praw.Reddit(user_agent=credentials.USER_AGENT)
self.praw.login(credentials.REDDIT_USER, credentials.REDDIT_PASSWORD)
# set of submissions currently being solved
# TODO: consider pickle
self.solving = dict()
# queue of skipped reddit ids
self.skipped = deque(maxlen=MAX_SKIPPED)
# blacklist of words on /r/astrophotography+apod
self.blacklist = ["moon", "lunar", "sun", "solar", "eclipse",\
"mercury", "venus", "mars", "jupiter", "saturn", "uranus",\
"neptune", "trails", "panorama"]
# whitelist of words on /r/astronomy+space+spaceporn
self.whitelist = ["galaxy", "ngc", "comet", "nebula", "constellation",\
"iss", "ison", "sky", "skies"]
# logging the solved posts
self.logger = logging.getLogger("astrobot")
hdlr = logging.FileHandler("solved.log")
formatter = logging.Formatter('%(message)s')
hdlr.setFormatter(formatter)
self.logger.addHandler(hdlr)
self.logger.setLevel(logging.INFO)
def run(self):
"""
The main loop.
"""
while True:
try:
self.refresh()
self.read_inbox()
self.process_new()
self.check_for_solved()
print "[INFO]:", "Sleeping for %d minute(s)." % (REST_TIME / 60)
time.sleep(REST_TIME)
except (praw.errors.APIException, requests.exceptions.HTTPError, urllib2.HTTPError) as e:
print "[WARN]:", "API error. Sleeping for %d minute(s)." % (ERROR_TIME / 60)
print "[WARN]:", e
time.sleep(ERROR_TIME)
except (KeyboardInterrupt, EOFError), e:
print "\n(quit)"
return -1
except Exception as e:
print "[WARN]:", "Sleeping for %d minute(s)." % (ERROR_TIME / 60)
print "[WARN]:", e
time.sleep(ERROR_TIME)
def refresh(self):
"""
Refresh the imgur access token.
"""
self.imgur.refresh_access_token()
def process_new(self):
"""
Find posts to process and send them to nova.Astrometry.net
"""
subreddits = self.praw.get_subreddit("astrophotography+astronomy+space+spaceporn+apod")
# get last 100 posts
for post in subreddits.get_new(limit=NEW_POSTS):
if self._check_condition(post):
if not self._send_for_solution(post):
self.skipped.append(post.id)
else:
self.skipped.append(post.id)
# get hidden posts
for post in self.praw.user.get_hidden():
post.unhide()
if self._check_condition(post, force=True):
if not self._send_for_solution(post):
self.skipped.append(post.id)
else:
self.skipped.append(post.id)
def check_for_solved(self):
"""
Poll for the status of every solution
and process successful ones.
"""
for subid in list(self.solving):
metadata = self.solving[subid]
result = self.astrometry.send_request("submissions/%d" % subid)
if len(result["job_calibrations"]) != 0:
metadata["job_id"] = result["job_calibrations"][0][0]
metadata["image_id"] = result["user_images"][0]
self._post_solved(metadata)
self.skipped.append(metadata["post"].id)
del(self.solving[subid])
continue
metadata["TTL"] -= 1
if metadata["TTL"] < 1:
print "[WARN]:", "Failed to solve the post in time."
self.skipped.append(metadata["post"].id)
del(self.solving[subid])
def read_inbox(self):
"""
Read the inbox for deletion messages.
"""
message_ok = Template("The automatic comment for "
"[your submission]($permalink) "
"was removed.\n\nIf you want to disable the bot "
"for all your future submissions, send me a PM! "
"\n\nSorry for inconvenience.")
message_not_ok = ("It seems you don't have right to remove the comment."
"If you believe you do, send me a PM!")
for msg in self.praw.get_inbox():
if "delete" in msg.subject and msg.new:
print "[INFO]:", "Deletion request was received."
remove_id = msg.body
deleted = False
me = self.praw.get_redditor(credentials.REDDIT_USER)
for c in me.get_comments(limit=None):
if c.id == remove_id and msg.author == c.submission.author:
c.delete()
msg.mark_as_read()
deleted = True
break
if deleted:
print "[INFO]:", "Deletion successful."
self.praw.send_message(msg.author, 'Comment removed',
message_ok.safe_substitute({"permalink": c.submission.permalink}))
else:
self.praw.send_message(msg.author, 'Error while processing request',
message_not_ok)
msg.mark_as_read()
# --- helper methods
def _check_condition(self, post, force=False):
"""
Decide whether to process the reddit post.
"""
if not force and post.id in self.skipped:
return False
if post.saved:
return False
if post.author and post.author.name.lower() == "eorequis":
return False
blacklist_matches = sum(word in post.title.lower() for word in self.blacklist)
whitelist_matches = sum(word in post.title.lower() for word in self.whitelist)
if post.subreddit.display_name.lower() in ["astrophotography", "apod"]:
if not force and blacklist_matches == 1 and whitelist_matches == 0:
return False
else:
if not force and whitelist_matches == 0:
return False
# TODO: fix 'load more comments'
post.replace_more_comments(limit=16, threshold=1)
for comment in praw.helpers.flatten_tree(post.comments):
if isinstance(comment, praw.objects.Comment) and \
"astrometry.net" in comment.body.lower():
return False
try:
if (self._parse_url(post.url) is None):
return False
except urllib2.HTTPError as e:
print "[INFO]:", "Location can't be opened."
return False
return True
def _send_for_solution(self, post):
"""
Process the reddit post and send
to nova.Astrometry.net.
"""
image_url = self._parse_url(post.url)
# get resolution of photo (used for computing range)
fd = urllib2.urlopen(image_url, context=self.context)
try:
image_file = io.BytesIO(fd.read())
im = Image.open(image_file)
except IOError:
return False
# upload
print "[INFO]:", "Sending post", post.permalink, "to nova.Astrometry.net"
subid = self._upload(image_url)
metadata = dict()
metadata["id"] = subid
metadata["post"] = post
metadata["image_size"] = im.size
metadata["TTL"] = TTL
if subid not in self.solving:
self.solving[subid] = metadata
return False
return True
def _post_solved(self, metadata):
"""
Post results of solved submission to the
comment section of the reddit post.
"""
post = metadata["post"]
# calibration
(ra, de, radius, rg) = self._get_calibration(metadata["job_id"], metadata["image_size"])
metadata["rectascension"] = ra
metadata["declination"] = de
metadata["range"] = rg
metadata["radius"] = radius
# annotated image
metadata["author"] = ""
if ("astrophotography" in post.subreddit.display_name.lower() and post.author):
metadata["author"] = post.author.name
metadata["annotated_image"] = self._upload_annotated(metadata["job_id"], metadata["author"])
# tags
metadata["tags"] = self._get_tags(metadata["job_id"])
if (metadata["annotated_image"] is not None):
comment = self._generate_comment(metadata)
c = post.add_comment(comment)
time.sleep(4)
c.edit(comment.replace('____id____', str(c.id)))
post.upvote() # can I do that?
post.save()
self.logger.info("%s:%s" % (str(metadata["id"]), post.id))
print "[INFO]:", "Post", post.permalink, "successfully solved."
def _parse_url(self, rawUrl):
"""
Get direct image URL for web pages.
"""
url = urlparse.urlparse(rawUrl)
# check whether the url is accessible
req = urllib2.Request(rawUrl, headers={'User-Agent' : credentials.USER_AGENT})
fd = urllib2.urlopen(req, context=self.context)
p = url.path.lower()
if p.endswith(".jpg") or p.endswith(".jpeg") or p.endswith(".png") or p.endswith(".gif"):
return rawUrl
# get direct url from imgur (skip sets and albums)
if "imgur.com" in url.netloc and "a/" not in url.path and ("," not in url.path) and ("gifv" not in url.path):
newloc = "i." + url.netloc
newpath = url.path
if newpath.endswith("/new"):
newpath.replace("/new", "")
newpath += ".jpg"
newpath = newpath.replace("gallery/", "")
newUrl = urlparse.ParseResult(url.scheme, newloc, newpath,
url.params, url.query, url.fragment)
return newUrl.geturl()
# get direct url from flickr
if "flickr.com" in url.netloc:
path = filter(lambda x: x != '', url.path.split('/'))
if path[0] == 'photos' and len(path) >= 3:
newpath = '/photos/%s/%s/sizes/l' % (path[1], path[2])
newUrl = urlparse.ParseResult(url.scheme, url.netloc, newpath,
url.params, url.query, url.fragment)
try:
file = urllib2.urlopen(newUrl.geturl(), context=self.context)
tree = etree.HTML(file.read())
staticUrl = tree.xpath('//div[@id="allsizes-photo"]/img/@src')
if len(staticUrl):
return staticUrl[0]
except:
pass
if "apod.nasa.gov" in url.netloc:
try:
file = urllib2.urlopen(url.geturl(), context=self.context)
tree = etree.HTML(file.read())
directUrl = tree.xpath('//img/@src')
if len(directUrl):
return "http://apod.nasa.gov/apod/" + directUrl[0]
except:
pass
if "wikipedia.org" in url.netloc and "File:" in url.path:
try:
file = urllib2.urlopen(url.geturl(), context=self.context)
tree = etree.HTML(file.read())
directUrl = tree.xpath('//div[@class="fullMedia"]/a/@href')[0]
if len(directUrl):
return "http:" + directUrl
except:
pass
return None
def _upload(self, image_url):
"""
Upload the image to Astrometry and return submission id.
"""
kwargs = dict(
allow_commercial_use="n",
allow_modifications="n",
publicly_visible="y")
#TODO: downsample if image too big...
result = self.astrometry.url_upload(image_url, **kwargs)
stat = result['status']
if stat != 'success':
print "[WARN]:", "Upload failed: status", stat
return
return result['subid']
# TODO: rewrite to pure python solution
def _upload_annotated(self, job_id, author):
"""
Get annotated image from astrometry, put label on it and upload to Imgur.
"""
subprocess.check_call(["./annotate.sh", str(job_id), author])
self.imgur.refresh_access_token()
try:
uploaded_image = self.imgur.upload_image(path=str(job_id) + ".png",\
album=credentials.ALBUM_ID)
return uploaded_image.link
except:
print "[WARN]:", "Imgur error. Image not uploaded."
return None
def _get_tags(self, job_id):
"""
Get the resolved objects.
"""
tags = self.astrometry.send_request('jobs/%s/tags' % job_id, {})["tags"]
# if there are too many tags, filter out stars
if (len(tags) > MAX_TAGS):
tags = filter(lambda x: x.find("star") == -1, tags)
return tags
def _get_calibration(self, job_id, image_size):
"""
Get calibration of solved job and parse it to
r. ascension, declination, radius and range.
"""
calibration = self.astrometry.send_request('jobs/%s/calibration' % str(job_id))
ra = calibration['ra']
de = calibration['dec']
radius = calibration['radius']
max_span = max(image_size)
angular_scale = calibration['pixscale'] * max_span / 3600.0
TINY_FLOAT_VALUE = 1.0e-8
RADIUS_EARTH = 6378135.0 # in meters
VIEWABLE_ANGULAR_SCALE = 50.0 # in degrees
alpha = 0.5 * VIEWABLE_ANGULAR_SCALE * (math.pi / 180.0)
beta = 0.5 * angular_scale * (math.pi / 180.0)
if (beta > alpha):
beta = alpha
rg = RADIUS_EARTH * (1.0 - (math.sin(alpha - beta) /\
(math.sin(alpha) + TINY_FLOAT_VALUE)))
return (float(ra), float(de), float(radius), float(rg))
def _hours_to_real(self, hours, minutes, seconds):
return hours + minutes / 60.0 + seconds / 3600.0
def _real_to_hours(self, real):
hours = int(real)
n = real - hours
if (n < 0):
n = -n
minutes = int(math.floor(n * 60))
n = n - minutes / 60.0
seconds = n * 3600
return (hours, minutes, seconds)
def _wikisky_link(self, metadata):
link = "http://server4.wikisky.org/v2"
link += "?ra=" + str(metadata["rectascension"] / 15.0)
link += "&de=" + str(metadata["declination"])
zoom = 18 - round(math.log(metadata["range"] / 90.0) / math.log(2))
link += "&zoom=" + str(int(zoom))
link += "&show_grid=1&show_constellation_lines=1"
link += "&show_constellation_boundaries=1&show_const_names=1"
link += "&show_galaxies=1&img_source=SKY-MAP"
return link
def _googlesky_link(self, metadata):
link = "http://www.google.com/sky/"
link += "#longitude=" + str(metadata["rectascension"] - 180)
link += "&latitude=" + str(metadata["declination"])
zoom = 20 - round(math.log(metadata["range"] / 90.0) / math.log(2))
link += "&zoom=" + str(int(zoom))
return link
def _generate_comment(self, metadata):
"""
Construct the comment for reddit.
"""
comment = ("*This is an automatically generated comment.*\n\n"
"---\n\n"
"$coordinates"
"$radius"
"$image"
"$tags"
"$links"
"---\n\n"
"$advertise"
"^Powered ^by [^Astrometry.net]("
"http://nova.astrometry.net/user_images/$image_id)"
" ^| [^Feedback]("
"http://www.reddit.com/message/compose?to=astro-bot)"
" ^| [^FAQ]("
"http://github.com/RobSis/astrobot/blob/master/FAQ.md)"
" ^| ^1) ^Tags ^may ^overlap"
" ^| ^OP ^can [^delete]("
"http://www.reddit.com/message/compose?to=astro-bot&subject=delete&message=____id____) ^this ^comment."
)
# data model for the template
model = dict()
model["advertise"] = ""
if (metadata["post"].subreddit.display_name.lower() != "astrophotography"):
model["advertise"] = "*If this is your photo, consider x-posting to /r/astrophotography!*\n\n"
model["coordinates"] = "> Coordinates: "
model["coordinates"] += "%d^h %d^m %.2f^s , " % self._real_to_hours(metadata["rectascension"] / 15.0)
model["coordinates"] += "%d° %d' %.2f\"\n\n" % self._real_to_hours(metadata["declination"])
model["radius"] = "> Radius: %.3f deg\n\n" % metadata["radius"]
model["image"] = "> Annotated image: [%s](%s)\n\n" % (metadata["annotated_image"], metadata["annotated_image"])
if (len(metadata["tags"]) > 0):
model["tags"] = "> Tags^1: *" + ", ".join(metadata["tags"]) + "*\n\n"
else:
model["tags"] = ""
model["links"] = "> Links: "
model["links"] += "[Google Sky](%s) | " % self._googlesky_link(metadata)
model["links"] += "[WIKISKY.ORG](%s)\n\n" % self._wikisky_link(metadata)
model["image_id"] = metadata["image_id"]
return Template(comment).safe_substitute(model)
if __name__ == '__main__':
bot = AstroBot()
sys.exit(bot.run())