8
from gettext import gettext as _
9
gettext.textdomain('cleardarksky')
2
from time import gmtime, strftime, time
11
3
from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory
13
4
from cleardarksky import cleardarkskyconfig
15
6
class CleardarkskyLens(SingleScopeLens):
17
11
keys = "http://cleardarksky.com//t/chart_keys00.txt"
18
12
prop = "http://cleardarksky.com//t/chart_prop00.txt"
19
13
wiki = "http://en.wikipedia.org"
23
17
description = 'Cleardarksky Lens'
24
18
search_hint = 'Search Clear Dark Sky'
25
19
icon = 'cleardarksky.svg'
28
# TODO: Add your categories
29
locations_category = ListViewCategory("Locations", "dialog-information-symbolic")
22
locations_category = ListViewCategory("Locations", "weather-clear-night-symbolic")
31
24
def search(self, search, results):
32
for article in self.wikipedia_query(search):
33
results.append("http://cleardarksky.com/c/%skey.html" % (article[0]),
34
"http://icons-search.com/img/yellowicon/firefox_win.zip/Firefox_Thunderbird_Win-icons-Firefox.ico-64x64.png",
35
self.locations_category,
37
article[2] + ", " + article[1],
39
"http://cleardarksky.com/c/%skey.html" % (article[0]))
26
for article in self.sky_query(search):
28
#only show the top 100
30
results.append("http://cleardarksky.com/c/" + article[0] + "key.html",
31
"weather-clear-night-symbolic",
32
self.locations_category,
34
article[2] + ", " + article[1],
36
"http://cleardarksky.com/c/" + article[0] + "key.html")
42
def wikipedia_query(self, search):
41
# According to cleardarksky.com, the list is generated around 3:00AM EST
42
# I'm giving the server 1 hour to compute the new location data. That should be more than
43
# enough time to be sure that whatever is on the server is the latest
44
t = gmtime(time() - 60*60*5)
45
hour = str(strftime("%H", t))
46
newPull = int(strftime("%Y", t))*365 + int(strftime("%j", t))
47
if(hour >= 4 and newPull > self.lastPull):
48
self.lastPull = newPull
49
string = unicode(urllib2.urlopen(self.prop).read(), errors='ignore')
50
self.cache = list(string.split("\n"))
52
def sky_query(self, search):
45
string = urllib2.urlopen(self.prop).read()
46
for line in string.split("\n"):
47
if search.lower() in line.lower():
48
splitted = list(line.split('|'))
49
if(len(splitted) == 3):
50
pos1 = splitted[1].lower().find(search.lower()) # Province/State
51
pos2 = splitted[2].lower().find(search.lower()) # City
56
distance = pos1*2 + pos2
58
row = (splitted[0], splitted[1], splitted[2], distance)
56
for line in self.cache:
57
splitted = list(line.split('|'))
58
if(len(splitted) == 3):
59
needle = search.lower()
60
haystack = str(splitted[2].lower() + ", " + splitted[1].lower())
61
pos = haystack.find(search.lower())
63
row = (splitted[0], splitted[1], splitted[2], pos)
60
64
results.append(row)
61
print "Searching Clear Dark Sky"
62
print results.sort(lambda x,y: x[3] - y[3])
65
results.sort(lambda x,y: x[3] - y[3])
64
except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError, simplejson.JSONDecodeError):
65
print "Error : Unable to search Wikipedia"
67
except (IOError, KeyError, urllib2.URLError, urllib2.HTTPError):
68
print "Error : Unable to search Clear Dark Sky"