aboutsummaryrefslogtreecommitdiffstats
path: root/cardbase.py
blob: b6942ee67c0f209da7a41960dbce43481e36fd94 (plain)
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
128
129
130
import re

class Card():
    def __init__(self):
        self.title = ""
        self.cost = ""
        self.colour = ""
        self.type = ""
        self.subtype = ""
        self.edition = ""
        self.scan = ""
        self.artist = ""
        self.text = ""
        self.flavour = ""
        self.rarity = ""
        self.number = ""
        self.power = ""
        self.toughness = ""
        self.loyalty = ""
        
def fetchCard(cardSet, cardNo):
    card = Card()
    card.edition = cardSet
    card.scan = "http://magiccards.info/scans/en/" + cardSet + "/" + cardNo + ".jpg"
    card.number = cardNo
    return card

def makeUrl(cardSet, cardNo):
    return "http://magiccards.info/" + cardSet + "/en/" + cardNo + ".html"

def getTitle(page):
    return page.xpath("/html/body/table[3]/tr/td[2]/span/a/text()")[0]

def extractSubTitle(page):
    line = page.xpath("/html/body/table[3]/tr/td[2]/p[1]/text()")[0]
    line = re.sub("\n", "", line)
    line = re.sub(" +", " ", line)
    return line.strip()

def getCost(page):
    cost = extractSubTitle(page)
    cost = re.search(" ([0-9X]*[WGRBU]*) ", cost)
    
    if cost:
        return cost.group(1)
    else:
        return ""
    
def getColour(page):
    colours = extractSubTitle(page)
    colours = re.search(" [0-9X]*([WGRBU]*) ", colours)
    if colours:
        colours = colours.group(1)
        
        colours = re.sub("U+", "U", colours)
        colours = re.sub("W+", "W", colours)
        colours = re.sub("R+", "R", colours)
        colours = re.sub("B+", "B", colours)
        colours = re.sub("G+", "G", colours)
        
        return colours
        
    else:
        return ""
    
def getType(page):
    types = extractSubTitle(page)
    types = re.search("([A-Za-z ]*) —", types).group(1)
    
    return types

def getSubType(page):
    subtypes = extractSubTitle(page)
    subtypes = re.search("— ([A-Za-z ]*)", subtypes)
    
    if subtypes:
        return subtypes.group(1).strip()
    else:
        return ""

def getArtist(page):
    artist = page.xpath("/html/body/table[3]/tr/td[2]/p[4]/text()")[0]
    artist = re.sub("Illus. ", "", artist)
    return artist
    
def getText(page):
    text = page.xpath("/html/body/table[3]/tr/td[2]/p[2]/b/text()")
    return text
    
def getFlavour(page):
    flavour = page.xpath("/html/body/table[3]/tr/td[2]/p[3]/i/text()")
    if flavour:
        return flavour[0]
    else:
        return ""
    
def getRarity(page):
    rarity = page.xpath("/html/body/table[3]/tr/td[3]/small/b[2]/text()")[0]
    rarity = re.search("\(([A-Za-z ]*)\)", rarity).group(1)
    
    return rarity
    
def getPower(page):
    power = extractSubTitle(page)
    power = re.search("([0-9X\*]+)/[0-9X\*]+", power)
    
    if power:
        return power.group(1)
    else:
        return ""
    
def getToughness(page):
    toughness = extractSubTitle(page)
    toughness = re.search("[0-9X\*]+/([0-9X\*]+)", toughness)
    
    if toughness:
        return toughness.group(1)
    else:
        return ""
    
def getLoyalty(page):
    loyalty = extractSubTitle(page)
    loyalty = re.search("\(Loyalty: ([0-9X*]+)\)", loyalty)
    
    if loyalty:
        return loyalty.group(1)
    else:
        return ""