Ludum Dare Parse XML

script to parse recent comments on ld.

Looks at most recent blog posts via rss feed. Creates blog post of authors games.

Gets score out of 5.

create graphs of scores.


In [199]:
import requests
import xmltodict
import json
import random
import pandas
import bs4
import statistics

import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-199-d9b27802097b> in <module>()
      7 import statistics
      8 
----> 9 import matplotlib
     10 import numpy as np
     11 import matplotlib.pyplot as plt

ImportError: No module named 'matplotlib'

In [ ]:


In [146]:
ldreq = requests.get('http://ludumdare.com/compo/feed/')

In [381]:
ldxmd = xmltodict.parse(ldreq.text)

In [382]:
ldlen = ldxmd['rss']['channel']['item']

In [383]:
len(ldlen)


Out[383]:
20

In [384]:
ranld = random.randint(0, len(ldlen))

In [385]:
ldtit = ldlen[ranld]['title']

In [386]:
ldow = ldtit.lower()

In [387]:
undscr = ldow.replace(' ', '-')

In [388]:
undscr


Out[388]:
'7-ludum-dare-results-and-their-graphs'

In [389]:
lddes = ldlen[ranld]['description']

In [390]:
ldcret = ldlen[ranld]['dc:creator']

In [391]:
ldcate = ldlen[ranld]['category']

In [392]:
reqnot = requests.get('http://ldstats.info/api/authors/' + ldcret)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, **httplib_request_kw)
    330             try:  # Python 2.7+, use buffering of HTTP responses
--> 331                 httplib_response = conn.getresponse(buffering=True)
    332             except TypeError:  # Python 2.6 and older

TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-392-c674cb52efe0> in <module>()
----> 1 reqnot = requests.get('http://ldstats.info/api/authors/' + ldcret)

/usr/lib/python3/dist-packages/requests/api.py in get(url, **kwargs)
     58 
     59     kwargs.setdefault('allow_redirects', True)
---> 60     return request('get', url, **kwargs)
     61 
     62 

/usr/lib/python3/dist-packages/requests/api.py in request(method, url, **kwargs)
     47 
     48     session = sessions.Session()
---> 49     return session.request(method=method, url=url, **kwargs)
     50 
     51 

/usr/lib/python3/dist-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    455         }
    456         send_kwargs.update(settings)
--> 457         resp = self.send(prep, **send_kwargs)
    458 
    459         return resp

/usr/lib/python3/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    593 
    594         # Resolve redirects if allowed.
--> 595         history = [resp for resp in gen] if allow_redirects else []
    596 
    597         # Shuffle things around if there's history.

/usr/lib/python3/dist-packages/requests/sessions.py in <listcomp>(.0)
    593 
    594         # Resolve redirects if allowed.
--> 595         history = [resp for resp in gen] if allow_redirects else []
    596 
    597         # Shuffle things around if there's history.

/usr/lib/python3/dist-packages/requests/sessions.py in resolve_redirects(self, resp, req, stream, timeout, verify, cert, proxies)
    187                 cert=cert,
    188                 proxies=proxies,
--> 189                 allow_redirects=False,
    190             )
    191 

/usr/lib/python3/dist-packages/requests/sessions.py in send(self, request, **kwargs)
    567 
    568         # Send the request
--> 569         r = adapter.send(request, **kwargs)
    570 
    571         # Total elapsed time of the request (approximately)

/usr/lib/python3/dist-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    360                     decode_content=False,
    361                     retries=Retry(self.max_retries, read=False),
--> 362                     timeout=timeout
    363                 )
    364 

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, **response_kw)
    514             httplib_response = self._make_request(conn, method, url,
    515                                                   timeout=timeout,
--> 516                                                   body=body, headers=headers)
    517 
    518             # If we're going to release the connection in ``finally:``, then

/usr/lib/python3/dist-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, **httplib_request_kw)
    331                 httplib_response = conn.getresponse(buffering=True)
    332             except TypeError:  # Python 2.6 and older
--> 333                 httplib_response = conn.getresponse()
    334         except SocketTimeout:
    335             raise ReadTimeoutError(

/usr/lib/python3.4/http/client.py in getresponse(self)
   1170             response = self.response_class(self.sock, method=self._method)
   1171 
-> 1172         response.begin()
   1173         assert response.will_close != _UNKNOWN
   1174         self.__state = _CS_IDLE

/usr/lib/python3.4/http/client.py in begin(self)
    349         # read until we get a non-100 response
    350         while True:
--> 351             version, status, reason = self._read_status()
    352             if status != CONTINUE:
    353                 break

/usr/lib/python3.4/http/client.py in _read_status(self)
    311 
    312     def _read_status(self):
--> 313         line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
    314         if len(line) > _MAXLINE:
    315             raise LineTooLong("status line")

/usr/lib/python3.4/socket.py in readinto(self, b)
    369         while True:
    370             try:
--> 371                 return self._sock.recv_into(b)
    372             except timeout:
    373                 self._timeout_occurred = True

KeyboardInterrupt: 

In [393]:
notxt = reqnot.text

In [394]:
nojsn = json.loads(notxt)

In [395]:
nument = len(nojsn['entries'])

In [396]:
pernoj = nojsn['entries'][0]['percents']

In [397]:
gnamaut = list()

In [398]:
if 'overall' in pernoj.keys():
    print (pernoj.keys())


dict_keys(['audio', 'innovation', 'humor', 'theme', 'mood', 'fun', 'overall', 'graphics'])

In [ ]:


In [399]:
pandati = pandas.to_datetime(ldlen[ranld]['pubDate'])

In [400]:
str(pandati.date())


Out[400]:
'2015-09-16'

In [401]:
str(pandati.time())


Out[401]:
'06:04:29'

In [402]:
opemeta = open('/home/wcmckee/ldare/posts/' + undscr + '.meta', 'w')

In [403]:
opemeta.write(undscr + '\n' + ldtit + '\n' + str(pandati.date()) + ' ' + str(pandati.time()) + '\n')


Out[403]:
96

In [404]:
for cate in ldcate:
    opemeta.write(cate + ', ')

In [405]:
opemeta.close()

In [406]:
oprst = open('/home/wcmckee/ldare/posts/' + undscr + '.rst', 'w')

In [407]:
undscr


Out[407]:
'7-ludum-dare-results-and-their-graphs'

In [408]:
grfxp = list()

In [409]:
print (nojsn)


{'link': 'http://ludumdare.com/compo/author/dancingmad', 'entries': [{'link': 'http://ludumdare.com/compo/ludum-dare-33/?action=preview&uid=39644', 'percents': {'audio': 1.6, 'innovation': 0.4, 'humor': 0.9, 'theme': 0.3, 'mood': 1.9, 'fun': 1.5, 'overall': 0.6, 'graphics': 1.4}, 'type': 'jam', 'scores': {'audio': 4.22, 'innovation': 4.31, 'humor': 4.45, 'theme': 4.5, 'average': 4.35, 'mood': 4.23, 'fun': 4.07, 'overall': 4.33, 'graphics': 4.65}, 'ranking': {'audio': 24, 'innovation': 6, 'humor': 14, 'theme': 5, 'mood': 29, 'fun': 23, 'overall': 9, 'total': 1526, 'graphics': 21}, 'coolness': 100, 'ludum': 33, 'title': 'Internship Adventure'}, {'link': 'http://ludumdare.com/compo/ludum-dare-30/?action=preview&uid=39644', 'percents': {'innovation': 33, 'humor': 39.5, 'fun': 25.6, 'mood': 21.1, 'audio': 19.5, 'overall': 26.1, 'theme': 53.8, 'graphics': 24.3}, 'type': 'jam', 'scores': {'innovation': 3.11, 'humor': 2.35, 'fun': 3.24, 'mood': 3.39, 'average': 3.16, 'audio': 3.43, 'overall': 3.38, 'theme': 2.71, 'graphics': 3.69}, 'ranking': {'innovation': 345, 'humor': 413, 'fun': 268, 'mood': 221, 'audio': 204, 'overall': 273, 'total': 1045, 'theme': 562, 'graphics': 254}, 'coolness': 100, 'ludum': 30, 'title': 'Effraction'}], 'ldUser': 'dancingmad', 'ludums': [33, 30], 'ldUserId': '39644'}

In [410]:
outafiv = list()

In [448]:
dictgiv = dict()

In [453]:
for ojs in range(nument):
    gnamaut.append(nojsn['entries'][ojs]['title'] + '-' + ldcret)
    
    #print(nojsn['entries'][ojs]['link'])
    #print(nojsn['entries'][ojs]['percents'])
    #print(nojsn['entries'][ojs]['ranking'])
    #alpcnt = (nojsn['entries'][ojs]['percents'])
    #print(alpcnt.values())
    
    #alprm = alpcnt.items()
    
    #print(alpcnt)
    #print (lnoj)
    #print(pandas.read_table(lnoj))
    #lnoj = (nojsn['entries'][ojs]['scores'])
    totscor = (lnoj['graphics'] + lnoj['audio'] + lnoj['innovation'] + lnoj['fun']+
                   lnoj['mood'] + lnoj['humor'] + lnoj['theme'])
    
    outafiv.append(lnoj['graphics'])
    outafiv.append(lnoj['audio'])
    outafiv.append(lnoj['theme'])
    outafiv.append(lnoj['fun'])
    outafiv.append(lnoj['innovation'])
    outafiv.append(lnoj['mood'])
    dictgiv.update({'graphics' : lnoj['graphics'], 'audio' : lnoj['audio'], 'theme' : lnoj['theme'],
                   'mood' : lnoj['mood'], 'humor' : lnoj['humor'], 'theme' : lnoj['theme'],
                   'innovation' : lnoj['innovation']})
    
    #oprsi = open('/home/wcmckee/ldare/posts/' + (nojsn['entries'][ojs]['title'] + '-' + ldcret, 'w'))
    
    print(totscor)
    
    #grfxp.append(nojsn['entries'][ojs]['rankings']['graphics'])
    #grfxp.append(nojsn['entries'][ojs]['rankings']['audio'])
    #reqldg = requests.get(nojsn['entries'][ojs]['link'])
    #reqtx = bs4.BeautifulSoup(reqldg.text)
    
    #bstx = (reqtx.text)
    
    #print(reqtx.findAll('img'))
    
    #for ret in (reqtx.findAll('img')):
    #    print(ret)
    #    for re in ret:
    #        print(re)
    #        oprst.write(re)
    #        oprst.close()


21.92
21.92

In [457]:
for dig in dictgiv.items:
    print(dig)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-457-99203b3f922a> in <module>()
----> 1 for dig in dictgiv.items:
      2     print(dig)

TypeError: 'builtin_function_or_method' object is not iterable

In [470]:
patim = pandas.TimeSeries(dictgiv)

In [473]:
scordf = pandas.DataFrame(patim)

In [ ]:


In [462]:
dicitme = dictgiv.items()

In [465]:
dicitme


Out[465]:
dict_items([('audio', 3.43), ('innovation', 3.11), ('humor', 2.35), ('theme', 2.71), ('mood', 3.39), ('graphics', 3.69)])

In [455]:
outafiv


Out[455]:
[3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39,
 3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39,
 3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39,
 3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39,
 3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39,
 3.69,
 3.43,
 2.71,
 3.24,
 3.11,
 3.39]

In [ ]:


In [413]:
nodecl = list()

In [414]:
for outs in set(outafiv):
    print (str(outs)[0])
    nodecl.append((str(outs)[0]))

In [415]:
intlis = list()

In [416]:
for nod in nodecl:
    print(nod)
    intlis.append(int(nod))

In [ ]:


In [417]:
for intl in intlis:
    #print(intl)
    print(statistics.median_low(intlis))

In [418]:
intlis


Out[418]:
[]

In [419]:
math.pow(statistics.math.pi, )


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-419-10ac1d543f09> in <module>()
----> 1 math.pow(statistics.math.pi, )

TypeError: pow expected 2 arguments, got 1

In [420]:
statistics.mean(intlis)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-420-f62e95e79b80> in <module>()
----> 1 statistics.mean(intlis)

/usr/lib/python3.4/statistics.py in mean(data)
    290     n = len(data)
    291     if n < 1:
--> 292         raise StatisticsError('mean requires at least one data point')
    293     return _sum(data)/n
    294 

StatisticsError: mean requires at least one data point

In [421]:
statistics.math.pi


Out[421]:
3.141592653589793

In [422]:
statistics.median(intlis)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-422-39098213422f> in <module>()
----> 1 statistics.median(intlis)

/usr/lib/python3.4/statistics.py in median(data)
    311     n = len(data)
    312     if n == 0:
--> 313         raise StatisticsError("no median for empty data")
    314     if n%2 == 1:
    315         return data[n//2]

StatisticsError: no median for empty data

In [423]:
import numpy

In [424]:
import math

In [425]:
math.cos(stsgr)


Out[425]:
-0.9927997728658027

In [426]:
math.degrees(stsgr)


Out[426]:
186.87973417850353

In [427]:
math.radians(stsgr)


Out[427]:
0.056926822435881715

In [428]:
math.sin(stsgr)


Out[428]:
-0.1197856877828505

In [429]:
pandas.TimeSeries(outafiv)


Out[429]:
Series([], dtype: float64)

In [430]:
pandas.DataFrame(outafiv)


Out[430]:

In [ ]:


In [431]:
grfxp


Out[431]:
[]

In [432]:
stsgr = statistics.mean(outafiv)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-432-d35654123b9e> in <module>()
----> 1 stsgr = statistics.mean(outafiv)

/usr/lib/python3.4/statistics.py in mean(data)
    290     n = len(data)
    291     if n < 1:
--> 292         raise StatisticsError('mean requires at least one data point')
    293     return _sum(data)/n
    294 

StatisticsError: mean requires at least one data point

In [433]:
stsgr


Out[433]:
3.2616666666666667

In [434]:
stqgr = statistics.median(outafiv)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-434-8a4a0e26c0a1> in <module>()
----> 1 stqgr = statistics.median(outafiv)

/usr/lib/python3.4/statistics.py in median(data)
    311     n = len(data)
    312     if n == 0:
--> 313         raise StatisticsError("no median for empty data")
    314     if n%2 == 1:
    315         return data[n//2]

StatisticsError: no median for empty data

In [435]:
statistics.variance(grfxp)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-435-65bcb253967d> in <module>()
----> 1 statistics.variance(grfxp)

/usr/lib/python3.4/statistics.py in variance(data, xbar)
    511     n = len(data)
    512     if n < 2:
--> 513         raise StatisticsError('variance requires at least two data points')
    514     ss = _ss(data, xbar)
    515     return ss/(n-1)

StatisticsError: variance requires at least two data points

In [436]:
stqgr


Out[436]:
3.3150000000000004

In [437]:
statistics.stdev(grfxp)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-437-ee051eb36d15> in <module>()
----> 1 statistics.stdev(grfxp)

/usr/lib/python3.4/statistics.py in stdev(data, xbar)
    573 
    574     """
--> 575     var = variance(data, xbar)
    576     try:
    577         return var.sqrt()

/usr/lib/python3.4/statistics.py in variance(data, xbar)
    511     n = len(data)
    512     if n < 2:
--> 513         raise StatisticsError('variance requires at least two data points')
    514     ss = _ss(data, xbar)
    515     return ss/(n-1)

StatisticsError: variance requires at least two data points

In [438]:
grfxp


Out[438]:
[]

In [439]:
statistics.mode(grfxp)


---------------------------------------------------------------------------
StatisticsError                           Traceback (most recent call last)
<ipython-input-439-f944f51d9a6c> in <module>()
----> 1 statistics.mode(grfxp)

/usr/lib/python3.4/statistics.py in mode(data)
    435                 )
    436     else:
--> 437         raise StatisticsError('no mode for empty data')
    438 
    439 

StatisticsError: no mode for empty data

In [440]:
stqgr


Out[440]:
3.3150000000000004

In [476]:
scordf.to_html()


Out[476]:
'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>0</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>audio</th>\n      <td> 3.43</td>\n    </tr>\n    <tr>\n      <th>graphics</th>\n      <td> 3.69</td>\n    </tr>\n    <tr>\n      <th>humor</th>\n      <td> 2.35</td>\n    </tr>\n    <tr>\n      <th>innovation</th>\n      <td> 3.11</td>\n    </tr>\n    <tr>\n      <th>mood</th>\n      <td> 3.39</td>\n    </tr>\n    <tr>\n      <th>theme</th>\n      <td> 2.71</td>\n    </tr>\n  </tbody>\n</table>'

In [477]:
oprst.write(lddes + '\n\n' + str(totscor) + '\n\n' + scordf.to_html())


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-477-13160cdc9eca> in <module>()
----> 1 oprst.write(lddes + '\n\n' + str(totscor) + '\n\n' + scordf.to_html())

ValueError: I/O operation on closed file.

In [444]:
oprst.close()

In [ ]:


In [175]:
for gna in gnamaut:
    gnrep = gna.replace(' ', '-')
    gnlow = gnrep.lower()
    print(gnlow)
    wrmeta = open('/home/wcmckee/ldare/posts/' + gnlow + '.meta', 'w')
    wrmeta.write(gnlow + '\n' + gna + '\n' + str(pandati.date()) + ' ' + str(pandati.time()) + '\n')
    wrmeta.close()
    
    #wrst = open('/home/wcmckee/ldare/posts/' + gnlow + '.rst', 'w')
    #wrst.write('test')
    #wrst.close()


internship-adventure-dancingmad
effraction-dancingmad

In [ ]: