Xtraeme

Hearthstone Card Data (id, cardname, rarity, expansion, class)

Feb 10th, 2021 (edited)
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.04 KB | None | 0 0
  1. #Useful for working with https://docs.google.com/spreadsheets/d/1ewBaWGhzG01X4OWZCwC7tkVimCP_DPi-1kGfvAHkn9M/copy
  2. #Data from https://github.com/HearthSim/hsdata/blob/master/CardDefs.xml
  3. import xml.etree.ElementTree as ET
  4. import sys
  5. #import operator
  6.  
  7. '''Get cardset by # or str'''
  8. def CardSet(i):
  9.     switcher = {
  10. 2: "Basic",
  11. 3: "Classic",
  12. 12: "Naxx",
  13. 13: "GvG",          #goblins v. gnomes
  14. 14: "Blackrock",
  15. 15: "TGT",          #"The grand tournament"
  16. 20: "LoE",          #league of explorers
  17. 21: "ToG",          #the old gods
  18. 23: "Kara",         #Karazhan
  19. 25: "MSG",          #Mean Streets of Gadgetzan
  20.  
  21. 1001: "KFT",        #Frozen Throne
  22. 1004: "KnC",        #Kobolds and catacombs
  23. 1125: "Woods",      #Witchwood
  24. 1127: "Boomsday",   #Boomsday
  25. 1129: "Rastakhan",  #Rumble
  26. 1130: "Shadows",    #Rise of Shadows
  27. 1158: "Uldum",      #Saviors of Uldum
  28.                     #--- end support---
  29. 1347: "DoD",        #Descent of Dragons
  30. 1403: "Galakrond",
  31. 1414: "AoO",        #Ashes of Outland
  32. 1443: "Scholo",     #Scholomance Academy
  33. 1463: "Demon",      #Demon Hunter Initiate
  34. 1466: "Madness",    #Madness at the Darkmoon Faire
  35.  
  36. 4: "HoF",
  37. 18: "HoF",
  38. 1130: "HoF",
  39. 1452: "HoF",
  40. 1453: "HoF",
  41. }
  42.     #print(i)
  43.     if(type(i) is str and i.isnumeric()):
  44.         i = int(i)
  45.     if(type(i) is int):
  46.         return switcher.get(i, "INVALID")
  47.     else:
  48.         for key, value in switcher.items():
  49.             if value.lower() == i.lower():
  50.                 #print(f'{key}: {value}')
  51.                 return key
  52.         return "INVALID"
  53.  
  54.  
  55. '''Get class by # or str'''
  56. def HS_Class(i):
  57.     switcher = {
  58. 2: "Druid",
  59. 3: "Hunter",
  60. 4: "Mage",
  61. 5: "Paladin",
  62. 6: "Priest",
  63. 7: "Rogue",
  64. 8: "Shaman",
  65. 9: "Warlock",
  66. 10: "Warrior",
  67. 12: "Neutral",
  68. 14: "Demon Hunter",
  69. }
  70.     #print(i)
  71.     if(type(i) is str and i.isnumeric()):
  72.         i = int(i)
  73.     if(type(i) is int):
  74.         return switcher.get(i, "INVALID")
  75.     else:
  76.         for key, value in switcher.items():
  77.             if value == i:
  78.                 #print(f'{key}: {value}')
  79.                 return key
  80.  
  81.  
  82. def HS_ClassSort(e):
  83.     return e[4][0]
  84.  
  85.  
  86. '''Get Rarity by # or str'''
  87. def Rarity(i):
  88.     switcher = {
  89. 1: "Common",
  90. 2: "NA",
  91. 3: "Rare",
  92. 4: "Epic",
  93. 5: "Legendary",
  94. }
  95.     #print(i)
  96.     if(type(i) is str and i.isnumeric()):
  97.         i = int(i)
  98.     if(type(i) is int):
  99.         return switcher.get(i, "INVALID")
  100.     else:
  101.         for key, value in switcher.items():
  102.             if value == i:
  103.                 #print(f'{key}: {value}')
  104.                 return key
  105.  
  106. def RaritySort(e):
  107.     return e[2][0]
  108.  
  109.  
  110. '''Prints elements'''
  111. def ElemPrint(elem):
  112.     for subtags in elem:
  113.         print(subtags.tag, subtags.attrib)
  114.  
  115.  
  116. tree = ET.parse("CardDefs.xml")
  117. root = tree.getroot()
  118. filter_on_id = CardSet(sys.argv[1]) if (len(sys.argv) > 1) else 1466
  119.  
  120. cardsfound = list()
  121. dupelist = list()
  122.  
  123. for elem in root.findall('Entity'):
  124.     # How to make decisions based on attributes even in 2.6:
  125.     id = int(elem.get('ID'))
  126.     if(elem.get('CardID').find("HERO") == -1):                                  #id > 54870 and ... filter by upper ids
  127.         name = elem.find('Tag/enUS').text.replace(',', '')
  128.         matches = [match for match in dupelist if name in match]
  129.         if(len(matches) == 0):
  130.             #ElemPrint(elem)
  131.             rare        = elem.find('.//Tag[@name="RARITY"]')
  132.             cardset     = elem.find('.//Tag[@name="CARD_SET"]')
  133.             collectible = elem.find('.//Tag[@name="COLLECTIBLE"]')
  134.             hs_class    = elem.find('.//Tag[@name="CLASS"]')
  135.            
  136.             cardset_id  = int(cardset.get('value'))
  137.             if(rare != None and collectible != None and cardset_id == filter_on_id):    # and cardset_id ==  1443 ... filter by expansion
  138.                 dupelist.append(name)
  139.                 cardsfound.append([elem.get('ID'),
  140.                                    name,
  141.                                    [ int(rare.get('value')),
  142.                                      Rarity(int(rare.get('value')))],
  143.                                    CardSet(cardset_id),
  144.                                    [ int(hs_class.get('value')),
  145.                                      HS_Class(int(hs_class.get('value')))]]
  146.                                   )
  147.                 #print(elem.get('ID') + "," + name +
  148.                 #     "," + Rarity(int(rare.get('value'))) +
  149.                 #     "," + CardSet(cardset_id))
  150.  
  151.  
  152. cardsfound.sort(key=RaritySort)
  153. cardsfound.sort(key=HS_ClassSort)
  154. for card in cardsfound:
  155.     print(card[0] + "," +
  156.           card[1] + "," +
  157.           card[2][1] + "," +
  158.           card[3] + "," +
  159.           card[4][1])
Add Comment
Please, Sign In to add comment