diff --git a/res/cardsScript.py b/res/cardsScript.py index d671336c180..eb343663fcc 100644 --- a/res/cardsScript.py +++ b/res/cardsScript.py @@ -1,40 +1,14 @@ -# This script parses through cards.txt and makes sure there are matching entries in card-pictures.txt and the different rarity files -# It appends any new entries to the bottom of the current lists. I(Sol) can fix this later -# Check for errors in cardsScript.log - -# To Install Python check out: http://www.python.org/download/ The latest Python 2 should do the trick (2.7 as of July 27, 2010) -# Once installed, just run the script from the location. Double Click on PC. Or run through a command prompt. - +# This python script is designed to handle the following: individual cards located in /res/cardsfolder +# Insert of rarity, picture, and sets of from httplib import HTTP from urlparse import urlparse from urllib import urlopen +import os def clean(name): return name.lower().replace('-',' ').replace(',','').replace('_', ' ').replace('\'', '').replace('\"', '').replace('.', '').strip() - -def getRarity(fileName, cardDict, quest): - file = open(fileName) - line = clean(file.readline()) - while line != '': - if cardDict.has_key(line): - if quest: - cardDict[line].qRarity = True - else: - cardDict[line].rarity = True - line = clean(file.readline()) - file.close() - -def writeRarity(fileName, card): - if card.rarity == False: - rarity = open(fileName, 'a') - rarity.write(card.name+'\n') - rarity.close() - if card.qRarity == False: - rarity = open("quest/"+fileName, 'a') - rarity.write(card.name+'\n') - rarity.close() - + def checkURL(url): p = urlparse(url) h = HTTP(p[1]) @@ -45,93 +19,261 @@ def checkURL(url): def getURL(url): return urlopen(url).read() + +def getSVarString(line, str): + start = line.find(str) + len(str) + 1 + return line[start:].strip() +def getRarity(name, html): + # magiccards.info uses () on the page for rarity. + # since older editions had funky things like c2, and u1, only use open parenthesis for this search + landCount = html.count("(Land") + commonCount = html.count("(Common") + uncommonCount = html.count("(Uncommon") + rareCount = html.count("(Rare") + mythicCount = html.count("(Mythic Rare)") + if (landCount > 0): + return 'Land' + elif (commonCount + uncommonCount + rareCount + mythicCount == 0): + err.write("Bad magiccards.info Query: " + name + '\n') + return '' + elif (commonCount >= uncommonCount and commonCount >= rareCount and commonCount >= mythicCount): + return 'Common' + elif (commonCount < uncommonCount and uncommonCount >= rareCount and uncommonCount >= mythicCount): + return 'Uncommon' + elif (rareCount >= mythicCount): + return 'Rare' + else: + return 'Mythic' + +def getPicture(name): + urlName = name.replace(' ', '_') + picUrl ='http://www.wizards.com/global/images/magic/general/' + urlName + '.jpg' + if not checkURL(picUrl): + err.write("Bad Picture URL " + name + '\n') + return '' + return picUrl + +def getSets(name, html): + delimitedStr = '' + first = True; + for s in sets: + if (html.find(s + ' (') != -1) or (html.find(s + ' (') != -1): + if not first: + delimitedStr = delimitedStr + ',' + first = False + delimitedStr = delimitedStr + sets[s] + + return delimitedStr + +def initSets(): + # Base Sets + sets['Limited Edition Beta'] = 'LEB' + sets['Unlimited Edition'] = '2ED' + sets['Revised Edition'] = '3ED' + sets['Fourth Edition'] = '4ED' + sets['Fifth Edition'] = '5ED' + sets['Classic Sixth Edition'] = '6ED' + sets['Seventh Edition'] = '7ED' + sets['Eighth Edition'] = '8ED' + sets['Ninth Edition'] = '9ED' + sets['Tenth Edition'] = '10E' + sets['Magic 2010'] = 'M10' + sets['Magic 2011'] = 'M11' + + # Portal + sets['Portal'] = 'POR' + sets['Portal Second Age'] = 'P02' + sets['Portal Three Kingdoms'] = 'PTK' + + # Starter + sets['Starter 1999'] = 'S99' + sets['Starter 2000'] = 'S00' + + # Early sets + sets['Arabian Nights'] = 'ARN' + sets['Antiquities'] = 'ATQ' + sets['Legends'] = 'LEG' + sets['The Dark'] = 'DRK' + sets['Fallen Empires'] = 'FEM' + sets['Homelands'] = 'HML' + + # Ice Age + sets['Ice Age'] = 'ICE' + sets['Alliances'] = 'ALL' + sets['Coldsnap'] = 'CSP' + + # Mirage + sets['Mirage'] = 'MIR' + sets['Visions'] = 'VIS' + sets['Weatherlight'] = 'WTH' + + # Rath Cycle + sets['Tempest'] = 'TMP' + sets['Stronghold'] = 'STH' + sets['Exodus'] = 'EXO' + + # Artifacts Cycle + sets['Urza\'s Saga'] = 'USG' + sets['Urza\'s Legacy'] = 'ULG' + sets['Urza\'s Destiny'] = 'UDS' + + # Masques + sets['Mercadian Masques'] = 'MMQ' + sets['Nemesis'] = 'NMS' + sets['Prophecy'] = 'PCY' + + # Invasion + sets['Invasion'] = 'INV' + sets['Planeshift'] = 'PLS' + sets['Apocalypse'] = 'APC' + + # Odyssey + sets['Odyssey'] = 'ODY' + sets['Torment'] = 'TOR' + sets['Judgment'] = 'JUD' + + # Onslaught + sets['Onslaught'] = 'ONS' + sets['Legions'] = 'LGN' + sets['Scourge'] = 'SCG' + + # Mirrodin + sets['Mirrodin'] = 'MRD' + sets['Darksteel'] = 'DST' + sets['Fifth Dawn'] = '5DN' + + # Kamigawa + sets['Champions of Kamigawa'] = 'CHK' + sets['Betrayers of Kamigawa'] = 'BOK' + sets['Saviors of Kamigawa'] = 'SOK' + + # Ravnica + sets['Ravnica: City of Guilds'] = 'RAV' + sets['Guildpact'] = 'GPT' + sets['Dissension'] = 'DIS' + + # Time Spiral + sets['Time Spiral'] = 'TSP' + sets['Planar Chaos'] = 'PLC' + sets['Future Sight'] = 'FUT' + + # Lorwyn + sets['Lorwyn'] = 'LRW' + sets['Morningtide'] = 'MOR' + + # Shadowmoor + sets['Shadowmoor'] = 'SHM' + sets['Eventide'] = 'EVE' + + # Alara + sets['Shards of Alara'] = 'ALA' + sets['Conflux'] = 'CON' + sets['Alara Reborn'] = 'ARB' + + # Zendikar + sets['Zendikar'] = 'ZEN' + sets['Worldwake'] = 'WWK' + sets['Rise of the Eldrazi'] = 'ROE' + + # Scars of Mirrodin + sets['Scars of Mirrodin'] = 'SOM' + sets['Mirrodin Beseiged'] = 'MBS' + #sets['Unknown Mirrodin Set'] class Card: - def __init__(self, name): + def __init__(self, name, cleanName): self.name = name - self.cleanName = clean(name) - self.picture = False - self.rarity = False - self.qRarity = False + self.cleanName = cleanName + self.hasPicture = False self.picURL = '' - - -#get master card list and drop into a dictionary -file = open('cards.txt') -line = file.readline().strip() -cardDict = {} + self.hasRarity = False + self.rarity = '' + self.hasSets = False + self.sets = '' + self.lines = '' -while line != 'End': - temp = Card(line) - cardDict[temp.cleanName] = temp - skip = file.readline() - while skip.strip() != '': - skip = file.readline() +#get master card list and drop into a dictionary +folder = "cardsfolder" +err = open('cardsScript.log','w') +cardDict = {} +rarityStr = 'SVar:Rarity' +pictureStr = 'SVar:Picture' +setStr = 'SVar:Sets' +sets = {} +initSets() + + +for fileName in os.listdir(folder): +# parse cardsfolder for Card Lines and Rarity/Picture SVars. Filling in any gaps + file = open(folder + '\\' + fileName) + cleanName = fileName.replace('.txt', '') + + html = '' + + line = file.readline().strip() + name = line.replace('Name:','') + + card = Card(name, cleanName) + cardDict[cleanName] = card + card.lines = line + '\n' + line = file.readline().strip() -file.close() - -#compare entires to card pictures -file = open('card-pictures.txt') - -line = file.readline() -while line != '': - start = line.find('.jpg') - picName = clean(line[0:start]) - if cardDict.has_key(picName): - cardDict[picName].picture = True - line = file.readline() - -file.close() - -#compre to rarity fies -getRarity('common.txt', cardDict, False) -getRarity('uncommon.txt', cardDict, False) -getRarity('rare.txt', cardDict, False) -getRarity('quest/common.txt', cardDict, True) -getRarity('quest/uncommon.txt', cardDict, True) -getRarity('quest/rare.txt', cardDict, True) + while line != 'End': + if line == '' or line == 'End': + break -#output -picFile = open('card-pictures.txt', 'a') -errFile = open('cardsScript.log', 'w') - -cards = cardDict.values() -cards.sort() - -for i in range(len(cards)): - c = cards[i] - # skip basic land - if (c.name == "Swamp" or c.name == "Forest" or c.name == "Plains" or c.name == "Mountain" or c.name == "Island" or c.name == "Snow-Covered Swamp" or c.name == "Snow-Covered Forest" or c.name == "Snow-Covered Plains" or c.name == "Snow-Covered Mountain" or c.name == "Snow-Covered Island"): - continue; + if line.find(rarityStr) != -1: + card.hasRarity = True + card.rarity = getSVarString(line, rarityStr) + elif line.find(pictureStr) != -1: + card.hasPicture = True + card.picURL = getSVarString(line, pictureStr) + elif line.find(setStr) != -1: + card.hasSets = True + card.sets = getSVarString(line, setStr) + else: + card.lines += line +'\n' + + line = file.readline().strip() + + if card.hasRarity and card.hasPicture and card.hasSets: + err.write(card.name + '... No Changes' + '\n') + continue - if c.picture == False: - urlName = c.cleanName.replace(' ', '_') - picUrl ='http://www.wizards.com/global/images/magic/general/' + urlName + '.jpg' - if checkURL(picUrl): - c.picURL = picUrl - picFile.write(urlName + '.jpg\t\t' + c.picURL + '\n') - else: - errFile.write("Bad Picture URL " + c.name + '\n') - # generally for portal cards, where the primary URL doesn't work - - if c.rarity == False or c.qRarity == False: - html = getURL('http://magiccards.info/query?q=!'+c.name) - # magiccards.info uses () on the page for rarity. - # since older editions had funky things like c2, and u1, only use open parenthesis for this search - commonCount = html.count("(Common") - uncommonCount = html.count("(Uncommon") - rareCount = html.count("(Rare") + html.count("(Mythic Rare)") - if (commonCount == 0 and uncommonCount == 0 and rareCount == 0): - errFile.write("Bad magiccards.info Query: " + c.name + '\n') - elif (commonCount >= uncommonCount and commonCount >= rareCount): - writeRarity("common.txt", c) - elif (commonCount < uncommonCount and uncommonCount >= rareCount): - writeRarity("uncommon.txt", c) - else: - writeRarity("rare.txt", c) + if not card.hasRarity: + html = getURL('http://magiccards.info/query?q=!'+card.name) + rarity = getRarity(card.name, html) + if not rarity == '': + card.hasRarity = True + card.rarity = rarity + + if not card.hasPicture: + pic = getPicture(card.cleanName) + if not pic == '': + card.hasPicture = True + card.picURL = pic + + if not card.hasSets: + if html == '': + html = getURL('http://magiccards.info/query?q=!'+card.name) + allSets = getSets(card.name, html) + if not allSets == '': + card.hasSets = True + card.sets = allSets + + file = open(folder + "/" + fileName, 'w') + file.write(card.lines) + if card.hasRarity: + file.write('SVar:Rarity:'+card.rarity + '\n') + if card.hasPicture: + file.write('SVar:Picture:'+card.picURL + '\n') + if card.hasSets: + file.write('SVar:Sets:'+card.sets + '\n') + file.write('End') + file.close() + err.write(card.name + '... Updated') -picFile.close() -errFile.close() \ No newline at end of file +err.close() \ No newline at end of file