Added gitignore
[vidyarank.git] / vidyarank / gameart.py
blobd4f7195171219a3ef05c5038cec6f0e41ddccbcb
1 #!/bin/env python
2 from urllib import request,parse
3 import xml.etree.ElementTree as ET
4 import re
5 """
6 Gets a gameart for selected game
7 """
9 def main():
10 """Main function"""
11 print(get("heroes of might and magic iii"))
13 def exact_name(name):
14 """Changes name of the game so it matches the database"""
15 name_url = "http://thegamesdb.net/api/GetGame.php?name={}".format(name)
16 data = openurl(name_url)
17 xmldoc = ET.parse(data)
18 try:
19 game = xmldoc.findall('Game')[0]
20 except IndexError:
21 return 'Wrong game'
22 exit()
23 title = game.find('GameTitle')
24 return title.text
26 def openurl(url):
27 """Opens url"""
28 req = request.Request(
29 url,
30 data=None,
31 headers={
32 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) ' \
33 ' AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
36 return request.urlopen(req)
38 def get(title):
39 """Opens freaking XML"""
40 while True:
41 try:
42 game_id = "http://thegamesdb.net/api/GetGame.php?exactname={}".format(parse.quote(title))
43 data = openurl(game_id)
44 xmldoc = ET.parse(data)
45 url = xmldoc.find('baseImgUrl').text
46 game = xmldoc.findall('Game')[0]
47 id_game = game.find('Images')
48 boxart = id_game.findall('boxart')
49 break
50 except IndexError:
51 title = exact_name(title)
52 for i in boxart:
53 if i.get('side') == 'front':
54 url += i.text
55 break
56 return url
57 #It could have been so simple with JSON...
58 if __name__ == '__main__':
59 main()