# IndexTicker.py
# Obtain stock indexes for xbmc
# Place in XBMC/skin/YourSkin/scripts/
# Skin integration instructions (required) can be found at the end of the script
#
# GPL share and share alike
import xbmc, xbmcgui
from xbmcgui import Window
from urllib import quote_plus, unquote_plus
import re
import sys
import os
class Main:
# grab the slideshow window
WINDOW = Window( 12007 )
def _clear_properties( self ):
self.WINDOW.clearProperty( "TimeOQ" )
self.WINDOW.clearProperty( "DJ" )
self.WINDOW.clearProperty( "SP" )
self.WINDOW.clearProperty( "NQ" )
self.WINDOW.clearProperty( "DJUD" )
self.WINDOW.clearProperty( "SPUD" )
self.WINDOW.clearProperty( "NQUD" )
def __init__( self ):
# clear properties
self._clear_properties()
self._fetch_quotes()
def _fetch_quotes( self ):
# Fetch Google finance page
import urllib,urllib2 , re
Base_URL = "http://finance.google.com/"
WebSock = urllib.urlopen(Base_URL) # Opens a 'Socket' to URL
WebHTML = WebSock.read() # Reads Contents of URL and saves to Variable
WebSock.close() # Closes connection to url
DJquote = re.compile(r'<td.*><span id="ref_983582_l">(\d+,\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
DJdelta = re.compile(r'<td><span class=".*ch[grb]" id="ref_983582_c">([+-]?\d*\.\d\d)</span>').findall(WebHTML) # regex quote
DJperchange = re.compile(r'<td><span class="ch[grb]" id="ref_983582_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
SPquote = re.compile(r'<td.*><span id="ref_626307_l">(\d*\,*\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
SPdelta = re.compile(r'<td><span class=".*ch[gr]" id="ref_626307_c">([+-]\d*\.\d\d)</span>').findall(WebHTML) # regex quote
SPperchange = re.compile(r'<td><span class="ch[gr]" id="ref_626307_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
NQquote = re.compile(r'<td.*><span id="ref_13756934_l">(\d*\,*\d\d\d\.\d\d)</span>').findall(WebHTML) # regex quote
NQdelta = re.compile(r'<td><span class=".*ch[gr]" id="ref_13756934_c">([+-]\d*\.\d\d)</span>').findall(WebHTML) # regex quote
NQperchange = re.compile(r'<td><span class="ch[gr]" id="ref_13756934_cp">(\(.*\d*\.\d\d\%\))</span>').findall(WebHTML) # regex quote
try: print 'INDEXTICKER: DJquote - ' + DJquote[0]
except: pass
try: print 'INDEXTICKER: DJdelta - ' + DJdelta[0]
except: pass
try: print 'INDEXTICKER: DJperchange - ' + DJperchange[0]
except: pass
try: print 'INDEXTICKER: SPquote - ' + SPquote[0]
except: pass
try: print 'INDEXTICKER: SPdelta - ' + SPdelta[0]
except: pass
try: print 'INDEXTICKER: SPperchange - ' + SPperchange[0]
except: pass
try: print 'INDEXTICKER: NQquote - ' + NQquote[0]
except: pass
try: print 'INDEXTICKER: NQdelta - ' + NQdelta[0]
except: pass
try: print 'INDEXTICKER: NQperchange - ' + NQperchange[0]
except: pass
# Parse Values
# Time
import time
Time = str(time.strftime ('%H:%M:%S%p'))
Date = str(time.strftime ('%m/%d/%Y'))
# print Date + Time
# Clean
TimeOQ = Date + ' ' + Time
try:
DJ = DJquote[0] + ' ' + DJdelta[0] + ' ' + DJperchange[0]
except:
print "INDEXTICKER: DJ concat failed"
pass
try:
SP = SPquote[0] + ' ' + SPdelta[0] + ' ' + SPperchange[0]
except:
print "INDEXTICKER: S&P concat failed"
pass
try:
NQ = NQquote[0] + ' ' + NQdelta[0] + ' ' + NQperchange[0]
except:
print "INDEXTICKER: NASDAQ concat failed"
pass
# DJUD = DJdelta[0].startswith("-")
try:
if DJdelta[0].startswith("-") :
DJUD='negative'
else :
DJUD=''
except:
print "INDEXTICKER: DJUD parse failed"
pass
try:
if SPdelta[0].startswith("-") :
SPUD='negative'
else :
SPUD=''
except:
print "INDEXTICKER: SPUD parse failed"
pass
try:
if NQdelta[0].startswith("-") :
NQUD='negative'
else :
NQUD=''
except:
print "INDEXTICKER: NQUD parse failed"
pass
# set properties
try:
self.WINDOW.setProperty( "TimeOQ" , str( TimeOQ ) or "" )
except:
print "INDEXTICKER: TimeOQ set property failed"
pass
try:
self.WINDOW.setProperty( "DJ" , str( DJ ) or "" )
except:
print "INDEXTICKER: DJ set property failed"
pass
try:
self.WINDOW.setProperty( "DJUD" , str( DJUD ) or "" )
except:
print "INDEXTICKER: DJUD set property failed"
pass
try:
self.WINDOW.setProperty( "SP" , str( SP ) or "" )
except:
print "INDEXTICKER: SP set property failed"
pass
try:
self.WINDOW.setProperty( "SPUD" , str( SPUD ) or "" )
except:
print "INDEXTICKER: SPUD set property failed"
pass
try:
self.WINDOW.setProperty( "NQ" , str( NQ ) or "" )
except:
print "INDEXTICKER: NQ set property failed"
pass
try:
self.WINDOW.setProperty( "NQUD" , str( NQUD ) or "" )
except:
print "INDEXTICKER: NQUD set property failed"
pass
if ( __name__ == "__main__" ):
Main()
# Skin Integration (required)
# I call the script with a custom Keymap.xml
## the skin code below will fade the panel out after 3 minutes.
#<!-- Stock Beta -->
# <control type="group">
# <visible>!IsEmpty(Window.Property(DJ))</visible>
# <animation effect="fade" time="1800" start="100" end="0" delay="60000" reversible="false" condition="!IsEmpty(Window.Property(DJ))">Conditional</animation>
# <animation effect="fade" time="60" start="0" end="100" reversible="false" condition="IsEmpty(Window.Property(DJ))">Conditional</animation>
# <include>Window_OpenClose_Animation</include>
#
# <control type="image">
# <description>Stock background image</description>
# <posx>320r</posx>
# <posy>522</posy>
# <width>500</width>
# <height>120</height>
# <texture>panel.png</texture>
# </control>
# <control type="label">
# <description>Stock Indexes</description>
# <posx>2r</posx>
# <posy>529</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-15</font>
# <textcolor>lightgreen</textcolor>
# <shadowcolor>44000000</shadowcolor>
# <!-- <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> -->
# <label>[B]Stock Indexes[/B][CR]Quote Time: $INFO[Window.Property(TimeOQ)]</label>
# </control>
# <control type="label">
# <description>Positive DJ</description>
# <posx>2r</posx>
# <posy>564</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>green</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>Dow: $INFO[Window.Property(DJ)]</label>
# <visible>IsEmpty(Window.Property(DJUD))</visible>
# </control>
# <control type="label">
# <description>Negative DJ</description>
# <posx>2r</posx>
# <posy>564</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>red</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>Dow: $INFO[Window.Property(DJ)]</label>
# <visible>!IsEmpty(Window.Property(DJUD))</visible>
# </control>
# <control type="label">
# <description>Positive SP</description>
# <posx>2r</posx>
# <posy>587</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>green</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>S&P: $INFO[Window.Property(SP)]</label>
# <visible>IsEmpty(Window.Property(SPUD))</visible>
# </control>
# <control type="label">
# <description>Negative SP</description>
# <posx>2r</posx>
# <posy>587</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>red</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>S&P: $INFO[Window.Property(SP)]</label>
# <visible>!IsEmpty(Window.Property(SPUD))</visible>
# </control>
# <control type="label">
# <description>Positive NQ</description>
# <posx>2r</posx>
# <posy>610</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>green</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>NASDAQ: $INFO[Window.Property(NQ)]</label>
# <visible>IsEmpty(Window.Property(NQUD))</visible>
# </control>
# <control type="label">
# <description>Negative NQ</description>
# <posx>2r</posx>
# <posy>610</posy>
# <width>500</width>
# <height>240</height>
# <align>right</align>
# <aligny>top</aligny>
# <font>font-19</font>
# <textcolor>red</textcolor>
# <shadowcolor>black</shadowcolor>
# <label>NASDAQ: $INFO[Window.Property(NQ)]</label>
# <visible>!IsEmpty(Window.Property(NQUD))</visible>
# </control>
# </control>