From ab8e166520b75bcff9a204d0c8e6423464d0e888 Mon Sep 17 00:00:00 2001 From: jendave Date: Sun, 7 Aug 2011 01:56:01 +0000 Subject: [PATCH] Fix executable permissions. reprintSetInfo now takes a parameter for the set instead of it being hardcoded --- .gitattributes | 6 +- res/PerSetTracking.py | 262 ++++++++++----------- res/mtg-data.txt | 0 res/reprintSetInfo.py | 219 +++++++++--------- res/setInfoScript.py | 516 +++++++++++++++++++++--------------------- 5 files changed, 504 insertions(+), 499 deletions(-) mode change 100644 => 100755 res/PerSetTracking.py mode change 100755 => 100644 res/mtg-data.txt mode change 100644 => 100755 res/reprintSetInfo.py mode change 100644 => 100755 res/setInfoScript.py diff --git a/.gitattributes b/.gitattributes index a6d1be90af8..fba639cbd02 100644 --- a/.gitattributes +++ b/.gitattributes @@ -20,7 +20,7 @@ build/support/template.applescript -text /forge.properties svneol=native#text/plain /pom.xml svneol=native#text/xml res/AllTokens.txt -text svneol=native#text/plain -res/PerSetTracking.py -text svneol=native#text/x-python +res/PerSetTracking.py svneol=native#text/x-python res/blockdata/blocks.txt svneol=native#text/plain res/blockdata/setdata.txt svneol=native#text/plain res/booster-decks -text svneol=unset#unset @@ -8919,8 +8919,8 @@ res/quest/themes/White.thm -text res/quest/themes/Wolves[!!-~]WG.thm -text res/quest/themes/Zombies[!!-~]B.thm -text res/quest/veryhard.txt -text svneol=native#text/plain -res/reprintSetInfo.py -text svneol=native#text/x-python -res/setInfoScript.py -text svneol=native#text/x-python +res/reprintSetInfo.py svneol=native#text/x-python +res/setInfoScript.py svneol=native#text/x-python res/sound/tap.mp3 -text svneol=unset#audio/mpeg src/arcane/ui/CardArea.java svneol=native#text/plain src/arcane/ui/CardPanel.java svneol=native#text/plain diff --git a/res/PerSetTracking.py b/res/PerSetTracking.py old mode 100644 new mode 100755 index efcb516760e..4d3bbf7ee13 --- a/res/PerSetTracking.py +++ b/res/PerSetTracking.py @@ -1,131 +1,131 @@ -pathToMtgData = "mtg-data.txt" - -############IMPLEMENTATION FOLLOWS############ -import os,sys - -if not os.path.exists(pathToMtgData) : - print("This script requires the text version of Arch's mtg-data to be present.You can download it from slightlymagic.net's forum and either place the text version next to this script or edit this script and provide the path to the file at the top.") - print("Press Enter to exit") - raw_input("") - sys.exit() - -if not os.path.isdir(sys.path[0] + os.sep + 'PerSetTracking Results') : - os.mkdir(sys.path[0] + os.sep + 'PerSetTracking Results') - -forgeFolderContents = os.listdir(sys.path[0] + os.sep + "cardsfolder") -forgeFolderFiles = [] -forgeCards = [] -mtgDataCards = {} -setCodes = [] -forgeCardCount = 0 -mtgDataCardCount = 0 -setCodeCount = 0 - -hasFetchedSets = False -hasFetchedCardName = False -tmpName = "" -line = "" -prevline = "" - -#Parse mtg-data -print("Parsing mtg-data") -with open(pathToMtgData) as mtgdata : - for line in mtgdata : - if not hasFetchedSets : - if line != "\n" : - setCodes.append(line[0:3]) - else : - hasFetchedSets = True - if hasFetchedSets : - if not hasFetchedCardName : - tmpName = line - tmpName = tmpName.rstrip() - tmpName = tmpName.replace("AE", "Ae") - hasFetchedCardName = True - if line == "\n" : - mtgDataCards[tmpName] = prevline.rstrip() - hasFetchedCardName = False - - prevline = line - -#Parse Forge -print("Parsing Forge") -for i in forgeFolderContents : - if os.path.isfile(sys.path[0] + os.sep + "cardsfolder" + os.sep + i) == True : - forgeFolderFiles.append(i) -for file in forgeFolderFiles : - with open(sys.path[0] + os.sep + "cardsfolder" + os.sep + file) as currentForgeCard : - tmpname = currentForgeCard.readline() - tmpname = tmpname[5:].replace("AE","Ae") - tmpname = tmpname.rstrip() - forgeCards.append(tmpname) - - -#Compare datasets and output results -print("Comparing datasets and outputting results.") -totalData = {} -currentMissing = [] -currentImplemented = [] -total = 0 -percentage = 0 -for currentSet in setCodes : - if currentSet == 'UNH' or currentSet == 'UGL' : continue #skip Unhinged and Unglued since they are only counting basic lands anyway - #if currentSet == 'VG1' or currentSet == 'VG2' or currentSet == 'VG3' : continue - #if currentSet == 'VG4' or currentSet == 'VGO' or currentSet == 'VG ' : continue - #if currentSet == 'FVD' or currentSet == 'FVE' or currentSet == 'FVR' : continue - #if currentSet == 'SDC' or currentSet == 'AST' or currentSet == 'DKM' : continue - #if currentSet == 'BTD' or currentSet == 'ARC' or currentSet == 'COM' : continue - #if currentSet == 'CHR' or currentSet == 'MED' or currentSet == 'H09' : continue - #if currentSet == 'ME2' or currentSet == 'ME3' or currentSet == 'ME4' : continue - #if currentSet == 'ATH' or currentSet == 'HOP' or currentSet == 'BRB' : continue - #if currentSet == 'EVG' or currentSet == 'GVL' or currentSet == 'JVC' : continue - for card in mtgDataCards.keys() : - if mtgDataCards[card].count(currentSet) > 0 : - if card in forgeCards : - currentImplemented.append(card) - else : - currentMissing.append(card) - total = len(currentMissing)+len(currentImplemented) - percentage = 0 - if total > 0 : - percentage = (float(len(currentImplemented))/float(total))*100 - - currentMissing.sort() - currentImplemented.sort() - - with open(sys.path[0] + os.sep + "PerSetTracking Results" + os.sep + "set_" + currentSet + ".txt", "w") as output : - output.write("Implemented (" + str(len(currentImplemented)) + "):\n") - for everyImplemented in currentImplemented : - output.write(everyImplemented + '\n') - output.write("\n") - output.write("Missing (" + str(len(currentMissing)) + "):\n") - for everyMissing in currentMissing : - output.write(everyMissing + '\n') - output.write("\n") - output.write("Total: " + str(total) + "\n") - output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n") - totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage) - del currentMissing[:] - del currentImplemented[:] - -#sort sets by percentage completed -totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True) - -totalPercentage = 0 -totalMissing = 0 -totalImplemented = 0 -fullTotal = 0 -with open(sys.path[0] + os.sep + "PerSetTracking Results" + os.sep + "CompleteStats.txt", "w") as statsfile: - statsfile.write("Set: Implemented (Missing) / Total = Percentage Implemented\n") - for k,dataKey in totalDataList : - totalImplemented += dataKey[0] - totalMissing += dataKey[1] - fullTotal += dataKey[2] - statsfile.write(k + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n") - totalPercentage = totalImplemented / fullTotal - statsfile.write("\n") - statsfile.write("Total over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal)) - -print "Done!" -print "Press Enter to exit." -raw_input("") +#!/usr/bin/env python + +pathToMtgData = "mtg-data.txt" + +############IMPLEMENTATION FOLLOWS############ +import os,sys + +if not os.path.exists(pathToMtgData) : + print("This script requires the text version of Arch's mtg-data to be present.You can download it from slightlymagic.net's forum and either place the text version next to this script or edit this script and provide the path to the file at the top.") + print("Press Enter to exit") + raw_input("") + sys.exit() + +if not os.path.isdir(sys.path[0] + os.sep + 'PerSetTracking Results') : + os.mkdir(sys.path[0] + os.sep + 'PerSetTracking Results') + +forgeFolderContents = os.listdir(sys.path[0] + os.sep + "cardsfolder") +forgeFolderFiles = [] +forgeCards = [] +mtgDataCards = {} +setCodes = [] +forgeCardCount = 0 +mtgDataCardCount = 0 +setCodeCount = 0 + +hasFetchedSets = False +hasFetchedCardName = False +tmpName = "" +line = "" +prevline = "" + +#Parse mtg-data +print("Parsing mtg-data") +with open(pathToMtgData) as mtgdata : + for line in mtgdata : + if not hasFetchedSets : + if line != "\n" : + setCodes.append(line[0:3]) + else : + hasFetchedSets = True + if hasFetchedSets : + if not hasFetchedCardName : + tmpName = line + tmpName = tmpName.rstrip() + tmpName = tmpName.replace("AE", "Ae") + hasFetchedCardName = True + if line == "\n" : + mtgDataCards[tmpName] = prevline.rstrip() + hasFetchedCardName = False + + prevline = line + +#Parse Forge +print("Parsing Forge") +for i in forgeFolderContents : + if os.path.isfile(sys.path[0] + os.sep + "cardsfolder" + os.sep + i) == True : + forgeFolderFiles.append(i) +for file in forgeFolderFiles : + with open(sys.path[0] + os.sep + "cardsfolder" + os.sep + file) as currentForgeCard : + tmpname = currentForgeCard.readline() + tmpname = tmpname[5:].replace("AE","Ae") + tmpname = tmpname.rstrip() + forgeCards.append(tmpname) + +#Compare datasets and output results +print("Comparing datasets and outputting results.") +totalData = {} +currentMissing = [] +currentImplemented = [] +total = 0 +percentage = 0 +for currentSet in setCodes : + if currentSet == 'UNH' or currentSet == 'UGL' : continue #skip Unhinged and Unglued since they are only counting basic lands anyway + #if currentSet == 'VG1' or currentSet == 'VG2' or currentSet == 'VG3' : continue + #if currentSet == 'VG4' or currentSet == 'VGO' or currentSet == 'VG ' : continue + #if currentSet == 'FVD' or currentSet == 'FVE' or currentSet == 'FVR' : continue + #if currentSet == 'SDC' or currentSet == 'AST' or currentSet == 'DKM' : continue + #if currentSet == 'BTD' or currentSet == 'ARC' or currentSet == 'COM' : continue + #if currentSet == 'CHR' or currentSet == 'MED' or currentSet == 'H09' : continue + #if currentSet == 'ME2' or currentSet == 'ME3' or currentSet == 'ME4' : continue + #if currentSet == 'ATH' or currentSet == 'HOP' or currentSet == 'BRB' : continue + #if currentSet == 'EVG' or currentSet == 'GVL' or currentSet == 'JVC' : continue + for card in mtgDataCards.keys() : + if mtgDataCards[card].count(currentSet) > 0 : + if card in forgeCards : + currentImplemented.append(card) + else : + currentMissing.append(card) + total = len(currentMissing)+len(currentImplemented) + percentage = 0 + if total > 0 : + percentage = (float(len(currentImplemented))/float(total))*100 + currentMissing.sort() + currentImplemented.sort() + + with open(sys.path[0] + os.sep + "PerSetTracking Results" + os.sep + "set_" + currentSet + ".txt", "w") as output : + output.write("Implemented (" + str(len(currentImplemented)) + "):\n") + for everyImplemented in currentImplemented : + output.write(everyImplemented + '\n') + output.write("\n") + output.write("Missing (" + str(len(currentMissing)) + "):\n") + for everyMissing in currentMissing : + output.write(everyMissing + '\n') + output.write("\n") + output.write("Total: " + str(total) + "\n") + output.write("Percentage implemented: " + str(round(percentage,2)) + "%\n") + totalData[currentSet] = (len(currentImplemented),len(currentMissing),total,percentage) + del currentMissing[:] + del currentImplemented[:] + +#sort sets by percentage completed +totalDataList = sorted(totalData.items(), key=lambda (key,entry): entry[3], reverse=True) + +totalPercentage = 0 +totalMissing = 0 +totalImplemented = 0 +fullTotal = 0 +with open(sys.path[0] + os.sep + "PerSetTracking Results" + os.sep + "CompleteStats.txt", "w") as statsfile: + statsfile.write("Set: Implemented (Missing) / Total = Percentage Implemented\n") + for k,dataKey in totalDataList : + totalImplemented += dataKey[0] + totalMissing += dataKey[1] + fullTotal += dataKey[2] + statsfile.write(k + ": " + str(dataKey[0]) + " (" + str(dataKey[1]) + ") / " + str(dataKey[2]) + " = " + str(round(dataKey[3], 2)) + "%\n") + totalPercentage = totalImplemented / fullTotal + statsfile.write("\n") + statsfile.write("Total over all sets: " + str(totalImplemented) + " (" + str(totalMissing) + ") / " + str(fullTotal)) + +print "Done!" +print "Press Enter to exit." +raw_input("") diff --git a/res/mtg-data.txt b/res/mtg-data.txt old mode 100755 new mode 100644 diff --git a/res/reprintSetInfo.py b/res/reprintSetInfo.py old mode 100644 new mode 100755 index 7e6ebc7173c..18fe473fee8 --- a/res/reprintSetInfo.py +++ b/res/reprintSetInfo.py @@ -1,108 +1,111 @@ -# This script grabs the list of all cards in a set and clears out the setInfo -# After running this script, re-run setInfoScript to fill in the slots -# MAKE SURE THE setAbbr VARIABLE IS UPDATED TO THE SET YOU WANT TO CLEAR OUT - -from httplib import HTTP -from urlparse import urlparse -from urllib import urlopen -import os - -class SetInfo: - def __init__(self, set, rarity, image): - self.set = set - self.rarity = rarity - self.image = image - -class Card: - def __init__(self, name, cleanName): - self.name = name - self.cleanName = cleanName - self.hasSet = False - self.sets = {} - -def clean(name): - return name.replace(' ','_').replace('\'','').replace('-','_').replace('"','').replace(',','').lower() - -def getCardsInSet(): - html = urlopen('http://magiccards.info/query?q=e:'+setAbbr+'&v=olist').read() - - start = html.find('Card name') - end = html.find('', start) - block = html[start:end] - - while True: - nameIndex = block.find('.html">') - - if nameIndex == -1: - break - - nameEnd = block.find('<',nameIndex) - - name = block[nameIndex+7:nameEnd] - - # Add name to array - nameList.append(clean(name)+'.txt') - - block = block[nameEnd:] - - return - -folder = "cardsfolder" -err = open('reprintSetInfo.log','w') - -# THIS NEEDS TO BE UPDATED TO THE SET YOU WANT TO UPDATE -# SOME ARE THREE LETTER ABBR. BUT SOME ARE JUST TWO. CHECK BEFORE YOU RUN! -setAbbr = 'mbs' - -cardDict = {} -setStr = 'SetInfo:' -nameList = [] -getCardsInSet() - -for fileName in nameList: - # if file doesn't exist continue - filePath = os.path.join(folder, fileName) - - print filePath - - if os.path.isfile(filePath) == False: - continue - - file = open(filePath) - cleanName = fileName.replace('.txt', '') - - line = file.readline().strip() - # Handle name and creation - name = line.replace('Name:','') - - card = Card(name, cleanName) - cardDict[cleanName] = card - card.lines = line + '\n' - - # Start parsing the rest of the data file - line = file.readline().strip() - - while line != 'End': - # Skip empty lines - if line == '': - line = file.readline().strip() - continue - - # We really shouldn - if line == 'End': - break - - # Skip SetInfo lines - if line.find(setStr) == -1: - card.lines += line +'\n' - - line = file.readline().strip() - - file = open(os.path.join(folder, fileName), 'w') - file.write(card.lines) - - file.write('End') - file.close() - err.write(card.name + '... Updated\n') - -err.close() \ No newline at end of file +#!/usr/bin/env python + +# This script grabs the list of all cards in a set and clears out the setInfo +# After running this script, re-run setInfoScript to fill in the slots +# MAKE SURE THE setAbbr VARIABLE IS UPDATED TO THE SET YOU WANT TO CLEAR OUT + +from httplib import HTTP +from urlparse import urlparse +from urllib import urlopen +import sys +import os + +class SetInfo: + def __init__(self, set, rarity, image): + self.set = set + self.rarity = rarity + self.image = image + +class Card: + def __init__(self, name, cleanName): + self.name = name + self.cleanName = cleanName + self.hasSet = False + self.sets = {} + +def clean(name): + return name.replace(' ','_').replace('\'','').replace('-','_').replace('"','').replace(',','').lower() + +def getCardsInSet(): + html = urlopen('http://magiccards.info/query?q=e:'+setAbbr+'&v=olist').read() + + start = html.find('Card name') + end = html.find('', start) + block = html[start:end] + + while True: + nameIndex = block.find('.html">') + + if nameIndex == -1: + break + + nameEnd = block.find('<',nameIndex) + + name = block[nameIndex+7:nameEnd] + + # Add name to array + nameList.append(clean(name)+'.txt') + + block = block[nameEnd:] + + return + +folder = "cardsfolder" +err = open('reprintSetInfo.log','w') + +# THIS NEEDS TO BE UPDATED TO THE SET YOU WANT TO UPDATE +# SOME ARE THREE LETTER ABBR. BUT SOME ARE JUST TWO. CHECK BEFORE YOU RUN! +print "Using Set: " + sys.argv[1] +setAbbr = sys.argv[1] + +cardDict = {} +setStr = 'SetInfo:' +nameList = [] +getCardsInSet() + +for fileName in nameList: + # if file doesn't exist continue + filePath = os.path.join(folder, fileName) + print filePath + + if os.path.isfile(filePath) == False: + continue + + file = open(filePath) + cleanName = fileName.replace('.txt', '') + + line = file.readline().strip() + # Handle name and creation + name = line.replace('Name:','') + + card = Card(name, cleanName) + cardDict[cleanName] = card + card.lines = line + '\n' + + # Start parsing the rest of the data file + line = file.readline().strip() + + while line != 'End': + # Skip empty lines + if line == '': + line = file.readline().strip() + continue + + # We really shouldn + if line == 'End': + break + + # Skip SetInfo lines + if line.find(setStr) == -1: + card.lines += line +'\n' + + line = file.readline().strip() + + file = open(os.path.join(folder, fileName), 'w') + file.write(card.lines) + + file.write('End') + file.close() + err.write(card.name + '... Updated\n') + +err.close() diff --git a/res/setInfoScript.py b/res/setInfoScript.py old mode 100644 new mode 100755 index d846b214f52..399a6a19c7b --- a/res/setInfoScript.py +++ b/res/setInfoScript.py @@ -1,257 +1,259 @@ -# This python script is designed to handle the following: individual cards located in /res/cardsfolder -# Insert of SetInfo data into data files from magiccards.info - -from httplib import HTTP -from urlparse import urlparse -from urllib import urlopen -import os - -def getURL(url): - return urlopen(url).read() - -class SetInfo: - def __init__(self, set, rarity, image): - self.set = set - self.rarity = rarity - self.image = image - -class Card: - def __init__(self, name, cleanName): - self.name = name - self.cleanName = cleanName - self.hasSet = False - self.sets = {} - -def initSets(): - # Base Sets - allSets['Limited Edition Alpha'] = 'LEA' - allSets['Limited Edition Beta'] = 'LEB' - allSets['Unlimited Edition'] = '2ED' - allSets['Revised Edition'] = '3ED' - allSets['Fourth Edition'] = '4ED' - allSets['Fifth Edition'] = '5ED' - allSets['Classic Sixth Edition'] = '6ED' - allSets['Seventh Edition'] = '7ED' - allSets['Eighth Edition'] = '8ED' - allSets['Ninth Edition'] = '9ED' - allSets['Tenth Edition'] = '10E' - allSets['Magic 2010'] = 'M10' - allSets['Magic 2011'] = 'M11' - #allSets['Magic 2012'] = 'M12' - - # Portal - allSets['Portal'] = 'POR' - allSets['Portal Second Age'] = 'PO2' - allSets['Portal Three Kingdoms'] = 'PTK' - - # Starter - allSets['Starter 1999'] = 'S99' - allSets['Starter 2000'] = 'S00' - - # Early Sets - allSets['Arabian Nights'] = 'ARN' - allSets['Antiquities'] = 'ATQ' - allSets['Legends'] = 'LEG' - allSets['The Dark'] = 'DRK' - allSets['Fallen Empires'] = 'FEM' - allSets['Homelands'] = 'HML' - - # Ice Age - allSets['Ice Age'] = 'ICE' - allSets['Alliances'] = 'ALL' - allSets['Coldsnap'] = 'CSP' - - # Mirage - allSets['Mirage'] = 'MIR' - allSets['Visions'] = 'VIS' - allSets['Weatherlight'] = 'WTH' - - # Rath Cycle - allSets['Tempest'] = 'TMP' - allSets['Stronghold'] = 'STH' - allSets['Exodus'] = 'EXO' - - # Artifacts Cycle - allSets['Urza\'s Saga'] = 'USG' - allSets['Urza\'s Legacy'] = 'ULG' - allSets['Urza\'s Destiny'] = 'UDS' - - # Masques - allSets['Mercadian Masques'] = 'MMQ' - allSets['Nemesis'] = 'NMS' - allSets['Prophecy'] = 'PCY' - - # Invasion - allSets['Invasion'] = 'INV' - allSets['Planeshift'] = 'PLS' - allSets['Apocalypse'] = 'APC' - - # Odyssey - allSets['Odyssey'] = 'ODY' - allSets['Torment'] = 'TOR' - allSets['Judgment'] = 'JUD' - - # Onslaught - allSets['Onslaught'] = 'ONS' - allSets['Legions'] = 'LGN' - allSets['Scourge'] = 'SCG' - - # Mirrodin - allSets['Mirrodin'] = 'MRD' - allSets['Darksteel'] = 'DST' - allSets['Fifth Dawn'] = '5DN' - - # Kamigawa - allSets['Champions of Kamigawa'] = 'CHK' - allSets['Betrayers of Kamigawa'] = 'BOK' - allSets['Saviors of Kamigawa'] = 'SOK' - - # Ravnica - allSets['Ravnica: City of Guilds'] = 'RAV' - allSets['Guildpact'] = 'GPT' - allSets['Dissension'] = 'DIS' - - # Time Spiral - allSets['Time Spiral'] = 'TSP' - allSets['Time Spiral "Timeshifted"'] = 'TSB' - allSets['Planar Chaos'] = 'PLC' - allSets['Future Sight'] = 'FUT' - - # Lorwyn - allSets['Lorwyn'] = 'LRW' - allSets['Morningtide'] = 'MOR' - - # Shadowmoor - allSets['Shadowmoor'] = 'SHM' - allSets['Eventide'] = 'EVE' - - # Alara - allSets['Shards of Alara'] = 'ALA' - allSets['Conflux'] = 'CFX' - allSets['Alara Reborn'] = 'ARB' - - # Zendikar - allSets['Zendikar'] = 'ZEN' - allSets['Worldwake'] = 'WWK' - allSets['Rise of the Eldrazi'] = 'ROE' - - # Scars of Mirrodin - allSets['Scars of Mirrodin'] = 'SOM' - allSets['Mirrodin Besieged'] = 'MBS' - allSets['New Phyrexia'] = 'NPH' - -def addSets(card): - html = getURL('http://magiccards.info/query?q=!'+card.name) - start = html.find('
Editions:
') - end = html.find('
Languages:
', start) - block = html[start:end] - - print card.name - - for edition in allSets.keys(): - edIndex = block.find('>'+edition+'<') # Portal/Mirrodin issue - - if edIndex == -1: - edIndex = block.find('>'+edition+' (') # Single set issue - if edIndex == -1: - continue - - # Scrape rarity - rarityIndex = block.find('(',edIndex) - rarity = block[rarityIndex+1:block.find(')',rarityIndex)] - raritySpace = rarity.find(' ') - if raritySpace != -1: - rarity = rarity[0:raritySpace] # For older cards - - # What to do with TimeShifted cards? - if rarity == 'Special' and edition != 'Time Spiral "Timeshifted"': - continue - - # Get setAbbreviation and setNumber - dataIndex = block.rfind('"/',0,edIndex) - data = block[dataIndex+2:edIndex-1] # 1 instead of 2 because of Portal/Mirrodin Issue - - splitData = data.split('/') - setAbbr = splitData[0] - setNum = splitData[2].replace('.html', '') - - if len(setNum) > 4: - # Setnum not available here for most recent set. Switch to the .jpg used on page - jpgIndex = html.find('.jpg') - data = html[html.rfind('scans/en/', 0, jpgIndex):jpgIndex] - - # data = scans/en/[set]/[num] - splitData = data.split('/') - setAbbr = splitData[2] - setNum = splitData[3] - - image = 'http://magiccards.info/scans/en/' + setAbbr + '/' + setNum + '.jpg' - - card.sets[allSets[edition]] = SetInfo(allSets[edition], rarity, image) - - return - - -#get master card list and drop into a dictionary -folder = "cardsfolder" -err = open('setInfoScript.log','w') -allSets = {} -initSets() -cardDict = {} -setStr = 'SetInfo:' - -for fileName in os.listdir(folder): - if fileName.startswith('.'): - continue - -# parse cardsfolder for Card Lines and Rarity/Picture SVars. Filling in any gaps - file = open(folder + os.sep + fileName) - cleanName = fileName.replace('.txt', '') - - line = file.readline().strip() - # Handle name and creation - name = line.replace('Name:','') - - card = Card(name.replace(' ','+'), cleanName) #This makes it work on Mac OS X. Will test Windows and FreeBSD when I can. - cardDict[cleanName] = card - card.lines = line + '\n' - - # Start parsing the rest of the data file - line = file.readline().strip() - - while line != 'End': - # Skip empty lines - if line == '': - line = file.readline().strip() - continue - - # We really shouldn't - if line == 'End': - break - - if line.find(setStr) != -1: - info = line.replace('SetInfo:','') - parts = info.split('|') - - card.hasSet = True - card.sets[parts[0]] = SetInfo(parts[0], parts[1], parts[2]) - else: - card.lines += line +'\n' - - line = file.readline().strip() - - if not card.hasSet: - addSets(card) - card.hasSet = True - - file = open(folder + os.sep + fileName, 'w') - file.write(card.lines) - if card.hasSet: - for s in card.sets.values(): - file.write('SetInfo:'+ s.set + '|' + s.rarity + '|' + s.image + '\n') - - file.write('End') - file.close() - err.write(card.name + '... Updated\n') - -err.close() \ No newline at end of file +#!/usr/bin/env python + +# This python script is designed to handle the following: individual cards located in /res/cardsfolder +# Insert of SetInfo data into data files from magiccards.info + +from httplib import HTTP +from urlparse import urlparse +from urllib import urlopen +import os + +def getURL(url): + return urlopen(url).read() + +class SetInfo: + def __init__(self, set, rarity, image): + self.set = set + self.rarity = rarity + self.image = image + +class Card: + def __init__(self, name, cleanName): + self.name = name + self.cleanName = cleanName + self.hasSet = False + self.sets = {} + +def initSets(): + # Base Sets + allSets['Limited Edition Alpha'] = 'LEA' + allSets['Limited Edition Beta'] = 'LEB' + allSets['Unlimited Edition'] = '2ED' + allSets['Revised Edition'] = '3ED' + allSets['Fourth Edition'] = '4ED' + allSets['Fifth Edition'] = '5ED' + allSets['Classic Sixth Edition'] = '6ED' + allSets['Seventh Edition'] = '7ED' + allSets['Eighth Edition'] = '8ED' + allSets['Ninth Edition'] = '9ED' + allSets['Tenth Edition'] = '10E' + allSets['Magic 2010'] = 'M10' + allSets['Magic 2011'] = 'M11' + #allSets['Magic 2012'] = 'M12' + + # Portal + allSets['Portal'] = 'POR' + allSets['Portal Second Age'] = 'PO2' + allSets['Portal Three Kingdoms'] = 'PTK' + + # Starter + allSets['Starter 1999'] = 'S99' + allSets['Starter 2000'] = 'S00' + + # Early Sets + allSets['Arabian Nights'] = 'ARN' + allSets['Antiquities'] = 'ATQ' + allSets['Legends'] = 'LEG' + allSets['The Dark'] = 'DRK' + allSets['Fallen Empires'] = 'FEM' + allSets['Homelands'] = 'HML' + + # Ice Age + allSets['Ice Age'] = 'ICE' + allSets['Alliances'] = 'ALL' + allSets['Coldsnap'] = 'CSP' + + # Mirage + allSets['Mirage'] = 'MIR' + allSets['Visions'] = 'VIS' + allSets['Weatherlight'] = 'WTH' + + # Rath Cycle + allSets['Tempest'] = 'TMP' + allSets['Stronghold'] = 'STH' + allSets['Exodus'] = 'EXO' + + # Artifacts Cycle + allSets['Urza\'s Saga'] = 'USG' + allSets['Urza\'s Legacy'] = 'ULG' + allSets['Urza\'s Destiny'] = 'UDS' + + # Masques + allSets['Mercadian Masques'] = 'MMQ' + allSets['Nemesis'] = 'NMS' + allSets['Prophecy'] = 'PCY' + + # Invasion + allSets['Invasion'] = 'INV' + allSets['Planeshift'] = 'PLS' + allSets['Apocalypse'] = 'APC' + + # Odyssey + allSets['Odyssey'] = 'ODY' + allSets['Torment'] = 'TOR' + allSets['Judgment'] = 'JUD' + + # Onslaught + allSets['Onslaught'] = 'ONS' + allSets['Legions'] = 'LGN' + allSets['Scourge'] = 'SCG' + + # Mirrodin + allSets['Mirrodin'] = 'MRD' + allSets['Darksteel'] = 'DST' + allSets['Fifth Dawn'] = '5DN' + + # Kamigawa + allSets['Champions of Kamigawa'] = 'CHK' + allSets['Betrayers of Kamigawa'] = 'BOK' + allSets['Saviors of Kamigawa'] = 'SOK' + + # Ravnica + allSets['Ravnica: City of Guilds'] = 'RAV' + allSets['Guildpact'] = 'GPT' + allSets['Dissension'] = 'DIS' + + # Time Spiral + allSets['Time Spiral'] = 'TSP' + allSets['Time Spiral "Timeshifted"'] = 'TSB' + allSets['Planar Chaos'] = 'PLC' + allSets['Future Sight'] = 'FUT' + + # Lorwyn + allSets['Lorwyn'] = 'LRW' + allSets['Morningtide'] = 'MOR' + + # Shadowmoor + allSets['Shadowmoor'] = 'SHM' + allSets['Eventide'] = 'EVE' + + # Alara + allSets['Shards of Alara'] = 'ALA' + allSets['Conflux'] = 'CFX' + allSets['Alara Reborn'] = 'ARB' + + # Zendikar + allSets['Zendikar'] = 'ZEN' + allSets['Worldwake'] = 'WWK' + allSets['Rise of the Eldrazi'] = 'ROE' + + # Scars of Mirrodin + allSets['Scars of Mirrodin'] = 'SOM' + allSets['Mirrodin Besieged'] = 'MBS' + allSets['New Phyrexia'] = 'NPH' + +def addSets(card): + html = getURL('http://magiccards.info/query?q=!'+card.name) + start = html.find('
Editions:
') + end = html.find('
Languages:
', start) + block = html[start:end] + + print card.name + + for edition in allSets.keys(): + edIndex = block.find('>'+edition+'<') # Portal/Mirrodin issue + + if edIndex == -1: + edIndex = block.find('>'+edition+' (') # Single set issue + if edIndex == -1: + continue + + # Scrape rarity + rarityIndex = block.find('(',edIndex) + rarity = block[rarityIndex+1:block.find(')',rarityIndex)] + raritySpace = rarity.find(' ') + if raritySpace != -1: + rarity = rarity[0:raritySpace] # For older cards + + # What to do with TimeShifted cards? + if rarity == 'Special' and edition != 'Time Spiral "Timeshifted"': + continue + + # Get setAbbreviation and setNumber + dataIndex = block.rfind('"/',0,edIndex) + data = block[dataIndex+2:edIndex-1] # 1 instead of 2 because of Portal/Mirrodin Issue + + splitData = data.split('/') + setAbbr = splitData[0] + setNum = splitData[2].replace('.html', '') + + if len(setNum) > 4: + # Setnum not available here for most recent set. Switch to the .jpg used on page + jpgIndex = html.find('.jpg') + data = html[html.rfind('scans/en/', 0, jpgIndex):jpgIndex] + + # data = scans/en/[set]/[num] + splitData = data.split('/') + setAbbr = splitData[2] + setNum = splitData[3] + + image = 'http://magiccards.info/scans/en/' + setAbbr + '/' + setNum + '.jpg' + + card.sets[allSets[edition]] = SetInfo(allSets[edition], rarity, image) + + return + + +#get master card list and drop into a dictionary +folder = "cardsfolder" +err = open('setInfoScript.log','w') +allSets = {} +initSets() +cardDict = {} +setStr = 'SetInfo:' + +for fileName in os.listdir(folder): + if fileName.startswith('.'): + continue + +# parse cardsfolder for Card Lines and Rarity/Picture SVars. Filling in any gaps + file = open(folder + os.sep + fileName) + cleanName = fileName.replace('.txt', '') + + line = file.readline().strip() + # Handle name and creation + name = line.replace('Name:','') + + card = Card(name.replace(' ','+'), cleanName) #This makes it work on Mac OS X. Will test Windows and FreeBSD when I can. + cardDict[cleanName] = card + card.lines = line + '\n' + + # Start parsing the rest of the data file + line = file.readline().strip() + + while line != 'End': + # Skip empty lines + if line == '': + line = file.readline().strip() + continue + + # We really shouldn't + if line == 'End': + break + + if line.find(setStr) != -1: + info = line.replace('SetInfo:','') + parts = info.split('|') + + card.hasSet = True + card.sets[parts[0]] = SetInfo(parts[0], parts[1], parts[2]) + else: + card.lines += line +'\n' + + line = file.readline().strip() + + if not card.hasSet: + addSets(card) + card.hasSet = True + + file = open(folder + os.sep + fileName, 'w') + file.write(card.lines) + if card.hasSet: + for s in card.sets.values(): + file.write('SetInfo:'+ s.set + '|' + s.rarity + '|' + s.image + '\n') + + file.write('End') + file.close() + err.write(card.name + '... Updated\n') + +err.close()