idlechampportskin.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. image_dir = '/home/txtsd/.local/share/Steam/steamapps/common/IdleChampions/IdleDragons_Data/StreamingAssets/downloaded_files/'
  33. with open(filename) as f:
  34. file = f.read()
  35. js = json.loads(html.unescape(file))
  36. js_graphic = js['graphic_defines']
  37. js_attack = js['attack_defines']
  38. js_hero = js['hero_defines']
  39. js_hero_skin = js['hero_skin_defines']
  40. js_upgrade = js['upgrade_defines']
  41. js_premium_item = js['premium_item_defines']
  42. js_sound = js['sound_defines']
  43. js_buff = js['buff_defines']
  44. js_loot = js['loot_defines']
  45. js_achievement = js['achievement_defines']
  46. js_ability = js['ability_defines']
  47. js_effect = js['effect_defines']
  48. js_changelog = js['changelog_defines']
  49. js_text = js['text_defines']
  50. js_chest_type = js['chest_type_defines']
  51. js_effect_key = js['effect_key_defines']
  52. js_tutorial_state = js['tutorial_state_defines']
  53. js_game_rule = js['game_rule_defines']
  54. js_news = js['news_defines']
  55. js_language = js['language_defines']
  56. js_familiar = js['familiar_defines']
  57. breakout = 0
  58. LICENSE = '''
  59. == Licensing ==
  60. {{Copyright game}}'''
  61. for hero in js_hero:
  62. if not re.search('^E\d', hero['name']):
  63. for skin in js_hero_skin:
  64. if hero['id'] == skin['hero_id']:
  65. imgid = skin['details']['portrait_graphic_id']
  66. for gfx in js_graphic:
  67. if imgid == gfx['id']:
  68. imgname = gfx['graphic']
  69. imgname = imgname.replace('/', '__')
  70. name = skin['name']
  71. fname_wiki = 'Icon_{name}.png'.format(name=name)
  72. # fname_orig = 'images/Portraits__Portrait_{name}.png'.format(name=name)
  73. fname_orig = '{imgname}.png'.format(imgname=imgname)
  74. # fname_crop = 'images/Portraits__Portrait_{name}_cropped.png'.format(name=name)
  75. fname_crop = '{imgname}_cropped.png'.format(imgname=imgname)
  76. fname_orig_path = 'images/' + fname_orig
  77. fname_crop_path = 'images/' + fname_crop
  78. # print(fname_wiki)
  79. # print(fname_orig_path)
  80. # print(fname_crop_path)
  81. # print('os.path.isfile(fname_orig_path)', os.path.isfile(fname_orig_path))
  82. if PROCESS:
  83. if REDOWNLOAD or (not os.path.isfile(fname_orig_path)):
  84. pathname = fname_orig.replace('__', '//').replace('.png', '')
  85. file = requests.get('http://ps5.idlechampions.com/~idledragons/mobile_assets/{pathname}'.format(pathname=pathname))
  86. with open('/tmp/IC_temp', 'wb') as t:
  87. t.write(file.content)
  88. with open('/tmp/IC_temp', 'rb') as t:
  89. current_file = t.read()
  90. # current_file = t.read()
  91. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', current_file, re.MULTILINE|re.DOTALL)
  92. if res is not None:
  93. with open(fname_orig_path, 'wb') as g:
  94. g.write(res.group())
  95. else:
  96. data = zlib.decompress(current_file)
  97. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', data, re.MULTILINE|re.DOTALL)
  98. with open(fname_orig_path, 'wb') as g:
  99. g.write(res.group())
  100. result1 = subprocess.run(['convert', fname_orig_path, '-trim', fname_crop_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  101. if result1.stderr:
  102. print(result1.stderr)
  103. 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)
  104. if COMPARE:
  105. dir_path = os.path.dirname(os.path.realpath(__file__))
  106. if not os.path.isdir(os.path.join(dir_path, 'output')):
  107. os.makedirs(os.path.join(dir_path, 'output'))
  108. main_file = os.path.join(dir_path, 'images/' + '{filename}'.format(filename=fname_crop))
  109. posted_file = os.path.join(dir_path, 'images/' + 'posted/{filename}'.format(filename=fname_wiki))
  110. try:
  111. result = filecmp.cmp(main_file, posted_file, shallow=False)
  112. except FileNotFoundError as e:
  113. print('{name}: No source found'.format(name=name))
  114. if not main_file:
  115. print('{name}: {main_file} does not exist'.format(name=name, main_file=main_file))
  116. result = None
  117. if (result is not None) and (result == True):
  118. print('{name}: No changes'.format(name=name))
  119. else:
  120. print('{name}: Changes detected!'.format(name=name))
  121. # if SHOW_CHANGES:
  122. # result = subprocess.run(['git', 'diff', '--no-index', posted_file, main_file])
  123. if POST:
  124. if _summary is None:
  125. _summary = input('Enter change summary: ')
  126. EDIT_PARAMS = {
  127. 'action': 'upload',
  128. 'filename': fname_wiki,
  129. 'ignorewarnings': 1,
  130. 'text': LICENSE,
  131. 'format': 'json',
  132. 'bot': '1',
  133. 'summary': _summary,
  134. # 'nocreate': '1',
  135. }
  136. FILE = {'file': (fname_wiki, open(fname_crop_path, 'rb'), 'multipart/form-data')}
  137. print('{name}: Posting...'.format(name=name))
  138. R2 = instance.post(data=EDIT_PARAMS, files=FILE)
  139. if R2.status_code == 200:
  140. if 'error' not in R2.json():
  141. print('{name}: Success!'.format(name=name))
  142. result1 = subprocess.run(['cp', main_file, posted_file])
  143. else:
  144. print('{name}: FAIL! Error Message: {error}'.format(name=name, error=R2.json()['error']['info']))
  145. else:
  146. print('{name}: FAIL!'.format(name=name))
  147. # break