This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
price.py
127 lines (99 loc) · 3.29 KB
/
price.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
'''
prices.py - Willie Magic: The Gathering Price Lookup Module
A module for the IRC Bot Willie that allows a user to get the TCG Player
prices for cards.
Author: John Cleaver
License: BSD 3 Clause
'''
import requests
import xml.etree.ElementTree as ET
import sys
import argparse
import config
from unidecode import unidecode
get_vars = {"pk": config.tcgplayer_partner_code, "s": "", "p": ""}
def get_tcgplayer_price(card_name):
card_name = sanitize(card_name)
try:
tcgxml = get_tcgplayer_xml(card_name)
except requests.RequestException as e:
return u"TCGPlayer is either down or is having problems." \
+ u" " \
+ u"Try again later." \
+ u" " \
+ unicode(e)
else:
try:
if tcgxml == u"Product not found.":
raise CardNotFoundError(card_name)
tcgprice = parse_tcg_player_xml(card_name, tcgxml)
except CardNotFoundError as e:
return u"Could not find the card: " + unicode(e)
except ET.ParseError as e:
return u"TCGPlayer is not returning XML for that. Perhaps something went wrong?"
else:
return tcgprice
def get_tcgplayer_xml(card_name, url=config.tcgplayer_api_url):
""" Makes the API call and returns the resulting XML. """
if not url:
raise NoUrlException()
get_vars['p'] = unidecode(card_name)
r = requests.get(url, params=get_vars)
return r.text
def parse_tcg_player_xml(card_name, xml):
""" Converts the XML response from the API into an ordered dict. """
root = ET.fromstring(xml)
if root.text is None:
raise CardNotFoundError(card_name)
card = TCGPrice(card_name,
root[0][1].text,
root[0][2].text,
root[0][3].text,
root[0][4].text,
root[0][5].text)
return card
def sanitize(card_name):
return card_name
class TCGPrice():
def __init__(self, card, high, low, avg, foil, link):
self.card = card
self.high = high
self.low = low
self.avg = avg
self.foil = foil
self.link = link
def __unicode__(self):
return (self.card
+ u" | "
+ u"Avg: " + self.avg
+ u" | "
+ u"Low: " + self.low
+ u" | "
+ u"High: " + self.high
+ u" | "
+ u"Foil: " + self.foil
+ u" | "
+ u"Link: " + self.link)
def __iter__(self):
for attr, value in self.__dict__.iteritems():
yield attr, value
class NoUrlException(Exception):
def __str__(self):
return repr("""The TCG API URL is not present.
Enter it and reload the module.""")
class CardNotFoundError(Exception):
def __init__(self, card_name):
self.card_name = card_name
def __str__(self):
return self.card_name
def main(argv):
if not argv.card:
print "You must specify a card."
sys.exit()
else:
print(get_tcgplayer_price(" ".join(argv.card)))
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("card", nargs="+", help="The Card to find.")
args = parser.parse_args()
main(args)