idlechampchestimages.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. set_chests = set()
  73. breakout = 0
  74. LICENSE = '''
  75. == Licensing ==
  76. {{Copyright game}}'''
  77. for chest in js_chest_type:
  78. if chest['id'] not in set_chests:
  79. set_chests.add(chest['id'])
  80. name = chest['name']
  81. if chest['id'] == 63:
  82. name += ' (Farideh)'
  83. if chest['id'] == 112:
  84. name += ' (Ishi)'
  85. print(chest['id'], chest['name'])
  86. if not re.search('E\d', name):
  87. imgid = chest['graphic_id']
  88. for gfx in js_graphic:
  89. if imgid == gfx['id']:
  90. imgname = gfx['graphic']
  91. imgname = imgname.replace('/', '__')
  92. fname_wiki = 'Icon_{name}.png'.format(name=name)
  93. fname_orig = '{imgname}.png'.format(imgname=imgname)
  94. fname_crop = '{imgname}_cropped.png'.format(imgname=imgname)
  95. fname_orig_path = 'images/' + fname_orig
  96. fname_crop_path = 'images/' + fname_crop
  97. # print(name)
  98. # print(imgname)
  99. # print()
  100. if PROCESS:
  101. if REDOWNLOAD or (not os.path.isfile(fname_orig_path)):
  102. pathname = fname_orig.replace('__', '//').replace('.png', '')
  103. file = requests.get(
  104. 'http://ps5.idlechampions.com/~idledragons/mobile_assets/{pathname}'.format(pathname=pathname))
  105. with open('/tmp/IC_temp', 'wb') as t:
  106. t.write(file.content)
  107. with open('/tmp/IC_temp', 'rb') as t:
  108. current_file = t.read()
  109. # current_file = t.read()
  110. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)',
  111. current_file, re.MULTILINE | re.DOTALL)
  112. if res is not None:
  113. with open(fname_orig_path, 'wb') as g:
  114. g.write(res.group())
  115. else:
  116. data = zlib.decompress(current_file)
  117. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)',
  118. data, re.MULTILINE | re.DOTALL)
  119. with open(fname_orig_path, 'wb') as g:
  120. g.write(res.group())
  121. result1 = subprocess.run(['convert', fname_orig_path, '-alpha', 'extract', '-auto-level', '-negate', '-threshold', '80%', '-negate', '-type', 'bilevel', '-define',
  122. 'connected-components:area-threshold=1000', '-define', 'connected-components:verbose=true', '-connected-components', '4', 'null:'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  123. # print(result1.stdout.decode())
  124. res = re.search(
  125. b'((?P<width>\d+)x(?P<height>\d+)\+(?P<offset_x>\d+)\+(?P<offset_y>\d+)).*?gray\(255\)', result1.stdout)
  126. # print(res.groups())
  127. # print(res.group('width'))
  128. # print(res.group('height'))
  129. # print(res.group('offset_x'))
  130. # print(res.group('offset_y'))
  131. result2 = subprocess.run(
  132. [
  133. 'convert',
  134. fname_orig_path,
  135. '-crop',
  136. '{w}x{h}+{x}+{y}'.format(
  137. w=res.group('width').decode(),
  138. h=res.group('height').decode(),
  139. x=res.group('offset_x').decode(),
  140. y=res.group('offset_y').decode()
  141. ),
  142. fname_crop_path
  143. ],
  144. stdout=subprocess.PIPE,
  145. stderr=subprocess.PIPE
  146. )
  147. if result2.stderr:
  148. print(result2.stderr)
  149. result3 = subprocess.run(['pngcrush', '-brute', '-rem', 'alla', '-rem', 'allb', '-rem', 'text', '-reduce', '-check', '-q', '-s', '-ow', fname_crop_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  150. if COMPARE:
  151. dir_path = os.path.dirname(os.path.realpath(__file__))
  152. if not os.path.isdir(os.path.join(dir_path, 'output')):
  153. os.makedirs(os.path.join(dir_path, 'output'))
  154. main_file = os.path.join(dir_path, 'images/' +
  155. '{filename}'.format(filename=fname_crop))
  156. posted_file = os.path.join(dir_path, 'images/' +
  157. 'posted/{filename}'.format(filename=fname_wiki))
  158. try:
  159. result = filecmp.cmp(main_file, posted_file, shallow=False)
  160. except FileNotFoundError as e:
  161. print('{name}: No source found'.format(name=name))
  162. if not main_file:
  163. print('{name}: {main_file} does not exist'.format(
  164. name=name, main_file=main_file))
  165. result = None
  166. if (result is not None) and (result == True):
  167. print('{name}: No changes'.format(name=name))
  168. else:
  169. print('{name}: Changes detected!'.format(name=name))
  170. # if SHOW_CHANGES:
  171. # result = subprocess.run(['git', 'diff', '--no-index', posted_file, main_file])
  172. if POST:
  173. if _summary is None:
  174. _summary = input('Enter change summary: ')
  175. EDIT_PARAMS = {
  176. 'action': 'upload',
  177. 'filename': fname_wiki,
  178. 'ignorewarnings': 1,
  179. 'text': LICENSE,
  180. 'format': 'json',
  181. 'bot': '1',
  182. 'summary': _summary,
  183. # 'nocreate': '1',
  184. }
  185. FILE = {'file': (fname_wiki, open(
  186. fname_crop_path, 'rb'), 'multipart/form-data')}
  187. print('{name}: Posting...'.format(name=name))
  188. R2 = instance.post(data=EDIT_PARAMS, files=FILE)
  189. if R2.status_code == 200:
  190. if 'error' not in R2.json():
  191. print('{name}: Success!'.format(name=name))
  192. result1 = subprocess.run(['cp', main_file, posted_file])
  193. else:
  194. print('{name}: FAIL! Error Message: {error}'.format(
  195. name=name, error=R2.json()['error']['info']))
  196. # print(R2.json())
  197. if R2.json()['error']['code'] == 'fileexists-no-change':
  198. result1 = subprocess.run(['cp', main_file, posted_file])
  199. else:
  200. print('{name}: FAIL!'.format(name=name))
  201. # break