Improving my XML soccer parsing in python



I have something that kinda works but I know there's a much better way to do it somehow and I'd love to hear your opinion.


The below code checks a remote XML file every 5 seconds for x tries and looks for changes to the score in a particular game and outputs a message if the score has changed or not.


What's a better way to do this?



from xml.dom import minidom
import time
import urllib
import urllib2
import requests
url = 'http://ift.tt/1DUTuA9'
r = requests.get(url)
with open("test.xml", "wb") as code:
code.write(r.content)

#Config
curr_score = 0
playing = 'home'
team = 'Everton'

for x in xrange(1320):
xmldoc = minidom.parse('test.xml')
gameplayedat = xmldoc.getElementsByTagName(playing)

for s in gameplayedat :
if s.attributes['name'].value == team :
if s.attributes['goals'].value > curr_score :
print "Score increased " + " - " + s.attributes['goals'].value
curr_score = s.attributes['goals'].value
else:
print "Score the same " + " - " + s.attributes['goals'].value
curr_score = s.attributes['goals'].value
time.sleep(5)


The XML is laid out like this:



<livescore updated="19.01.2015 17:20:21" sport="soccer">
<league country="africa" name="Africa: Africa Cup Of Nations" cup="True" id="2892" sub_id="28920">
<match alternate_id="4085749" alternate_id_2="4295052" commentary="True" date="19.01.2015" id="4203316" static_id="19011523425322346965" status="62" time="16:00">
<home goals="1" id="2342532" name="Ghana"/>
<away goals="1" id="2346965" name="Senegal"/>
<events>
<event assist="" assistid="" extra_min="" id="21964024" minute="13" player="B. Coundoul" playerid="2338774" result="" team="away" type="yellowcard"/>
<event assist="" assistid="" extra_min="" id="21964025" minute="14" player="A. Ayew (pen.)" playerid="2352405" result="[1 - 0]" team="home" type="goal"/>
<event assist="" assistid="" extra_min="" id="21964026" minute="25" player="D. N'Doye" playerid="2350975" result="" team="away" type="yellowcard"/>
<event assist="D. N'Doye" assistid="19142" extra_min="" id="21964027" minute="58" player="M. Diouf" playerid="2360781" result="[1 - 1]" team="away" type="goal"/>
</events>
<ht score="[1-0]"/>
</match>
<match alternate_id="4085750" alternate_id_2="4295053" commentary="True" date="19.01.2015" id="4203317" static_id="19011523375672347374" status="19:00" time="19:00">
<home goals="?" id="2337567" name="Algeria"/>
<away goals="?" id="2347374" name="South Africa"/>
<events/>
<ht score=""/>
</match>
</league>
</livescore

No comments:

Post a Comment