Mercurial > hg > config
comparison python/smartopen.py @ 0:f3ab51c79813
adding configuration from https://svn.openplans.org/svn/config_jhammel/
| author | k0s <k0scist@gmail.com> |
|---|---|
| date | Thu, 15 Oct 2009 11:41:26 -0400 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f3ab51c79813 |
|---|---|
| 1 #!/usr/bin/python | |
| 2 | |
| 3 """ smart open the data passed in """ | |
| 4 | |
| 5 import urllib | |
| 6 import urllib2 | |
| 7 import sys | |
| 8 import os | |
| 9 import address | |
| 10 | |
| 11 class Location: | |
| 12 """ | |
| 13 generic class for locations | |
| 14 """ | |
| 15 | |
| 16 def __init__(self, baseurl=""): | |
| 17 self.baseurl = baseurl | |
| 18 | |
| 19 def url(self, query): | |
| 20 return self.baseurl + self.process(query) | |
| 21 | |
| 22 def process(self, query): | |
| 23 return query | |
| 24 | |
| 25 def test(self, query): | |
| 26 return True | |
| 27 | |
| 28 def open(self, query): | |
| 29 if not self.test(query): | |
| 30 return False | |
| 31 url = self.url(query) | |
| 32 os.system("firefox '%s'" % url) | |
| 33 return True | |
| 34 | |
| 35 class URL(Location): | |
| 36 """a straight URL""" | |
| 37 | |
| 38 def process(self, query): | |
| 39 if '://' in query: | |
| 40 return query | |
| 41 return 'http://' + query | |
| 42 | |
| 43 def test(self, query): | |
| 44 """try to open the url""" | |
| 45 | |
| 46 if ' ' in query or '\n' in query: | |
| 47 return False | |
| 48 | |
| 49 try: | |
| 50 site = urllib.urlopen(self.process(query)) | |
| 51 except IOError: | |
| 52 return False | |
| 53 return True | |
| 54 | |
| 55 class GoogleMap(Location): | |
| 56 """try to google-maps the address""" | |
| 57 | |
| 58 def __init__(self): | |
| 59 gmapsurl='http://maps.google.com/maps?f=q&hl=en&q=' | |
| 60 Location.__init__(self, gmapsurl) | |
| 61 | |
| 62 def process(self, query): | |
| 63 theaddress = address.normalizeaddress(query) | |
| 64 if not theaddress: | |
| 65 return theaddress | |
| 66 return urllib.quote_plus(theaddress) | |
| 67 | |
| 68 def test(self, query): | |
| 69 return bool(self.process(query)) | |
| 70 | |
| 71 class Revision(Location): | |
| 72 def __init__(self): | |
| 73 revision_url = 'http://trac.openplans.org/openplans/changeset/' | |
| 74 Location.__init__(self, revision_url) | |
| 75 | |
| 76 def process(self, query): | |
| 77 return query[1:] | |
| 78 | |
| 79 def test(self, query): | |
| 80 if query[0] != 'r': | |
| 81 return False | |
| 82 return query[1:].isdigit() | |
| 83 | |
| 84 | |
| 85 | |
| 86 class TracTicket(Location): | |
| 87 def __init__(self): | |
| 88 # url for # data | |
| 89 number_url = 'http://trac.openplans.org/openplans/ticket/' | |
| 90 Location.__init__(self, number_url) | |
| 91 | |
| 92 def process(self, query): | |
| 93 if query.startswith('#'): | |
| 94 return query[1:] | |
| 95 return query | |
| 96 | |
| 97 def test(self, query): | |
| 98 query = self.process(query) | |
| 99 if len(query.split()) != 1: | |
| 100 return False | |
| 101 return query.isdigit() | |
| 102 | |
| 103 class Wikipedia(Location): | |
| 104 """try to open the query in wikipedia""" | |
| 105 def __init__(self): | |
| 106 wikiurl = 'http://en.wikipedia.org/wiki/' | |
| 107 Location.__init__(self, wikiurl) | |
| 108 | |
| 109 def process(self, query): | |
| 110 return urllib.quote_plus('_'.join(query.split())) | |
| 111 | |
| 112 def test(self, query): | |
| 113 'test to see if the article exists' | |
| 114 | |
| 115 # need a phony user agent so wikipedia won't know we're a bot | |
| 116 headers = {} | |
| 117 headers['User-Agent'] = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4' | |
| 118 | |
| 119 request = urllib2.Request(self.url(query), None, headers) | |
| 120 f = urllib2.urlopen(request).read() | |
| 121 | |
| 122 if 'Wikipedia does not have an article with this exact name' in f: | |
| 123 return False | |
| 124 return True | |
| 125 | |
| 126 class Google(Location): | |
| 127 def __init__(self): | |
| 128 googleurl = 'http://www.google.com/search?hl=en&q=' | |
| 129 Location.__init__(self, googleurl) | |
| 130 | |
| 131 def process(self, query): | |
| 132 return urllib.quote_plus(query) | |
| 133 | |
| 134 # get data to be operated on | |
| 135 data = ' '.join(sys.argv[1:]) | |
| 136 if not data: | |
| 137 data = sys.stdin.read() | |
| 138 | |
| 139 locations = [ URL, | |
| 140 GoogleMap, | |
| 141 Revision, | |
| 142 TracTicket, | |
| 143 Wikipedia, | |
| 144 Google | |
| 145 ] | |
| 146 | |
| 147 for loc in locations: | |
| 148 loc = loc() | |
| 149 if loc.open(data): | |
| 150 sys.exit(0) |
