From 5fd864718f4cd9e1e67835063a1efc24dd85e0c4 Mon Sep 17 00:00:00 2001 From: Eduardo Pedroni Date: Mon, 1 Jun 2015 12:36:28 +0200 Subject: Moved some things around, separated parsing into a different module to make way for the GUI --- cardbase.py | 153 +++++++++------------------------------------- cardparser.py | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++ fulltest.sh | 2 +- test_callow.py | 32 +++++----- test_cardclass.py | 7 ++- test_coecon.py | 34 +++++------ test_fetching.py | 17 ++++++ test_hydra.py | 34 +++++------ test_island.py | 34 +++++------ test_nightmare.py | 37 ++++++----- test_shivandragon.py | 44 ++++++-------- test_shoal.py | 36 +++++------ test_sorin.py | 33 +++++----- test_ugin.py | 37 ++++++----- 14 files changed, 375 insertions(+), 294 deletions(-) create mode 100644 cardparser.py create mode 100755 test_fetching.py diff --git a/cardbase.py b/cardbase.py index be5a3b8..c4bc17e 100755 --- a/cardbase.py +++ b/cardbase.py @@ -1,132 +1,37 @@ -import re +#!/usr/bin/env python3 -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 +import requests +import sys +from lxml import html -def makeUrl(cardSet, cardNo): - return "http://magiccards.info/" + cardSet + "/en/" + cardNo + ".html" +database = "" -def getTitle(page): - return page.xpath("/html/body/table[3]/tr/td[2]/span/a/text()")[0] +def parseInput(raw): + if raw == "help": + print("Need help? too bad") + elif raw == " -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 main(args): -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) - colours = re.sub("[\{\}/]*", "", colours) - - return colours - - else: - return "" - -def getType(page): - types = extractSubTitle(page) - types = re.search("([A-Za-z ]*)( —)?", types).group(1).strip() - - return types + try: + database = open(args[1], "w") + except: + print("Please provide a valid database file as the first argument.") + sys.exit(1) -def getSubType(page): - subtypes = extractSubTitle(page) - subtypes = re.search("— ([A-Za-z ]*)", subtypes) - - if subtypes: - return subtypes.group(1).strip() - else: - return "" + print("Welcome to cardbase") + print("For a list of commands, type \"help\"") -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: - flavour = re.sub("\n", "", " ".join(flavour)) - return flavour - 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 "" - + exit = False + while(not exit): + try: + raw = input("> ") + parseInput(raw) + except: + exit = True + + +# The entry point +if __name__ == "__main__": + main(sys.argv) diff --git a/cardparser.py b/cardparser.py new file mode 100644 index 0000000..759ad47 --- /dev/null +++ b/cardparser.py @@ -0,0 +1,169 @@ +import re +from lxml import html + +class Card(): + def __init__(self): + self.title = "" + self.cost = "" + self.convertedCost = "" + 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 = "" + +# fetching functions +def makeUrl(cardSet, cardNo): + return "http://magiccards.info/" + cardSet + "/en/" + cardNo + ".html" + +def remoteFetch(url): + return requests.get(url).text + +def fetchCard(cardSet, cardNo): + # build object + card = Card() + card.edition = cardSet + card.scan = "http://magiccards.info/scans/en/" + cardSet + "/" + cardNo + ".jpg" + card.number = cardNo + + #setRemoteData(card, makeUrl(cardSet, cardNo)) + + return card + +def setRemoteData(card, url, fetchCallback=remoteFetch): + # fetch card from upstream + page = html.fromstring(fetchCallback(url)) + + # set remote data + card.title = getTitle(page) + card.cost = getCost(page) + card.convertedCost = getConvertedCost(page) + card.colour = getColour(page) + card.type = getType(page) + card.subtype = getSubType(page) + card.artist = getArtist(page) + card.text = getText(page) + card.flavour = getFlavour(page) + card.rarity = getRarity(page) + card.power = getPower(page) + card.toughness = getToughness(page) + card.loyalty = getLoyalty(page) + +# parsing functions +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 getConvertedCost(page): + cost = extractSubTitle(page) + cost = re.search("\(([0-9+])\)", 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) + colours = re.sub("[\{\}/]*", "", colours) + + return colours + + else: + return "" + +def getType(page): + types = extractSubTitle(page) + types = re.search("([A-Za-z ]*)( —)?", types).group(1).strip() + + 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: + flavour = re.sub("\n", "", " ".join(flavour)) + return flavour + 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 "" diff --git a/fulltest.sh b/fulltest.sh index 67a3dc6..ac01b15 100755 --- a/fulltest.sh +++ b/fulltest.sh @@ -1,3 +1,3 @@ #!/usr/bin/env sh -python -m unittest test_shivandragon test_callow test_cardclass test_sorin test_island test_shoal test_coecon test_hydra test_nightmare test_ugin +python -m unittest test_cardclass test_shivandragon test_callow test_sorin test_island test_shoal test_coecon test_hydra test_nightmare test_ugin diff --git a/test_callow.py b/test_callow.py index 452029c..ab6c67e 100755 --- a/test_callow.py +++ b/test_callow.py @@ -1,9 +1,8 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html +import cardparser class Test_cardInformationParsing(unittest.TestCase): @@ -14,46 +13,49 @@ class Test_cardInformationParsing(unittest.TestCase): # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Callow Jushi") + self.assertEqual(cardparser.getTitle(self.page), "Callow Jushi") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "1UU") + self.assertEqual(cardparser.getCost(self.page), "1UU") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "3") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "U") + self.assertEqual(cardparser.getColour(self.page), "U") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Creature") + self.assertEqual(cardparser.getType(self.page), "Creature") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Human Wizard") + self.assertEqual(cardparser.getSubType(self.page), "Human Wizard") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Tsutomu Kawade") + self.assertEqual(cardparser.getArtist(self.page), "Tsutomu Kawade") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["Whenever you cast a Spirit or Arcane spell, you may put a ki counter on Callow Jushi.", "At the beginning of the end step, if there are two or more ki counters on Callow Jushi, you may flip it."]) + self.assertEqual(cardparser.getText(self.page), ["Whenever you cast a Spirit or Arcane spell, you may put a ki counter on Callow Jushi.", "At the beginning of the end step, if there are two or more ki counters on Callow Jushi, you may flip it."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "") + self.assertEqual(cardparser.getFlavour(self.page), "") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Uncommon") + self.assertEqual(cardparser.getRarity(self.page), "Uncommon") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "2") + self.assertEqual(cardparser.getPower(self.page), "2") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "2") + self.assertEqual(cardparser.getToughness(self.page), "2") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("bok", "31a") + cls.card = cardparser.fetchCard("bok", "31a") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "bok") diff --git a/test_cardclass.py b/test_cardclass.py index fc17d62..409bff4 100755 --- a/test_cardclass.py +++ b/test_cardclass.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 import unittest -import cardbase +import cardparser class Test_cardClass(unittest.TestCase): def setUp(self): - self.card = cardbase.Card() + self.card = cardparser.Card() # Tests def test_cardHasTitle(self): @@ -14,6 +14,9 @@ class Test_cardClass(unittest.TestCase): def test_cardHasCost(self): self.assertIsNotNone(self.card.cost) + + def test_cardHasConvertedCost(self): + self.assertIsNotNone(self.card.convertedCost) def test_cardHasColour(self): self.assertIsNotNone(self.card.colour) diff --git a/test_coecon.py b/test_coecon.py index 2d22be5..8c1dac8 100755 --- a/test_coecon.py +++ b/test_coecon.py @@ -1,61 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/coecon", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Coerced Confession") + self.assertEqual(cardparser.getTitle(self.page), "Coerced Confession") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "4{U/B}") + self.assertEqual(cardparser.getCost(self.page), "4{U/B}") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "5") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "UB") + self.assertEqual(cardparser.getColour(self.page), "UB") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Sorcery") + self.assertEqual(cardparser.getType(self.page), "Sorcery") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "") + self.assertEqual(cardparser.getSubType(self.page), "") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Mathias Kollros") + self.assertEqual(cardparser.getArtist(self.page), "Mathias Kollros") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["Target player puts the top four cards of his or her library into his or her graveyard. You draw a card for each creature card put into that graveyard this way."]) + self.assertEqual(cardparser.getText(self.page), ["Target player puts the top four cards of his or her library into his or her graveyard. You draw a card for each creature card put into that graveyard this way."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "\"Ask the right questions in the right way and truth is inevitable.\" —Lazav") + self.assertEqual(cardparser.getFlavour(self.page), "\"Ask the right questions in the right way and truth is inevitable.\" —Lazav") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Uncommon") + self.assertEqual(cardparser.getRarity(self.page), "Uncommon") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "") + self.assertEqual(cardparser.getPower(self.page), "") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "") + self.assertEqual(cardparser.getToughness(self.page), "") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("gtc", "217") + cls.card = cardparser.fetchCard("gtc", "217") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "gtc") diff --git a/test_fetching.py b/test_fetching.py new file mode 100755 index 0000000..bf17a22 --- /dev/null +++ b/test_fetching.py @@ -0,0 +1,17 @@ +#!/usr/bin/env python3 + +import unittest +import cardparser + +class Test_cardPageFetching(unittest.TestCase): + + # Tests + def test_correctUrlIsBuilt(self): + self.assertEqual(cardparser.makeUrl("set", "number"), "http://magiccards.info/set/en/number.html") + +def test(): + unittest.main(exit=False) + +# The entry point +if __name__ == "__main__": + test() diff --git a/test_hydra.py b/test_hydra.py index 1fbaa9f..4853715 100755 --- a/test_hydra.py +++ b/test_hydra.py @@ -1,61 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/hydra", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Khalni Hydra") + self.assertEqual(cardparser.getTitle(self.page), "Khalni Hydra") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "GGGGGGGG") + self.assertEqual(cardparser.getCost(self.page), "GGGGGGGG") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "8") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "G") + self.assertEqual(cardparser.getColour(self.page), "G") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Creature") + self.assertEqual(cardparser.getType(self.page), "Creature") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Hydra") + self.assertEqual(cardparser.getSubType(self.page), "Hydra") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Todd Lockwood") + self.assertEqual(cardparser.getArtist(self.page), "Todd Lockwood") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["Khalni Hydra costs {G} less to cast for each green creature you control.", "Trample"]) + self.assertEqual(cardparser.getText(self.page), ["Khalni Hydra costs {G} less to cast for each green creature you control.", "Trample"]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "\"In ages past, bargains were struck and promises were made. Now we must collect on our debt. Begin the hymns.\" —Moruul, Khalni druid") + self.assertEqual(cardparser.getFlavour(self.page), "\"In ages past, bargains were struck and promises were made. Now we must collect on our debt. Begin the hymns.\" —Moruul, Khalni druid") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Mythic Rare") + self.assertEqual(cardparser.getRarity(self.page), "Mythic Rare") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "8") + self.assertEqual(cardparser.getPower(self.page), "8") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "8") + self.assertEqual(cardparser.getToughness(self.page), "8") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("roe", "192") + cls.card = cardparser.fetchCard("roe", "192") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "roe") diff --git a/test_island.py b/test_island.py index 44641cb..0710949 100755 --- a/test_island.py +++ b/test_island.py @@ -1,61 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/island", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Island") + self.assertEqual(cardparser.getTitle(self.page), "Island") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "") + self.assertEqual(cardparser.getCost(self.page), "") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "") + self.assertEqual(cardparser.getColour(self.page), "") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Basic Land") + self.assertEqual(cardparser.getType(self.page), "Basic Land") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Island") + self.assertEqual(cardparser.getSubType(self.page), "Island") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Florian de Gesincourt") + self.assertEqual(cardparser.getArtist(self.page), "Florian de Gesincourt") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["({T}: Add {U} to your mana pool.)"]) + self.assertEqual(cardparser.getText(self.page), ["({T}: Add {U} to your mana pool.)"]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "") + self.assertEqual(cardparser.getFlavour(self.page), "") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Land") + self.assertEqual(cardparser.getRarity(self.page), "Land") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "") + self.assertEqual(cardparser.getPower(self.page), "") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "") + self.assertEqual(cardparser.getToughness(self.page), "") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("dtk", "253") + cls.card = cardparser.fetchCard("dtk", "253") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "dtk") diff --git a/test_nightmare.py b/test_nightmare.py index 24a6581..befcc6f 100755 --- a/test_nightmare.py +++ b/test_nightmare.py @@ -1,64 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # fetch shivan dragon info by the card's collector number (281 in M15) - # cls.page = html.fromstring(requests.get("http://magiccards.info/m15/en/281.html").text) - - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/nightmare", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Nightmare") + self.assertEqual(cardparser.getTitle(self.page), "Nightmare") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "5B") + self.assertEqual(cardparser.getCost(self.page), "5B") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "6") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "B") + self.assertEqual(cardparser.getColour(self.page), "B") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Creature") + self.assertEqual(cardparser.getType(self.page), "Creature") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Nightmare Horse") + self.assertEqual(cardparser.getSubType(self.page), "Nightmare Horse") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Vance Kovacs") + self.assertEqual(cardparser.getArtist(self.page), "Vance Kovacs") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["Flying (This creature can't be blocked except by creatures with flying or reach.)", "Nightmare's power and toughness are each equal to the number of Swamps you control."]) + self.assertEqual(cardparser.getText(self.page), ["Flying (This creature can't be blocked except by creatures with flying or reach.)", "Nightmare's power and toughness are each equal to the number of Swamps you control."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "The thunder of its hooves beats dreams into despair.") + self.assertEqual(cardparser.getFlavour(self.page), "The thunder of its hooves beats dreams into despair.") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Rare") + self.assertEqual(cardparser.getRarity(self.page), "Rare") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "*") + self.assertEqual(cardparser.getPower(self.page), "*") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "*") + self.assertEqual(cardparser.getToughness(self.page), "*") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("m15", "276") + cls.card = cardparser.fetchCard("m15", "276") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "m15") diff --git a/test_shivandragon.py b/test_shivandragon.py index 4f003e6..0c73426 100755 --- a/test_shivandragon.py +++ b/test_shivandragon.py @@ -1,64 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # fetch shivan dragon info by the card's collector number (281 in M15) - # cls.page = html.fromstring(requests.get("http://magiccards.info/m15/en/281.html").text) - - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/shivandragon", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Shivan Dragon") + self.assertEqual(cardparser.getTitle(self.page), "Shivan Dragon") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "4RR") + self.assertEqual(cardparser.getCost(self.page), "4RR") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "6") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "R") + self.assertEqual(cardparser.getColour(self.page), "R") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Creature") + self.assertEqual(cardparser.getType(self.page), "Creature") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Dragon") + self.assertEqual(cardparser.getSubType(self.page), "Dragon") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Donato Giancola") + self.assertEqual(cardparser.getArtist(self.page), "Donato Giancola") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["Flying (This creature can't be blocked except by creatures with flying or reach.)", "{R}: Shivan Dragon gets +1/+0 until end of turn."]) + self.assertEqual(cardparser.getText(self.page), ["Flying (This creature can't be blocked except by creatures with flying or reach.)", "{R}: Shivan Dragon gets +1/+0 until end of turn."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "The undisputed master of the mountains of Shiv.") + self.assertEqual(cardparser.getFlavour(self.page), "The undisputed master of the mountains of Shiv.") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Rare") + self.assertEqual(cardparser.getRarity(self.page), "Rare") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "5") + self.assertEqual(cardparser.getPower(self.page), "5") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "5") + self.assertEqual(cardparser.getToughness(self.page), "5") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("m15", "281") + cls.card = cardparser.fetchCard("m15", "281") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "m15") @@ -68,13 +65,6 @@ class Test_additionalCardData(unittest.TestCase): def test_cardHasCorrectNumber(self): self.assertEqual(self.card.number, "281") - - -class Test_cardPageFetching(unittest.TestCase): - - # Tests - def test_correctUrlIsBuilt(self): - self.assertEqual(cardbase.makeUrl("m15", "281"), "http://magiccards.info/m15/en/281.html") def test(): unittest.main(exit=False) diff --git a/test_shoal.py b/test_shoal.py index 610ce4b..8359bce 100755 --- a/test_shoal.py +++ b/test_shoal.py @@ -1,61 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod - def setUpClass(cls): - # but actually, use the pre-fetched file to avoid querying the server too much + def setUpClass(cls): with open("testcards/shoal", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Disrupting Shoal") + self.assertEqual(cardparser.getTitle(self.page), "Disrupting Shoal") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "XUU") + self.assertEqual(cardparser.getCost(self.page), "XUU") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "2") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "U") + self.assertEqual(cardparser.getColour(self.page), "U") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Instant") + self.assertEqual(cardparser.getType(self.page), "Instant") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Arcane") + self.assertEqual(cardparser.getSubType(self.page), "Arcane") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Scott M. Fischer") + self.assertEqual(cardparser.getArtist(self.page), "Scott M. Fischer") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.", "Counter target spell if its converted mana cost is X."]) + self.assertEqual(cardparser.getText(self.page), ["You may exile a blue card with converted mana cost X from your hand rather than pay Disrupting Shoal's mana cost.", "Counter target spell if its converted mana cost is X."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "") + self.assertEqual(cardparser.getFlavour(self.page), "") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Rare") + self.assertEqual(cardparser.getRarity(self.page), "Rare") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "") + self.assertEqual(cardparser.getPower(self.page), "") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "") + self.assertEqual(cardparser.getToughness(self.page), "") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("bok", "33") + cls.card = cardparser.fetchCard("bok", "33") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "bok") diff --git a/test_sorin.py b/test_sorin.py index 05192f8..905ca9e 100755 --- a/test_sorin.py +++ b/test_sorin.py @@ -1,10 +1,8 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @@ -15,46 +13,49 @@ class Test_cardInformationParsing(unittest.TestCase): # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Sorin Markov") + self.assertEqual(cardparser.getTitle(self.page), "Sorin Markov") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "3BBB") + self.assertEqual(cardparser.getCost(self.page), "3BBB") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "6") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "B") + self.assertEqual(cardparser.getColour(self.page), "B") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Planeswalker") + self.assertEqual(cardparser.getType(self.page), "Planeswalker") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Sorin") + self.assertEqual(cardparser.getSubType(self.page), "Sorin") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Michael Komarck") + self.assertEqual(cardparser.getArtist(self.page), "Michael Komarck") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["+2: Sorin Markov deals 2 damage to target creature or player and you gain 2 life.", "−3: Target opponent's life total becomes 10.", "−7: You control target player during that player's next turn."]) + self.assertEqual(cardparser.getText(self.page), ["+2: Sorin Markov deals 2 damage to target creature or player and you gain 2 life.", "−3: Target opponent's life total becomes 10.", "−7: You control target player during that player's next turn."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "") + self.assertEqual(cardparser.getFlavour(self.page), "") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Mythic Rare") + self.assertEqual(cardparser.getRarity(self.page), "Mythic Rare") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "") + self.assertEqual(cardparser.getPower(self.page), "") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "") + self.assertEqual(cardparser.getToughness(self.page), "") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "4") + self.assertEqual(cardparser.getLoyalty(self.page), "4") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("m12", "109") + cls.card = cardparser.fetchCard("m12", "109") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "m12") diff --git a/test_ugin.py b/test_ugin.py index 948d341..153e226 100755 --- a/test_ugin.py +++ b/test_ugin.py @@ -1,64 +1,61 @@ #!/usr/bin/env python3 -import sys import unittest -import cardbase from lxml import html - +import cardparser class Test_cardInformationParsing(unittest.TestCase): @classmethod def setUpClass(cls): - # fetch shivan dragon info by the card's collector number (281 in M15) - # cls.page = html.fromstring(requests.get("http://magiccards.info/m15/en/281.html").text) - - # but actually, use the pre-fetched file to avoid querying the server too much with open("testcards/ugincons", "r") as file: cls.page = html.fromstring(file.read()) # Tests def test_correctTitleIsParsed(self): - self.assertEqual(cardbase.getTitle(self.page), "Ugin's Construct") + self.assertEqual(cardparser.getTitle(self.page), "Ugin's Construct") def test_correctCostIsParsed(self): - self.assertEqual(cardbase.getCost(self.page), "4") + self.assertEqual(cardparser.getCost(self.page), "4") + + def test_correctConvertedCostIsParsed(self): + self.assertEqual(cardparser.getConvertedCost(self.page), "4") def test_correctColourIsParsed(self): - self.assertEqual(cardbase.getColour(self.page), "") + self.assertEqual(cardparser.getColour(self.page), "") def test_correctTypeIsParsed(self): - self.assertEqual(cardbase.getType(self.page), "Artifact Creature") + self.assertEqual(cardparser.getType(self.page), "Artifact Creature") def test_correctSubTypeIsParsed(self): - self.assertEqual(cardbase.getSubType(self.page), "Construct") + self.assertEqual(cardparser.getSubType(self.page), "Construct") def test_correctArtistIsParsed(self): - self.assertEqual(cardbase.getArtist(self.page), "Peter Mohrbacher") + self.assertEqual(cardparser.getArtist(self.page), "Peter Mohrbacher") def test_correctTextIsParsed(self): - self.assertEqual(cardbase.getText(self.page), ["When Ugin's Construct enters the battlefield, sacrifice a permanent that's one or more colors."]) + self.assertEqual(cardparser.getText(self.page), ["When Ugin's Construct enters the battlefield, sacrifice a permanent that's one or more colors."]) def test_correctFlavourIsParsed(self): - self.assertEqual(cardbase.getFlavour(self.page), "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.") + self.assertEqual(cardparser.getFlavour(self.page), "While trapping the Eldrazi on Zendikar, Ugin learned little from Sorin, but he gleaned the rudiments of lithomancy from Nahiri.") def test_correctRarityIsParsed(self): - self.assertEqual(cardbase.getRarity(self.page), "Uncommon") + self.assertEqual(cardparser.getRarity(self.page), "Uncommon") def test_correctPowerIsParsed(self): - self.assertEqual(cardbase.getPower(self.page), "4") + self.assertEqual(cardparser.getPower(self.page), "4") def test_correctToughnessIsParsed(self): - self.assertEqual(cardbase.getToughness(self.page), "5") + self.assertEqual(cardparser.getToughness(self.page), "5") def test_correctLoyaltyIsParsed(self): - self.assertEqual(cardbase.getLoyalty(self.page), "") + self.assertEqual(cardparser.getLoyalty(self.page), "") class Test_additionalCardData(unittest.TestCase): @classmethod def setUpClass(cls): - cls.card = cardbase.fetchCard("frf", "164") + cls.card = cardparser.fetchCard("frf", "164") def test_cardHasCorrectEdition(self): self.assertEqual(self.card.edition, "frf") -- cgit v1.2.3