Index: indicator-china-weather-1.0.4/src/indicator-china-weather.py =================================================================== --- indicator-china-weather-1.0.4.orig/src/indicator-china-weather.py 2013-03-13 09:53:51.000000000 +0800 +++ indicator-china-weather-1.0.4/src/indicator-china-weather.py 2013-03-28 07:06:50.411271846 +0800 @@ -25,7 +25,6 @@ pass import os, sys, tempfile import gtk, pygtk, gobject -pygtk.require('2.0') import commands, threading import appindicator import logging, logging.handlers @@ -412,7 +411,6 @@ if self.placechosen >= len(self.places): self.placechosen = 0 - print(self.placechosen) self.place = self.places[self.placechosen] self.settings.set_value("placechosen", self.placechosen) self.city_id = self.places[self.placechosen][0] @@ -455,6 +453,7 @@ if pint > 7 or pint < 20: self.icon = self.weather_icons[self.weather_data['img1']] else : + #self.pm_show.set_label(_('PM2.5:') + self.pm['quality'] + ' ' + str(self.pm['aqi'])) self.icon = self.weather_icons[self.weather_data['img2']] self.menu_normal() self.winder.set_icon(self.icon) @@ -471,19 +470,18 @@ #city = self.place.split(',')[2] #self.pm = get_pm_from_city(city) - - if self.pm == None:#(LP: #1152853) + if self.pm in (False, None, '', '[]', "['']"):#(LP: #1152853) self.pm = {} pm_flag = False - if self.pm.has_key('explain') and self.pm.has_key('cue') and self.pm.has_key('figure'): + if self.pm.has_key('aqi') and self.pm.has_key('quality'): pm_flag = True if not pm_flag: self.pm_show.set_label(_('PM2.5:') + 'N/A') else: - if self.pm['explain'] == 'NA' and self.pm['cue'] == 'NA' and self.pm['figure'] == 'NA': + if (self.pm).has_key('error'): self.pm_show.set_label(_('PM2.5:') + 'N/A') else: - self.pm_show.set_label(_('PM2.5:') + self.pm['explain'] + ' ' + self.pm['figure'][0]) + self.pm_show.set_label(_('PM2.5:') + self.pm['quality'] + ' ' + str(self.pm['aqi'])) if self.temp: self.update_label(self.weather_data['temp'] + '℃') @@ -499,11 +497,11 @@ from pycwapi import get_location_from_cityid try: #self.place = get_location_from_cityid(self.city_id) - self.pm = get_pm_from_city(self.place[1].split(',')[2]) - if self.pm['explain'] == 'NA' and self.pm['cue'] == 'NA' and self.pm['figure'] == 'NA': + self.pm = get_pm(self.place[1].split(',')[2]) + if self.pm.has_key('error'): self.pm_show.set_label(_('PM2.5:') + 'N/A') else: - self.pm_show.set_label(_('PM2.5:') + self.pm['explain'] + ' ' + self.pm['figure'][0]) + self.pm_show.set_label('PM2.5:' + self.pm['quality'] + ' ' + str(self.pm['aqi'])) except Exception, e: log.error(e) log.debug(traceback.format_exc(e)) @@ -622,7 +620,7 @@ need_to_update = False #City id if iw.city_change_flag: - iw.city_change_flag = False + iw.citget_location_from_cityidy_change_flag = False need_to_update = True #Show temperature @@ -653,7 +651,6 @@ iw.places = newplaces log.debug("Preferences: Places changed to '%s'" % str(iw.places)) iw.settings.set_value("places", str(iw.places)) - print(iw.place) if (type(iw.place) != None) and (iw.place in iw.places): iw.placechosen = iw.places.index(iw.place) iw.city_id = iw.place[0] @@ -663,7 +660,6 @@ iw.place = iw.places[0] iw.city_id = iw.places[0][0] log.debug("Preferences: Place Chosen changed to '%s'" % iw.placechosen) - print(iw.placechosen) iw.settings.set_value("city_id", str(iw.city_id)) iw.settings.set_value("placechosen", iw.placechosen) iw.menu_normal() Index: indicator-china-weather-1.0.4/src/pm25.py =================================================================== --- indicator-china-weather-1.0.4.orig/src/pm25.py 2013-03-13 09:49:23.000000000 +0800 +++ indicator-china-weather-1.0.4/src/pm25.py 2013-03-28 07:06:50.415271846 +0800 @@ -1,169 +1,37 @@ #!/usr/bin/env python -import os, sys -import urllib2, urllib -from sgmllib import SGMLParser +import json +import os,sys +import urllib2,urllib -CHANGSHA_URL = 'http://pm25.moji001.com/aqi/index-47.html' -PART_URL = 'http://pm25.moji001.com/aqi/' -#CITY = '\xe5\x85\xb0\xe5\xb7\x9e' +beginurl = 'http://pm25.in/api/querys/pm2_5.json?city=' +#city = '\xe5\x85\xb0\xe5\xb7\x9e' +token = '&token=X1K1gyCibzqxfvJQnpsi' -PROJECT_ROOT_DIRECTORY = os.path.abspath( - os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))) -PM_CITY_LIST_FILE = os.path.join(PROJECT_ROOT_DIRECTORY, 'src/city_list.txt') -PM_DIC = {'figure':'','explain':'','cue':''} - -class GetCodeUrl: - def change_str(self, cityplace): - tran_unicode_tmp = unicode(cityplace, "utf-8") - return tran_unicode_tmp +class Getpmdata: def get_url(self,cityplace): - all_url = '' - cs_string = self.read_from_url(CHANGSHA_URL) - city_url = get_city_url(cs_string,cityplace) - if city_url != 'NA': - all_url = PART_URL + city_url - #print all_url - return all_url - - def read_from_url(self, url): - request = urllib2.Request(url, headers={'User-Agent' : 'Magic Browser'}) + url = beginurl + cityplace + token + return url + def get_data(self,url): + request = urllib2.Request(url, headers={'User-Agent ' : 'Magic Browser'}) f = urllib2.urlopen(request) - code_string = f.read() - return code_string - -class GetCue (SGMLParser): - def reset(self): - self.Cue = [] - self.flag = False - self.getdata = False - self.verbatim = 0 - SGMLParser.reset(self) - def start_div(self, attrs): - if self.flag == True: - self.verbatim += 1 - return - for k,v in attrs: - if k == 'class' and v == 'pmleftPrompt': - self.flag = True - return - def end_div(self): - if self.verbatim == 0: - self.flag = False - if self.flag == True: - self.verbatim -=1 - def start_p(self, attrs): - if self.flag == False: - return - self.getdata = True - def end_p(self): - if self.getdata: - self.getdata = False - def handle_data(self, text): - if self.getdata: - self.Cue.append(text) - PM_DIC['cue'] = self.Cue - def printID(self): - for i in self.Cue: - print i - -class GetFigure(SGMLParser): - def reset(self): - self.Figure = [] - self.flag = False - self.getdata = False - self.verbatim = False - SGMLParser.reset(self) - def start_div(self, attrs): - if self.getdata == True: - self.verbatim += 1 - return - for k,v in attrs: - if k == 'class' and v == 'pmaqiVLeft': - self.getdata = True - return - def end_div(self): - if self.getdata: - self.getdata = False - def handle_data(self, text): - if self.getdata: - self.Figure.append(text) - PM_DIC['figure'] = self.Figure - def printID(self): - for i in self.Figure: - print i - -class GetExplain: - def my_handle_data(self, json_string): - tmp_str=json_string - urlindex = tmp_str.find('
') - urlend = tmp_str[urlindex:].find('
') - div_str = tmp_str[urlindex:urlindex + urlend] - urlindex = -1 - urlend = -1 - urlindex = div_str.find('">') - urlend = div_str.find('') - urlend = tmp_str[urlindex:].find('
') - city_list_str_tmp = tmp_str[urlindex:urlindex + urlend] - urlindex = -1 - urlend = -1 - urlindex = city_list_str_tmp.find('') - city_list_str_no = city_list_str_tmp[urlindex:urlindex + urlend] - city_list_str_ok = city_list_str_no.replace('\t\t\t','') - urlend = -1 - urlend = city_list_str_ok.find('\t') - city_list_str = city_list_str_ok[:urlend - 1] - city_list_list = city_list_str.split('\n') - flag = 0 - for city in city_list_list: - if cityplace in city: - city_url = city.split('"')[3] - flag = 1 - break - if flag == 0: - city_url = 'NA' - return city_url - -def resault(): - PM_DIC['cue'] = ''.join(PM_DIC['cue']) - PM_DIC['figure'] = ''.join(PM_DIC['figure']) - - x = PM_DIC['explain'] - print x - y = PM_DIC['cue'] - print y - z = PM_DIC['figure'] - print z - -def get_pm_from_city(cityplace): +def get_pm(cityplace): try: - listerg = GetCodeUrl() - listerc = GetCue() - listerf = GetFigure() - listere = GetExplain() - url =listerg.get_url(cityplace) - if url == '' : - PM_DIC['explain'] = 'NA' - PM_DIC['cue'] = 'NA' - PM_DIC['figure'] = 'NA' - return PM_DIC - json_string = listerg.read_from_url(url) - listerc.feed(json_string) - listerf.feed(json_string) - listere.my_handle_data(json_string) - return PM_DIC + ob = Getpmdata() + url = ob.get_url(cityplace) + pmdata = ob.get_data(url) + return pmdata +# print pmdata except Exception as e: print(e) - -#if __name__ == "__main__": -# abc = get_pm_from_city(cityplace) -# print abc