idlechampadvimages.py 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. import json
  2. from pprint import pprint
  3. import re
  4. from collections import OrderedDict
  5. import math
  6. from decimal import Decimal
  7. import html
  8. import glob
  9. from idlechampaccount import ICAccount
  10. import subprocess
  11. import filecmp
  12. import os
  13. import hashlib
  14. import requests
  15. import zlib
  16. import tempfile
  17. COMPARE = True
  18. POST = False
  19. PROCESS = True
  20. REDOWNLOAD = False
  21. _summary = None
  22. if POST:
  23. instance = ICAccount()
  24. instance.login()
  25. # filename = '/home/txtsd/.local/share/Steam/steamapps/common/IdleChampions/IdleDragons_Data/StreamingAssets/downloaded_files/cached_definitions.json'
  26. filename = '/tmp/cached_definitions.json'
  27. if REDOWNLOAD or not os.path.isfile(filename):
  28. result = requests.get('http://master.idlechampions.com/~idledragons/post.php?call=getdefinitions')
  29. with open(filename, 'w') as f:
  30. if result.status_code == 200:
  31. f.write(result.text)
  32. adv_filename = 'json/adventure_defines.json'
  33. area_filename = 'json/adventure_area_defines.json'
  34. camp_filename = 'json/campaign_defines.json'
  35. monster_filename = 'json/monster_defines.json'
  36. with open(filename) as f:
  37. file = f.read()
  38. with open(adv_filename) as f:
  39. adv_file = f.read()
  40. with open(area_filename) as f:
  41. area_file = f.read()
  42. with open(camp_filename) as f:
  43. camp_file = f.read()
  44. with open(monster_filename) as f:
  45. monster_file = f.read()
  46. js = json.loads(file)
  47. js_adv = json.loads(adv_file)
  48. js_area = json.loads(area_file)
  49. js_camp = json.loads(camp_file)
  50. js_mon = json.loads(monster_file)
  51. js_graphic = js['graphic_defines']
  52. js_attack = js['attack_defines']
  53. js_hero = js['hero_defines']
  54. js_hero_skin = js['hero_skin_defines']
  55. js_upgrade = js['upgrade_defines']
  56. js_premium_item = js['premium_item_defines']
  57. js_sound = js['sound_defines']
  58. js_buff = js['buff_defines']
  59. js_loot = js['loot_defines']
  60. js_achievement = js['achievement_defines']
  61. js_ability = js['ability_defines']
  62. js_effect = js['effect_defines']
  63. js_changelog = js['changelog_defines']
  64. js_text = js['text_defines']
  65. js_chest_type = js['chest_type_defines']
  66. js_effect_key = js['effect_key_defines']
  67. js_tutorial_state = js['tutorial_state_defines']
  68. js_game_rule = js['game_rule_defines']
  69. js_news = js['news_defines']
  70. js_language = js['language_defines']
  71. js_familiar = js['familiar_defines']
  72. __js_adv = sorted(js_adv, key=lambda x: x['id'])
  73. __js_adv = sorted(__js_adv, key=lambda x: x['area_set_id'])
  74. __js_adv = sorted(__js_adv, key=lambda x: x['location_id'])
  75. __js_adv = sorted(__js_adv, key=lambda x: x['campaign_id'])
  76. set_advs = set()
  77. breakout = 0
  78. LICENSE = '''
  79. == Licensing ==
  80. {{Copyright game}}'''
  81. for adv in __js_adv:
  82. if adv['id'] not in set_advs:
  83. set_advs.add(adv['id'])
  84. if (not adv['name'] == 'Free Play') and (not 'Free Play (' in adv['name']):
  85. imgid = adv['graphic_id']
  86. for gfx in js_graphic:
  87. if imgid == gfx['id']:
  88. imgname = gfx['graphic']
  89. imgname = imgname.replace('/', '__')
  90. name = adv['name']
  91. fname_wiki = 'Icon_{name}.png'.format(name=name)
  92. fname_orig = '{imgname}.png'.format(imgname=imgname)
  93. fname_crop = '{imgname}_cropped.png'.format(imgname=imgname)
  94. fname_orig_path = 'images/' + fname_orig
  95. fname_crop_path = 'images/' + fname_crop
  96. if PROCESS:
  97. if REDOWNLOAD or (not os.path.isfile(fname_orig_path)):
  98. pathname = fname_orig.replace('__', '//').replace('.png', '')
  99. file = requests.get('http://ps5.idlechampions.com/~idledragons/mobile_assets/{pathname}'.format(pathname=pathname))
  100. with open('/tmp/IC_temp', 'wb') as t:
  101. t.write(file.content)
  102. with open('/tmp/IC_temp', 'rb') as t:
  103. current_file = t.read()
  104. # current_file = t.read()
  105. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', current_file, re.MULTILINE|re.DOTALL)
  106. if res is not None:
  107. with open(fname_orig_path, 'wb') as g:
  108. g.write(res.group())
  109. else:
  110. data = zlib.decompress(current_file)
  111. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', data, re.MULTILINE|re.DOTALL)
  112. with open(fname_orig_path, 'wb') as g:
  113. g.write(res.group())
  114. result1 = subprocess.run(['convert', fname_orig_path, '-trim', fname_crop_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  115. result2 = subprocess.run(['pngcrush', '-brute', '-rem', 'alla', '-rem', 'allb', '-rem', 'text', '-reduce', '-check', '-q', '-s', '-ow', fname_crop_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  116. if COMPARE:
  117. dir_path = os.path.dirname(os.path.realpath(__file__))
  118. if not os.path.isdir(os.path.join(dir_path, 'output')):
  119. os.makedirs(os.path.join(dir_path, 'output'))
  120. main_file = os.path.join(dir_path, 'images/' + '{filename}'.format(filename=fname_crop))
  121. posted_file = os.path.join(dir_path, 'images/' + 'posted/{filename}'.format(filename=fname_wiki))
  122. try:
  123. result = filecmp.cmp(main_file, posted_file, shallow=False)
  124. except FileNotFoundError as e:
  125. print('{name}: No source found'.format(name=name))
  126. if not main_file:
  127. print('{name}: {main_file} does not exist'.format(name=name, main_file=main_file))
  128. result = None
  129. if (result is not None) and (result == True):
  130. print('{name}: No changes'.format(name=name))
  131. else:
  132. print('{name}: Changes detected!'.format(name=name))
  133. # if SHOW_CHANGES:
  134. # result = subprocess.run(['git', 'diff', '--no-index', posted_file, main_file])
  135. if POST:
  136. if _summary is None:
  137. _summary = input('Enter change summary: ')
  138. EDIT_PARAMS = {
  139. 'action': 'upload',
  140. 'filename': fname_wiki,
  141. 'ignorewarnings': 1,
  142. 'text': LICENSE,
  143. 'format': 'json',
  144. 'bot': '1',
  145. 'summary': _summary,
  146. # 'nocreate': '1',
  147. }
  148. FILE = {'file': (fname_wiki, open(fname_crop_path, 'rb'), 'multipart/form-data')}
  149. print('{name}: Posting...'.format(name=name))
  150. R2 = instance.post(data=EDIT_PARAMS, files=FILE)
  151. if R2.status_code == 200:
  152. if 'error' not in R2.json():
  153. print('{name}: Success!'.format(name=name))
  154. result1 = subprocess.run(['cp', main_file, posted_file])
  155. else:
  156. print('{name}: FAIL! Error Message: {error}'.format(name=name, error=R2.json()['error']['info']))
  157. # print(R2.json())
  158. if R2.json()['error']['code'] == 'fileexists-no-change':
  159. result1 = subprocess.run(['cp', main_file, posted_file])
  160. else:
  161. print('{name}: FAIL!'.format(name=name))
  162. # break