[FIX] unittests
[pywienerlinien] / scotty.py
index 47f3a95..4d22534 100644 (file)
--- a/scotty.py
+++ b/scotty.py
@@ -1,3 +1,6 @@
+#!/usr/bin/env python
+# -*- coding: UTF-8 -*-
+
 from BeautifulSoup import BeautifulSoup, NavigableString
 from urllib2 import urlopen
 from urllib import urlencode
@@ -6,9 +9,11 @@ from datetime import datetime, time
 from textwrap import wrap
 import argparse
 import sys
+import os.path
 
 POSITION_TYPES = ('stop', 'address', 'poi')
 TIMEFORMAT = '%H:%M'
+DEBUGLOG = os.path.expanduser('~/gotoVienna.debug')
 
 class ParserError(Exception):
     
@@ -42,7 +47,12 @@ def search(origin_tuple, destination_tuple, dtime=None):
     params = urlencode(post)
     url = '%s?%s' % (settings.action, params)
     
-    print "\nurl %s url\n\n%s\n\nurl %s url\n" % ('~' * 100, url, '~' * 100)
+    try:
+        f = open(DEBUGLOG, 'a')
+        f.write(url + '\n')
+        f.close()
+    except:
+        print 'Unable to write to DEBUGLOG: %s' % DEBUGLOG
     
     return urlopen(url)
 
@@ -210,7 +220,7 @@ class rParser:
             try:
                 self._overview = self._parse_overview()
             except AttributeError:
-                f = open('DEBUG', 'w')
+                f = open(DEBUGLOG, 'w')
                 f.write(str(self.soup))
                 f.close()
 
@@ -224,8 +234,7 @@ if __name__ == '__main__':
     parser.add_argument('-dt', metavar='type', type=str, help='destination type: %s' % ' | '.join(POSITION_TYPES), default='stop', choices=POSITION_TYPES)
 
     args = parser.parse_args()
-    
-    html = search((args.o.encode('UTF-8'), args.ot), (args.d.encode('UTF-8'), args.dt)).read()
+    html = search((args.o, args.ot), (args.d, args.dt)).read()
     
     parser = sParser(html)
     state = parser.check_page()
@@ -237,7 +246,6 @@ if __name__ == '__main__':
                 print
                 print '* Origin ambiguous:'
                 lo = None
-                ld = None
                 while not lo or not lo.isdigit() or int(lo) > len(cor[0]):
                     i = 1
                     for c in cor[0]:
@@ -250,6 +258,7 @@ if __name__ == '__main__':
             if cor[1]:
                 print
                 print '* Destination ambiguous:'
+                ld = None
                 while not ld or not ld.isdigit() or int(ld) > len(cor[1]):
                     j = 1
                     for c in cor[1]:
@@ -278,6 +287,8 @@ if __name__ == '__main__':
                     print '%d. [%s] %s-%s (%s)' % (r + 1, overviews[r]['date'], overviews[r]['time'][0], overviews[r]['time'][1], overviews[r]['duration'])
                 print 'q. Quit'
                 l = sys.stdin.readline().strip()
+                print
+                print '~' * 100
                 
                 if l.isdigit() and int(l) <= len(details):
                     for detail in details[int(l) - 1]: