当前位置: 首页>>代码示例>>Python>>正文


Python Properties.set_geo_ip方法代码示例

本文整理汇总了Python中properties.Properties.set_geo_ip方法的典型用法代码示例。如果您正苦于以下问题:Python Properties.set_geo_ip方法的具体用法?Python Properties.set_geo_ip怎么用?Python Properties.set_geo_ip使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在properties.Properties的用法示例。


在下文中一共展示了Properties.set_geo_ip方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Window

# 需要导入模块: from properties import Properties [as 别名]
# 或者: from properties.Properties import set_geo_ip [as 别名]
class Weather:
    """
        Fetches weather info from weather.com and
        sets window properties for skinners to use.
    """
    # weather window for setting weather properties
    WINDOW = Window(12600)
    # set refresh time in minutes
    REFRESH_TIME = 25

    def __init__(self, addon, index, refresh=False):
        # set our Addon class
        self.Addon = addon
        # get weather.com code, used to fetch proper weather info
        self.location_id = self.Addon.getSetting("Location{index}Id".format(index=index))
        # is this an IP based GEO location
        geolocation = (self.location_id == "*")
        # set our properties class
        self.properties = Properties(addon=self.Addon, window=self.WINDOW, geolocation=geolocation, locationId=self.location_id)
        # if IP based GEO location, fetch location
        if (geolocation):
            self._get_geo_location_id()
        # set force refresh
        self.refresh = refresh
        # set base path for source
        self.base_path = os.path.join(xbmc.translatePath(self.Addon.getAddonInfo("Profile")).decode("UTF-8"), u"source", u"weather-{id}.xml".format(id=self.location_id))
        # set status property to false to signify fetching...
        self.properties.set_status_properties(msg="false")

    def _get_geo_location_id(self):
        # FIXME: Remove if block and always search for mobile application (e.g. traveling) maybe have
        # a setting since every time you change locations it has to fetch IP, if not mobile leave if block.
        # get GEO location id (IP based GEO location-only once per session) for mobile based we need to always search if IP has changed
        if (self.WINDOW.getProperty("Location.IP")):
            print "USING EXISTING IP"
            self.location_id = self.Addon.getSetting("LocationGeoId")
        else:
            print "GETTING NEW IP"
            # search for town by IP
            from resources.lib import search
            self.location_id, ip = search.TownSearch(addon=self.Addon, index="Geo").get_geo_location()
            # set IP property and location ID
            self.properties.set_geo_ip(self.location_id, ip)

    def fetch_weather(self):
        # get the source files date if it exists
        try:
            date = time.mktime(time.gmtime(os.path.getmtime(self.base_path)))
        except:
            date = 0
        # set default expiration date
        expires = date + (self.REFRESH_TIME * 60)
        # see if necessary to refresh source
        refresh = ((time.mktime(time.gmtime())) > expires) or self.refresh
        try:
            print "RRRRRRRRRRRRRRRRRRRRRR"
            # do we need to fetch new source?
            if (refresh):
                print "YES"
                # request base url
                print BASE_URLS["weather"].format(id=self.location_id, partnerid=PARTNER_ID, partnerkey=PARTNER_KEY)
                request = urllib2.Request(BASE_URLS["weather"].format(id=self.location_id, partnerid=PARTNER_ID, partnerkey=PARTNER_KEY), headers=HEADERS)
                # get response
                response = urllib2.urlopen(request)
                # read source
                source = unicode(response.read(), "UTF-8")
                # if error raise error
                if (source.find("<error>") >= 0):
                    raise urllib2.URLError("Error fetching weather info!")
                # cache source
                cache_source(self.base_path, source)
            else:
                # open cached file
                source = unicode(open(self.base_path, "r").read(), "UTF-8")
        except urllib2.URLError as error:
            """
            # remove source
            if (os.path.isfile(self.base_path)):
                os.remove(self.base_path)
            """
            # set error message
            print error.reason
            msg = "error"
            # clear info
            info = self._clear_info()
        else:
            # set success message
            msg = "true"
            # parse info
            info = self._parse_source(source)
            # set properties
            self.properties.set_properties(info)
        # set status property
        self.properties.set_status_properties(msg=msg)
        # return message for other weather addons
        return msg

    def _parse_source(self, source):
        # regex's
        regex_location = re.compile("<loc.+?id=\"([^\"]+)\">.+?<dnam>([^<]+)</dnam>.+?<tm>([^<]+)</tm>.+?<lat>([^<]+)</lat>.+?<lon>([^<]+)</lon>.+?<sunr>([^<]+)</sunr>.+?<suns>([^<]+)</suns>.+?<zone>([^<]+)</zone>.+?</loc>", re.DOTALL + re.IGNORECASE)
#.........这里部分代码省略.........
开发者ID:nuka1195,项目名称:weather.weather.com_plus,代码行数:103,代码来源:weather.py


注:本文中的properties.Properties.set_geo_ip方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。