Skip to content

Commit

Permalink
Add encoding support on English OS
Browse files Browse the repository at this point in the history
Support to print correctly on English OS
  • Loading branch information
unknown authored and unknown committed May 8, 2012
1 parent 3fb9b0d commit 8de0214
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions python/fetion/Lumia800.py
@@ -1,19 +1,20 @@
#!user/bin/python
#!user/bin/python
#coding:utf-8

import urllib2
import re
import ConfigParser
import getopt
import sys
import logging

import fetion
import fetionini

URL_HQ = 'http://bbs.hq1388.com/forum.php?mod=viewthread&tid=15525&extra=page%3D1'
LUMIA_PATTERN = r"Nokia Lumia 800"
PRICE_PATTERN = '<td><strong>(.+)¥(.*?)<.+'
FINISH_PATTERN = r'点击图片查看详情'
PRICE_PATTERN = r'<td><strong>(.+)¥(.*?)<.+'
FINISH_PATTERN = r'点击图片查看详情'

# Python has no build-in enum type.
# It's a simple workaround here.
Expand All @@ -23,27 +24,39 @@ class ParseState:
def get_lumia_prices():
state = ParseState.Start
prices = {}
logger = logging.getLogger()
logFileName = sys.argv[0] + 'log.txt'
handler = logging.FileHandler(logFileName)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)
for line in urllib2.urlopen(URL_HQ):
logger.debug(line)
# regular expression match can only match the UTF-8
# if source code is UTF-8 encoding
line = line.decode('gbk').encode('utf-8')
if ParseState.Start == state:
if re.search(LUMIA_PATTERN, line, re.I):
state = ParseState.Find
elif ParseState.Find == state:
m = re.match(PRICE_PATTERN, line, re.I)
if m:
prices[m.group(1).strip()] = m.group(2).strip()
key = m.group(1).strip().decode('utf-8')
value = m.group(2).strip().decode('utf-8')
prices[key] = value
elif re.search(FINISH_PATTERN, line):
state = ParseState.Finish
break

msg = LUMIA_PATTERN + '当天价格--'
msg = u'Nokia Lumia 800当天价格--'
for key, value in prices.iteritems():
msg += key + ':' + value + " "
logger.debug(key + u":" + value)
msg += key + u':' + value + u" "
return msg

def usage():
print 'python Lumia800.py 输出Lumia800当日价格'
print 'python Lumia800.py -s 输出Lumia800当日价格并发送短信'
print 'python Lumia800.py -h 帮助'
print u'python Lumia800.py 输出Lumia800当日价格'
print u'python Lumia800.py -s 输出Lumia800当日价格并发送短信'
print u'python Lumia800.py -h 帮助'

need_send_message = False
try:
Expand All @@ -63,4 +76,4 @@ def usage():
msg = get_lumia_prices()
print msg
if need_send_message:
fetionini.fetionINI(msg)
fetionini.fetionINI(msg.encode('gbk'))

0 comments on commit 8de0214

Please sign in to comment.