Corrections Post Scrape, Unicode JSON

Corrections should be after all scraping. Output files should be Unicode.
This commit is contained in:
tritoch
2017-06-19 20:38:12 -05:00
committed by GitHub
parent f7e71f4ca1
commit 47234b17d2
2 changed files with 23 additions and 14 deletions

View File

@@ -3,6 +3,7 @@ import spoilers
import os
import commentjson
import json
import io
presets = {
"isfullspoil": False, # when full spoil comes around, we only want to use WOTC images
@@ -43,8 +44,9 @@ def save_masterpieces(masterpieces, setinfo):
json.dump(masterpieces, outfile, sort_keys=True, indent=2, separators=(',', ': '))
def save_setjson(mtgs, filename):
with open('out/' + filename + '.json', 'w') as outfile:
json.dump(mtgs, outfile, sort_keys=True, indent=2, separators=(',', ': '))
with io.open('out/' + filename + '.json', 'w', encoding='utf8') as json_file:
data = json.dumps(mtgs, ensure_ascii=False, encoding='utf8', indent=2, sort_keys=True, separators=(',',':'))
json_file.write(unicode(data))
def save_errorlog(errorlog):
fixederrors = []
@@ -79,7 +81,7 @@ if __name__ == '__main__':
scryfall = spoilers.get_scryfall('https://api.scryfall.com/cards/search?q=++e:' + setinfo['setname'].lower())
mtgs = spoilers.get_image_urls(mtgs, presets['isfullspoil'], setinfo['setname'], setinfo['setlongname'], setinfo['setsize']) #get images
mtgjson = spoilers.smash_mtgs_scryfall(mtgs, scryfall)
[mtgjson, errors] = spoilers.errorcheck(mtgjson) #check for errors where possible
[mtgjson, errors] = spoilers.errorcheck(mtgjson, card_corrections) #check for errors where possible
errorlog += errors
spoilers.write_xml(mtgjson, setinfo['setname'], setinfo['setlongname'], setinfo['setreleasedate'])
#save_xml(spoilers.pretty_xml(setinfo['setname']), 'out/spoiler.xml')

View File

@@ -301,7 +301,7 @@ def correct_cards(mtgjson, manual_cards=[], card_corrections=[], delete_cards=[]
card['colorIdentity'] += CID
else:
card['colorIdentity'] = [CID]
print mtgjson
#print mtgjson
for card in mtgjson['cards']:
isManual = False
for manualCard in manual_cards:
@@ -323,18 +323,11 @@ def correct_cards(mtgjson, manual_cards=[], card_corrections=[], delete_cards=[]
mtgjson = {"cards": mtgjson2}
for card in mtgjson['cards']:
for cardCorrection in card_corrections:
if card['name'] == cardCorrection:
for correctionType in card_corrections[cardCorrection]:
if not correctionType == 'name':
card[correctionType] = card_corrections[cardCorrection][correctionType]
if 'name' in card_corrections[cardCorrection]:
card['name'] = card_corrections[cardCorrection]['name']
return mtgjson
def errorcheck(mtgjson):
def errorcheck(mtgjson, card_corrections={}):
errors = []
for card in mtgjson['cards']:
for key in card:
if key == "":
@@ -431,6 +424,20 @@ def errorcheck(mtgjson):
errors.append({"name": card['name'], "key": "number", "value": ""})
if not 'types' in card:
errors.append({"name": card['name'], "key": "types", "value": ""})
for card in mtgjson['cards']:
for cardCorrection in card_corrections:
if card['name'] == cardCorrection:
for correctionType in card_corrections[cardCorrection]:
# if not correctionType in card and correctionType not in :
# sys.exit("Invalid correction for " + cardCorrection + " of type " + card)
if not correctionType == 'name':
if correctionType == 'img':
card['url'] = card_corrections[cardCorrection][correctionType]
else:
card[correctionType] = card_corrections[cardCorrection][correctionType]
if 'name' in card_corrections[cardCorrection]:
card['name'] = card_corrections[cardCorrection]['name']
#print errors
return [mtgjson, errors]
@@ -488,7 +495,7 @@ def convert_scryfall(scryfall):
card2['number'] = card['collector_number']
card2['rarity'] = card['rarity'].replace('mythic','mythic rare').title()
if card.has_key('oracle_text'):
card2['text'] = card['oracle_text'].replace(u"\u2022 ", u'*').replace(u"\u2014",'-').replace(u"\u2212","-")
card2['text'] = card['oracle_text'].replace(u"\u2014",'-').replace(u"\u2212","-")
else:
card2['text'] = ''
card2['url'] = card['image_uri']