PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/programs/MyStocks/MystocksAPI/mystocks_search.py

http://xbmc-addons-chinese.googlecode.com/
Python | 53 lines | 52 code | 1 blank | 0 comment | 0 complexity | 1d362d57fe2b36733f7e006dabd77302 MD5 | raw file
  1. import sys
  2. import os
  3. import xbmc
  4. import xbmcgui
  5. import xbmcplugin
  6. import urllib2
  7. import re
  8. from mystocks_lib import *
  9. class Main:
  10. def __init__( self ):
  11. keyboard = xbmc.Keyboard('')
  12. keyboard.doModal()
  13. if (keyboard.isConfirmed()):
  14. self._search(keyboard.getText())
  15. def _search( self, inText):
  16. req = urllib2.Request('http://biz.finance.sina.com.cn/suggest/lookup_n.php?country=stock&q=' + inText)
  17. req.add_header('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3')
  18. response = urllib2.urlopen(req)
  19. link=response.read()
  20. response.close()
  21. match0=re.sub('\r','',link)
  22. match0=re.sub('\n','',match0)
  23. print sys.getdefaultencoding()
  24. match=re.compile('<title>检索结果_新浪财经_新浪网</title>').findall(match0)
  25. if (len(match) > 0):
  26. match=re.compile('<div>沪深股市</div>(.+?)<div class="clear"></div>').findall(match0)
  27. try:
  28. match2=re.compile('target="_blank">(.+?)</a></label>').findall(match[0])
  29. if (len(match2) > 0):
  30. for info in match2:
  31. re_v = info.split(' ')
  32. if(len(re_v[0]) == 8):
  33. addLink(re_v[1].decode('gbk').encode('utf8'), re_v[0][2:], "show_graph", re_v[0][:2], folder=False)
  34. xbmcplugin.endOfDirectory(int(sys.argv[1]))
  35. except:
  36. ok = xbmcgui.Dialog().ok('股票搜索', '对不起,没有找到相关股票。')
  37. u=sys.argv[0]+"?url=&mode=&shsz=&name="
  38. xbmc.executebuiltin('XBMC.RunPlugin(%s)' % u)
  39. else:
  40. match=re.compile('var fullcode="(.+?)";').findall(match0)
  41. if(len(match) > 0):
  42. stockId = match[0]
  43. match=re.compile('var stockname="(.+?)";').findall(match0)
  44. stockName = match[0]
  45. addLink(stockName.decode('gbk').encode('utf8'), stockId[2:], "show_graph", stockId[:2], folder=False)
  46. xbmcplugin.endOfDirectory(int(sys.argv[1]))
  47. else:
  48. ok = xbmcgui.Dialog().ok('股票搜索', '对不起,没有找到相关股票。')
  49. u=sys.argv[0]+"?url=&mode=&shsz=&name="
  50. xbmc.executebuiltin('XBMC.RunPlugin(%s)' % u)