~dwt/cleardarksky/0.2

« back to all changes in this revision

Viewing changes to cleardarksky/__init__.py

  • Committer: David Turner
  • Date: 2012-05-24 06:06:36 UTC
  • Revision ID: finalfantasykid13@gmail.com-20120524060636-7fb8n433dsm9wus1
-Added a single category for searching clear dark sky

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import logging
2
 
import optparse
3
 
 
4
1
import urllib2
5
 
import simplejson
6
 
 
7
 
import gettext
8
 
from gettext import gettext as _
9
 
gettext.textdomain('cleardarksky')
10
 
 
 
2
from time import gmtime, strftime, time
11
3
from singlet.lens import SingleScopeLens, IconViewCategory, ListViewCategory
12
 
 
13
4
from cleardarksky import cleardarkskyconfig
14
5
 
15
6
class CleardarkskyLens(SingleScopeLens):
16
7
 
 
8
    cache = list()
 
9
    lastPull = 0
 
10
 
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'
26
 
        search_on_blank=True
 
20
        search_on_blank=False
27
21
 
28
 
    # TODO: Add your categories
29
 
    locations_category = ListViewCategory("Locations", "dialog-information-symbolic")
 
22
    locations_category = ListViewCategory("Locations", "weather-clear-night-symbolic")
30
23
 
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,
36
 
                        "text/html",
37
 
                        article[2] + ", " + article[1],
38
 
                        "Location",
39
 
                        "http://cleardarksky.com/c/%skey.html" % (article[0]))
 
25
        i = 0
 
26
        for article in self.sky_query(search):
 
27
            if(i == 100):
 
28
                #only show the top 100
 
29
                break
 
30
            results.append("http://cleardarksky.com/c/" + article[0] + "key.html",
 
31
                           "weather-clear-night-symbolic",
 
32
                           self.locations_category,
 
33
                           "text/html",
 
34
                           article[2] + ", " + article[1],
 
35
                           "Location",
 
36
                           "http://cleardarksky.com/c/" + article[0] + "key.html")
 
37
            i += 1
40
38
        pass
41
39
        
42
 
    def wikipedia_query(self, search):
 
40
    def fetch(self):
 
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"))
 
51
        
 
52
    def sky_query(self, search):
43
53
        try:
44
54
            results = list()
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
52
 
                        if(pos1 < 0):
53
 
                            pos1 = 100
54
 
                        if(pos2 < 0):
55
 
                            pos2 = 100
56
 
                        distance = pos1*2 + pos2
57
 
                        print distance
58
 
                        row = (splitted[0], splitted[1], splitted[2], distance)
59
 
                        
 
55
            self.fetch()
 
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())
 
62
                    if(pos >= 0):
 
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])
63
66
            return results
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"
66
69
            return []
 
70