changeset 8:5e26e170a121

version 0.3.0
author Vlad Glagolev <enqlave@gmail.com>
date Mon, 26 Dec 2011 11:08:09 +0400
parents 100d7f29aa6f
children 243dfc25bdec
files ChangeLog MANIFEST.in pycweather/__init__.py pycweather/dmanager.py pycweather/weather.py setup.py share/template.xsl
diffstat 7 files changed, 154 insertions(+), 127 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Dec 26 10:53:20 2011 +0400
+++ b/ChangeLog	Mon Dec 26 11:08:09 2011 +0400
@@ -1,3 +1,8 @@
+2011-12-26 Vlad Glagolev <enqlave@gmail.com>
+	* version 0.3.0
+	* switched to AccuWeather RSS provider
+	* added ConkyWeather font usage to XSL
+
 2009-08-09 Vlad Glagolev <enqlave@gmail.com>
 	* version 0.2.0
 	* fixed max-day limit to 5 as supported by weather.com
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MANIFEST.in	Mon Dec 26 11:08:09 2011 +0400
@@ -0,0 +1,10 @@
+#
+# MANIFEST.in
+# Manifest template for creating the source distribution.
+#
+
+include ChangeLog
+include AUTHORS
+include LICENSE
+include README
+include MANIFEST.in
--- a/pycweather/__init__.py	Mon Dec 26 10:53:20 2011 +0400
+++ b/pycweather/__init__.py	Mon Dec 26 11:08:09 2011 +0400
@@ -2,7 +2,7 @@
 #
 # This file is part of PycWeather
 #
-# Copyright (c) 2008-2009 Vlad Glagolev <enqlave@gmail.com>
+# Copyright (c) 2008-2011 Vlad Glagolev <enqlave@gmail.com>
 # All rights reserved.
 #
 # Permission to use, copy, modify, and distribute this software for any
@@ -19,4 +19,4 @@
 
 """ pycweather/__init__.py: initialization script """
 
-__version__ = "0.2.1"
+__version__ = "0.3.0"
--- a/pycweather/dmanager.py	Mon Dec 26 10:53:20 2011 +0400
+++ b/pycweather/dmanager.py	Mon Dec 26 11:08:09 2011 +0400
@@ -2,7 +2,7 @@
 #
 # This file is part of PycWeather
 #
-# Copyright (c) 2008-2009 Vlad Glagolev <enqlave@gmail.com>
+# Copyright (c) 2008-2011 Vlad Glagolev <enqlave@gmail.com>
 # All rights reserved.
 #
 # Permission to use, copy, modify, and distribute this software for any
@@ -27,23 +27,17 @@
 from pycweather import weather
 
 
-# default values stay for Moscow (RU) location, 1 days to display (means only
-# current day), metric measurement system and internal XSL stylesheet usage
-_CODE = "RSXX0063"
-_DAYS = 1
-_UNIT = "m"
+# default values stay for Moscow (RU) location and internal XSL stylesheet usage
+_CODE = "ASI|RU|RS052|MOSCOW|"
 
 
 def usage():
     print """Usage: %s [options]\n
     -h, --help		show this help
     -v, --version	version information
-    -a, --auth <id:key> authority data in format: partner-id:license-key
     -c, --code <code>	location ID code (default is %s)
-    -d, --days <days>	number of days to display [1-5] (default is %d)
-    -u, --unit <unit>	measurement system [m]etric|[i]mperial (default is %s)
-    -s, --search <word>	search for the specified location (city, state, etc.)
-    -x, --xsl <file>	custom XSL file\n""" % (sys.argv[0], _CODE, _DAYS, _UNIT)
+    -m, --metric	use metric measurement system
+    -x, --xsl <file>	custom XSL file\n""" % (sys.argv[0], _CODE)
 
 
 def version():
@@ -56,15 +50,15 @@
 
 def main(template):
     try:
-        opts, args = getopt.getopt(sys.argv[1:], "hva:c:d:u:s:x:", ["help", \
-                                   "version", "auth=", "code=", "days=", \
-                                   "unit=", "search=", "xsl="])
+        opts, args = getopt.getopt(sys.argv[1:], "hvc:mx:", ["help", \
+                                   "version", "code=", "metric", "xsl="])
     except getopt.error, msg:
         print msg
         usage()
         exit(2)
 
     forecast = weather.Weather()
+    metric = 0
 
     for o, a in opts:
         if o in ("-h", "--help"):
@@ -72,17 +66,10 @@
             exit(0)
         elif o in ("-v", "--version"):
             version()
-        elif o in ("-a", "--auth"):
-            forecast.auth(a)
         elif o in ("-c", "--code"):
             code = a
-        elif o in ("-d", "--days"):
-            days = int(a) if a != "" else _DAYS
-        elif o in ("-u", "--unit"):
-            unit = a
-        elif o in ("-s", "--search"):
-            forecast.search(a)
-            exit(0)
+        elif o in ("-m", "--metric"):
+            metric = 1
         elif o in ("-x", "--xsl"):
             xsl = a
 
@@ -92,24 +79,10 @@
         code = _CODE
 
     try:
-        days
-    except NameError:
-        days = _DAYS
-
-    try:
-        unit
-    except NameError:
-        unit = _UNIT
-
-    try:
         xsl
     except NameError:
         xsl = template
 
     forecast.load(xsl)
 
-    if forecast.id and forecast.key:
-        forecast.preview(code, days, unit)
-    else:
-        print "Get partner ID and license key at: " + \
-              "http://registration.weather.com/ursa/xmloap/step1"
+    forecast.preview(code, metric)
--- a/pycweather/weather.py	Mon Dec 26 10:53:20 2011 +0400
+++ b/pycweather/weather.py	Mon Dec 26 11:08:09 2011 +0400
@@ -2,7 +2,7 @@
 #
 # This file is part of PycWeather
 #
-# Copyright (c) 2009 Vlad Glagolev <enqlave@gmail.com>
+# Copyright (c) 2009-2011 Vlad Glagolev <enqlave@gmail.com>
 # All rights reserved.
 #
 # Permission to use, copy, modify, and distribute this software for any
@@ -27,45 +27,14 @@
 from pycweather import __version__
 
 
-# correct url to the XOAP service
-XOAP_URL = "http://xoap.weather.com/"
+# correct url to AccuWeather RSS service
+RSS_URL = "http://rss.accuweather.com/rss/liveweather_rss.asp"
 
 
 class Weather:
 
     def __init__(self):
         self.xsl = None
-        self.id = None
-        self.key = None
-
-    def auth(self, credentials):
-        try:
-            self.id, self.key = credentials.split(":")
-        except:
-            print "Invalid credentials"
-
-    def search(self, location):
-        data = urlopen(XOAP_URL + "search/search?where=%s" % location)
-
-        tree = data.read()
-
-        try:
-            xml = fromstring(tree)
-        except XMLSyntaxError:
-            print "XML syntax error occured while parsing content: ", tree
-
-            return -1
-
-        if not xml.getchildren():
-            print "No location has been found!"
-        elif xml.tag == "error":
-            print "Error: %s" % xml.getchildren()[0].text
-        else:
-            print "Code    | Location\n--------+----------"
-            for loc in xml.xpath("loc"):
-                print loc.get("id") + ": " + loc.text
-
-            print "--\nTo catch weather information use: pycweather -c <code>"
 
     def load(self, xsl_file):
         try:
@@ -75,13 +44,11 @@
         except:
             print "Unable to read XSL file"
 
-    def preview(self, code, days, unit):
+    def preview(self, code, metric):
         try:
-            data = urlopen(XOAP_URL + \
-                   "weather/local/%s?cc=*&dayf=%d&unit=%s&par=%s&key=%s" % \
-                   (code, days, unit, self.id, self.key))
+            data = urlopen(RSS_URL + "?metric=%d&locCode=%s" % (metric, code))
         except:
-            print "Unable to connect to %s" % XOAP_URL
+            print "Unable to connect to %s" % RSS_URL
 
             return -1
 
@@ -94,11 +61,10 @@
 
             return -1
 
-        if xml.tag == "error":
-            print "Error: %s" % xml.xpath("err")[0].text
-
-            return -1
+        if xml.xpath("channel/description")[0].text == "Invalid Location":
+            print "Wrong location code, get one at www.accuweather.com"
+            exit(2)
         else:
             document = parse(StringIO(tostring(xml)))
 
-        print document.xslt(self.xsl)
+            print document.xslt(self.xsl)
--- a/setup.py	Mon Dec 26 10:53:20 2011 +0400
+++ b/setup.py	Mon Dec 26 11:08:09 2011 +0400
@@ -33,7 +33,7 @@
     author_email = "enqlave@gmail.com",
     packages = ["pycweather"],
     scripts = ["bin/pycweather"],
-    data_files = [('share/pycweather', ["share/template.xsl"])],
+    data_files = [('share/pycweather', ["share/template.xsl", "share/ConkyWeather.ttf"])],
     description = "Weather display manager for conky",
     platforms = ["Linux", "Unix"],
     long_description = "PycWeather is pure-pythonic weather display manager \
--- a/share/template.xsl	Mon Dec 26 10:53:20 2011 +0400
+++ b/share/template.xsl	Mon Dec 26 11:08:09 2011 +0400
@@ -3,7 +3,7 @@
 
 This file is part of PycWeather
 
-Copyright (c) 2009 Vlad Glagolev <enqlave@gmail.com>. All rights reserved.
+Copyright (c) 2009-2011 Vlad Glagolev <enqlave@gmail.com>. All rights reserved.
 
 Permission to use, copy, modify, and distribute this software for any
 purpose with or without fee is hereby granted, provided that the above
@@ -20,63 +20,136 @@
 -->
 <xsl:stylesheet xmlns="http://www.w3.org/1999/xhtml" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
 	<xsl:output method="text" disable-output-escaping="yes"/>
+	<!-- new line -->
 	<xsl:variable name="nl">
 		<xsl:text>&#10;</xsl:text>
 	</xsl:variable>
-	<xsl:template match="weather">
-		<xsl:apply-templates select="cc"/>
-		<xsl:apply-templates select="dayf"/>
-		<xsl:comment>PycWeather</xsl:comment>
-	</xsl:template>
-	<xsl:template match="cc">
-		<xsl:text>Location: </xsl:text><xsl:value-of select="obst"/><xsl:text> (</xsl:text><xsl:value-of select="../loc/lat"/><xsl:text>, </xsl:text><xsl:value-of select="../loc/lon"/><xsl:text>)</xsl:text><xsl:value-of select="$nl"/>
-		<xsl:text>Temperature: </xsl:text><xsl:value-of select="tmp"/>°<xsl:value-of select="../head/ut"/><xsl:value-of select="$nl"/>
-		<xsl:if test="tmp != flik">
-			<xsl:text>Windchill: </xsl:text><xsl:value-of select="flik"/>°<xsl:value-of select="../head/ut"/><xsl:value-of select="$nl"/>
-		</xsl:if>
-		<xsl:text>Conditions: </xsl:text><xsl:value-of select="t"/><xsl:value-of select="$nl"/>
-		<xsl:text>Wind: </xsl:text>
+	<!-- extract image ID -->
+	<xsl:template name="getCondition">
+		<xsl:param name="filename"/>
 		<xsl:choose>
-			<xsl:when test="wind/s = 'calm'">
-				<xsl:text>0</xsl:text>
+			<xsl:when test="contains($filename, '/')">
+				<xsl:call-template name="getCondition">
+					<xsl:with-param name="filename" select="substring-after($filename, '/')"/>
+				</xsl:call-template>
 			</xsl:when>
 			<xsl:otherwise>
-				<xsl:value-of select="wind/s"/>
+				<xsl:value-of select="$filename"/>
 			</xsl:otherwise>
 		</xsl:choose>
-		<xsl:value-of select="../head/us"/>
-		<xsl:text> (</xsl:text><xsl:value-of select="wind/t"/><xsl:text>)</xsl:text>
 	</xsl:template>
-	<!-- MULTIPLE DAYS DISPLAY -->
-	<xsl:template match="dayf">
-		<!-- don't repeat the first one -->
-		<xsl:apply-templates select="child::day[position() > 1]"/>
+	<!-- main block -->
+	<xsl:template match="channel">
+		<xsl:comment>PycWeather 0.3.0</xsl:comment>
+		<xsl:text>Location: </xsl:text><xsl:value-of select="normalize-space(substring-before(title, '- AccuWeather.com'))"/><xsl:value-of select="$nl"/>
+		<xsl:text>Update Time: </xsl:text><xsl:value-of select="pubDate"/>
+		<!-- don't parse info field -->
+		<xsl:apply-templates select="child::item[position() != last()]"/>
 	</xsl:template>
-	<xsl:template match="day">
+	<!-- day field -->
+	<xsl:template match="item">
 		<xsl:value-of select="$nl"/>
-		<xsl:value-of select="@dt"/><xsl:text>, </xsl:text><xsl:value-of select="@t"/>
-		<xsl:if test="@d = 1">
-			<xsl:text> (Tomorrow)</xsl:text>
-		</xsl:if>
-		<xsl:text>: </xsl:text>
-		<xsl:apply-templates select="part"/>
-	</xsl:template>
-	<xsl:template match="part">
+		<xsl:variable name="condition">
+			<xsl:call-template name="getCondition">
+				<xsl:with-param name="filename" select="substring-before(description, '_')"/>
+			</xsl:call-template>
+		</xsl:variable>
+		<!-- convert image ID to ConkyWeather font character -->
 		<xsl:choose>
-			<xsl:when test="@p = 'd'">
-				<xsl:text>Day (</xsl:text>
-				<xsl:value-of select="../hi"/>°<xsl:value-of select="../../../head/ut"/>
+			<xsl:when test="number($condition) = 1">
+				<xsl:text>a</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 2">
+				<xsl:text>b</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 3">
+				<xsl:text>c</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 4">
+				<xsl:text>c</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 5">
+				<xsl:text>c</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 6">
+				<xsl:text>d</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 7">
+				<xsl:text>e</xsl:text>
+			</xsl:when>
+			<xsl:when test="number($condition) = 8">
+				<xsl:text>e</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 11">
+				<xsl:text>0</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 12">
+				<xsl:text>h</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 13 or $condition = 14">
+				<xsl:text>g</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 15">
+				<xsl:text>l</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 16 or $condition = 17">
+				<xsl:text>k</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 18 or $condition = 26">
+				<xsl:text>i</xsl:text>
 			</xsl:when>
+			<xsl:when test="$condition = 19">
+				<xsl:text>p</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 20 or $condition = 21 or $condition = 23">
+				<xsl:text>o</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 22">
+				<xsl:text>r</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 24 or $condition = 31">
+				<xsl:text>E</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 25">
+				<xsl:text>u</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 29">
+				<xsl:text>v</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 30">
+				<xsl:text>5</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 32">
+				<xsl:text>6</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 33">
+				<xsl:text>A</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 34 or $condition = 36 or $condition = 37">
+				<xsl:text>B</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 35 or $condition = 38">
+				<xsl:text>C</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 39 or $condition = 40">
+				<xsl:text>G</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 41 or $condition = 42">
+				<xsl:text>K</xsl:text>
+			</xsl:when>
+			<xsl:when test="$condition = 43 or $condition = 44">
+				<xsl:text>O</xsl:text>
+			</xsl:when>
+			<!-- N/A -->
 			<xsl:otherwise>
-				<xsl:text>Night (</xsl:text>
-				<xsl:value-of select="../low"/>°<xsl:value-of select="../../../head/ut"/>
+				<xsl:text>-</xsl:text>
 			</xsl:otherwise>
 		</xsl:choose>
-		<xsl:text>, </xsl:text>
-		<xsl:apply-templates select="t"/>
-		<xsl:text>)</xsl:text>
-		<xsl:if test="@p = 'd'">
-			<xsl:text>; </xsl:text>
+		<xsl:text> -- </xsl:text>
+		<xsl:value-of select="title"/>
+		<xsl:if test="position() != 1">
+			<xsl:text>: </xsl:text>
+			<xsl:value-of select="substring-before(description, '&lt;')"/>
 		</xsl:if>
 	</xsl:template>
 </xsl:stylesheet>