Advertisement
Guest User

IndexTicker.py

a guest
Jul 6th, 2012
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.15 KB | None | 0 0
  1. # IndexTicker.py
  2. # Obtain stock indexes for xbmc
  3. # Place in XBMC/skin/YourSkin/scripts/
  4. # Skin integration instructions (required) can be found at the end of the script
  5. #
  6. # GPL share and share alike
  7.  
  8. import xbmc, xbmcgui
  9. from xbmcgui import Window
  10. from urllib import quote_plus, unquote_plus
  11. import re
  12. import sys
  13. import os
  14.  
  15. class Main:
  16.     # grab the slideshow window
  17.     WINDOW = Window( 12007 )
  18.  
  19.     def _clear_properties( self ):
  20.         self.WINDOW.clearProperty( "TimeOQ" )
  21.         self.WINDOW.clearProperty( "DJ" )
  22.         self.WINDOW.clearProperty( "SP" )
  23.         self.WINDOW.clearProperty( "NQ" )
  24.         self.WINDOW.clearProperty( "DJUD" )
  25.         self.WINDOW.clearProperty( "SPUD" )
  26.         self.WINDOW.clearProperty( "NQUD" )
  27.  
  28.     def __init__( self ):
  29.         # clear properties
  30.         self._clear_properties()
  31.         self._fetch_quotes()
  32.  
  33.     def _fetch_quotes( self ):
  34.         # Fetch Google finance page
  35.         import urllib,urllib2 , re
  36.         Base_URL = "http://finance.google.com/"
  37.  
  38.         WebSock = urllib.urlopen(Base_URL)  # Opens a 'Socket' to URL
  39.         WebHTML = WebSock.read()            # Reads Contents of URL and saves to Variable
  40.         WebSock.close()                     # Closes connection to url
  41.  
  42.         DJquote = re.compile(r'<td.*><span id="ref_983582_l">(\d+,\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
  43.         DJdelta = re.compile(r'<td><span class=".*ch[grb]" id="ref_983582_c">([+-]?\d*\.\d\d)</span>').findall(WebHTML) # regex quote
  44.         DJperchange = re.compile(r'<td><span class="ch[grb]" id="ref_983582_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
  45.  
  46.         SPquote = re.compile(r'<td.*><span id="ref_626307_l">(\d*\,*\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
  47.         SPdelta = re.compile(r'<td><span class=".*ch[gr]" id="ref_626307_c">([+-]\d*\.\d\d)</span>').findall(WebHTML) # regex quote
  48.         SPperchange = re.compile(r'<td><span class="ch[gr]" id="ref_626307_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
  49.  
  50.         NQquote = re.compile(r'<td.*><span id="ref_13756934_l">(\d*\,*\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
  51.         NQdelta = re.compile(r'<td><span class=".*ch[gr]" id="ref_13756934_c">([+-]\d*\.\d\d)</span>').findall(WebHTML) # regex quote
  52.         NQperchange = re.compile(r'<td><span class="ch[gr]" id="ref_13756934_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
  53.  
  54.         try: print 'INDEXTICKER: DJquote - ' + DJquote[0]
  55.         except: pass
  56.         try: print 'INDEXTICKER: DJdelta - ' + DJdelta[0]
  57.         except: pass
  58.         try: print 'INDEXTICKER: DJperchange - ' + DJperchange[0]
  59.         except: pass
  60.         try: print 'INDEXTICKER: SPquote - ' + SPquote[0]
  61.         except: pass
  62.         try: print 'INDEXTICKER: SPdelta - ' + SPdelta[0]
  63.         except: pass
  64.         try: print 'INDEXTICKER: SPperchange - ' + SPperchange[0]
  65.         except: pass
  66.         try: print 'INDEXTICKER: NQquote - ' + NQquote[0]
  67.         except: pass
  68.         try: print 'INDEXTICKER: NQdelta - ' + NQdelta[0]
  69.         except: pass
  70.         try: print 'INDEXTICKER: NQperchange - ' + NQperchange[0]
  71.         except: pass
  72.  
  73.         # Parse Values
  74.  
  75.         # Time
  76.         import time
  77.         Time = str(time.strftime ('%H:%M:%S%p'))
  78.         Date = str(time.strftime ('%m/%d/%Y'))
  79.         # print Date + Time
  80.  
  81.         # Clean
  82.         TimeOQ = Date + '  ' + Time
  83.         try:
  84.                 DJ = DJquote[0] + '  ' + DJdelta[0] + '  ' + DJperchange[0]
  85.         except:
  86.                 print "INDEXTICKER: DJ concat failed"
  87.                 pass
  88.         try:
  89.                 SP = SPquote[0] + '  ' + SPdelta[0] + '  ' + SPperchange[0]
  90.         except:        
  91.                 print "INDEXTICKER: S&P concat failed"
  92.                 pass
  93.         try:
  94.                 NQ = NQquote[0] + '  ' + NQdelta[0] + '  ' + NQperchange[0]
  95.         except:        
  96.                 print "INDEXTICKER: NASDAQ concat failed"
  97.                 pass
  98.         # DJUD = DJdelta[0].startswith("-")
  99.         try:
  100.                 if DJdelta[0].startswith("-") :
  101.                         DJUD='negative'
  102.                 else :
  103.                         DJUD=''
  104.         except:
  105.                 print "INDEXTICKER: DJUD parse failed"
  106.                 pass
  107.         try:
  108.                 if SPdelta[0].startswith("-") :
  109.                         SPUD='negative'
  110.                 else :
  111.                         SPUD=''
  112.         except:
  113.                 print "INDEXTICKER: SPUD parse failed"
  114.                 pass
  115.         try:
  116.                 if NQdelta[0].startswith("-") :
  117.                         NQUD='negative'
  118.                 else :
  119.                         NQUD=''
  120.         except:
  121.                 print "INDEXTICKER: NQUD parse failed"
  122.                 pass
  123.  
  124.         # set properties
  125.         try:
  126.                 self.WINDOW.setProperty( "TimeOQ" , str( TimeOQ ) or "" )
  127.         except:
  128.                 print "INDEXTICKER: TimeOQ set property failed"
  129.                 pass
  130.         try:
  131.                 self.WINDOW.setProperty( "DJ" , str( DJ ) or "" )
  132.         except:
  133.                 print "INDEXTICKER: DJ set property failed"
  134.                 pass
  135.         try:
  136.                 self.WINDOW.setProperty( "DJUD" , str( DJUD ) or "" )
  137.         except:
  138.                 print "INDEXTICKER: DJUD set property failed"
  139.                 pass
  140.         try:
  141.                 self.WINDOW.setProperty( "SP" , str( SP ) or "" )
  142.         except:
  143.                 print "INDEXTICKER: SP set property failed"
  144.                 pass
  145.         try:
  146.                 self.WINDOW.setProperty( "SPUD" , str( SPUD ) or "" )
  147.         except:
  148.                 print "INDEXTICKER: SPUD set property failed"
  149.                 pass
  150.         try:
  151.                 self.WINDOW.setProperty( "NQ" , str( NQ ) or "" )
  152.         except:
  153.                 print "INDEXTICKER: NQ set property failed"
  154.                 pass
  155.         try:
  156.                 self.WINDOW.setProperty( "NQUD" , str( NQUD ) or "" )
  157.         except:
  158.                 print "INDEXTICKER: NQUD set property failed"
  159.                 pass
  160.  
  161. if ( __name__ == "__main__" ):
  162.     Main()
  163.  
  164. # Skin Integration (required)
  165. # I call the script with a custom Keymap.xml
  166. ## the skin code below will fade the panel out after 3 minutes.
  167. #<!-- Stock Beta -->
  168. #                        <control type="group">
  169. #                                <visible>!IsEmpty(Window.Property(DJ))</visible>
  170. #                                <animation effect="fade" time="1800" start="100" end="0" delay="60000" reversible="false" condition="!IsEmpty(Window.Property(DJ))">Conditional</animation>
  171. #                                <animation effect="fade" time="60" start="0" end="100" reversible="false" condition="IsEmpty(Window.Property(DJ))">Conditional</animation>
  172. #                                <include>Window_OpenClose_Animation</include>
  173. #
  174. #                        <control type="image">
  175. #                                <description>Stock background image</description>
  176. #                                <posx>320r</posx>
  177. #                                <posy>522</posy>
  178. #                                <width>500</width>
  179. #                                <height>120</height>
  180. #                                <texture>panel.png</texture>
  181. #                        </control>
  182. #                        <control type="label">
  183. #                                <description>Stock Indexes</description>
  184. #                                <posx>2r</posx>
  185. #                                <posy>529</posy>
  186. #                                <width>500</width>
  187. #                                <height>240</height>
  188. #                                <align>right</align>
  189. #                                <aligny>top</aligny>
  190. #                                <font>font-15</font>
  191. #                                <textcolor>lightgreen</textcolor>
  192. #                                <shadowcolor>44000000</shadowcolor>
  193. #                                <!-- <label>[B]Stock Indexes[/B][CR]Quote Time: $INFO[Window.Property(TimeOQ)][CR]DJUD: $INFO[Window.Property(DJUD)][CR]SPUD: $INFO[Window.Property(SPUD)][CR]NQUD: $INFO[Window.Property(NQUD)]</label> -->
  194. #                                <label>[B]Stock Indexes[/B][CR]Quote Time: $INFO[Window.Property(TimeOQ)]</label>
  195. #                        </control>
  196. #                        <control type="label">
  197. #                                <description>Positive DJ</description>
  198. #                                <posx>2r</posx>
  199. #                                <posy>564</posy>
  200. #                                <width>500</width>
  201. #                                <height>240</height>
  202. #                                <align>right</align>
  203. #                                <aligny>top</aligny>
  204. #                                <font>font-19</font>
  205. #                                <textcolor>green</textcolor>
  206. #                                <shadowcolor>black</shadowcolor>
  207. #                                <label>Dow: $INFO[Window.Property(DJ)]</label>
  208. #                                <visible>IsEmpty(Window.Property(DJUD))</visible>
  209. #                        </control>
  210. #                        <control type="label">
  211. #                                <description>Negative DJ</description>
  212. #                                <posx>2r</posx>
  213. #                                <posy>564</posy>
  214. #                                <width>500</width>
  215. #                                <height>240</height>
  216. #                                <align>right</align>
  217. #                                <aligny>top</aligny>
  218. #                                <font>font-19</font>
  219. #                                <textcolor>red</textcolor>
  220. #                                <shadowcolor>black</shadowcolor>
  221. #                                <label>Dow: $INFO[Window.Property(DJ)]</label>
  222. #                                <visible>!IsEmpty(Window.Property(DJUD))</visible>
  223. #                        </control>
  224. #                        <control type="label">
  225. #                                <description>Positive SP</description>
  226. #                                <posx>2r</posx>
  227. #                                <posy>587</posy>
  228. #                                <width>500</width>
  229. #                                <height>240</height>
  230. #                                <align>right</align>
  231. #                                <aligny>top</aligny>
  232. #                                <font>font-19</font>
  233. #                                <textcolor>green</textcolor>
  234. #                                <shadowcolor>black</shadowcolor>
  235. #                                <label>S&P: $INFO[Window.Property(SP)]</label>
  236. #                                <visible>IsEmpty(Window.Property(SPUD))</visible>
  237. #                        </control>
  238. #                        <control type="label">
  239. #                                <description>Negative SP</description>
  240. #                                <posx>2r</posx>
  241. #                                <posy>587</posy>
  242. #                                <width>500</width>
  243. #                                <height>240</height>
  244. #                                <align>right</align>
  245. #                                <aligny>top</aligny>
  246. #                                <font>font-19</font>
  247. #                                <textcolor>red</textcolor>
  248. #                                <shadowcolor>black</shadowcolor>
  249. #                                <label>S&P: $INFO[Window.Property(SP)]</label>
  250. #                                <visible>!IsEmpty(Window.Property(SPUD))</visible>
  251. #                        </control>
  252. #                        <control type="label">
  253. #                                <description>Positive NQ</description>
  254. #                                <posx>2r</posx>
  255. #                                <posy>610</posy>
  256. #                                <width>500</width>
  257. #                                <height>240</height>
  258. #                                <align>right</align>
  259. #                                <aligny>top</aligny>
  260. #                                <font>font-19</font>
  261. #                                <textcolor>green</textcolor>
  262. #                                <shadowcolor>black</shadowcolor>
  263. #                                <label>NASDAQ: $INFO[Window.Property(NQ)]</label>
  264. #                                <visible>IsEmpty(Window.Property(NQUD))</visible>
  265. #                        </control>
  266. #                        <control type="label">
  267. #                                <description>Negative NQ</description>
  268. #                                <posx>2r</posx>
  269. #                                <posy>610</posy>
  270. #                                <width>500</width>
  271. #                                <height>240</height>
  272. #                                <align>right</align>
  273. #                                <aligny>top</aligny>
  274. #                                <font>font-19</font>
  275. #                                <textcolor>red</textcolor>
  276. #                                <shadowcolor>black</shadowcolor>
  277. #                                <label>NASDAQ: $INFO[Window.Property(NQ)]</label>
  278. #                                <visible>!IsEmpty(Window.Property(NQUD))</visible>
  279. #                        </control>
  280. #                        </control>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement