idlechampportrait.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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']) and not len(hero['name']) < 2:
  63. imgid = hero['portrait_graphic_id']
  64. for gfx in js_graphic:
  65. if imgid == gfx['id']:
  66. imgname = gfx['graphic']
  67. imgname = imgname.replace('/', '__')
  68. name = hero['name']
  69. fname_wiki = 'Icon_{name}.png'.format(name=name)
  70. # fname_orig = 'images/Portraits__Portrait_{name}.png'.format(name=name)
  71. fname_orig = '{imgname}.png'.format(imgname=imgname)
  72. # fname_crop = 'images/Portraits__Portrait_{name}_cropped.png'.format(name=name)
  73. fname_crop = '{imgname}_cropped.png'.format(imgname=imgname)
  74. fname_orig_path = 'images/' + fname_orig
  75. fname_crop_path = 'images/' + fname_crop
  76. # print(fname_wiki)
  77. # print(fname_orig_path)
  78. # print(fname_crop_path)
  79. # print('os.path.isfile(fname_orig_path)', os.path.isfile(fname_orig_path))
  80. if PROCESS:
  81. if REDOWNLOAD or (not os.path.isfile(fname_orig_path)):
  82. pathname = fname_orig.replace('__', '//').replace('.png', '')
  83. file = requests.get('http://ps5.idlechampions.com/~idledragons/mobile_assets/{pathname}'.format(pathname=pathname))
  84. with open('/tmp/IC_temp', 'wb') as t:
  85. t.write(file.content)
  86. with open('/tmp/IC_temp', 'rb') as t:
  87. current_file = t.read()
  88. # current_file = t.read()
  89. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', current_file, re.MULTILINE|re.DOTALL)
  90. if res is not None:
  91. with open(fname_orig_path, 'wb') as g:
  92. g.write(res.group())
  93. else:
  94. data = zlib.decompress(current_file)
  95. res = re.search(b'(\\x89\\x50\\x4e\\x47\\x0d\\x0a\\x1a\\x0a.*)', data, re.MULTILINE|re.DOTALL)
  96. with open(fname_orig_path, 'wb') as g:
  97. g.write(res.group())
  98. result1 = subprocess.run(['convert', fname_orig_path, '-trim', fname_crop_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  99. if result1.stderr:
  100. print(result1.stderr)
  101. 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)
  102. if COMPARE:
  103. dir_path = os.path.dirname(os.path.realpath(__file__))
  104. if not os.path.isdir(os.path.join(dir_path, 'output')):
  105. os.makedirs(os.path.join(dir_path, 'output'))
  106. main_file = os.path.join(dir_path, 'images/' + '{filename}'.format(filename=fname_crop))
  107. posted_file = os.path.join(dir_path, 'images/' + 'posted/{filename}'.format(filename=fname_wiki))
  108. try:
  109. result = filecmp.cmp(main_file, posted_file, shallow=False)
  110. except FileNotFoundError as e:
  111. print('{name}: No source found'.format(name=name))
  112. if not main_file:
  113. print('{name}: {main_file} does not exist'.format(name=name, main_file=main_file))
  114. result = None
  115. if (result is not None) and (result == True):
  116. print('{name}: No changes'.format(name=name))
  117. else:
  118. print('{name}: Changes detected!'.format(name=name))
  119. # if SHOW_CHANGES:
  120. # result = subprocess.run(['git', 'diff', '--no-index', posted_file, main_file])
  121. if POST:
  122. if _summary is None:
  123. _summary = input('Enter change summary: ')
  124. EDIT_PARAMS = {
  125. 'action': 'upload',
  126. 'filename': fname_wiki,
  127. 'ignorewarnings': 1,
  128. 'text': LICENSE,
  129. 'format': 'json',
  130. 'bot': '1',
  131. 'summary': _summary,
  132. # 'nocreate': '1',
  133. }
  134. FILE = {'file': (fname_wiki, open(fname_crop_path, 'rb'), 'multipart/form-data')}
  135. print('{name}: Posting...'.format(name=name))
  136. R2 = instance.post(data=EDIT_PARAMS, files=FILE)
  137. if R2.status_code == 200:
  138. if 'error' not in R2.json():
  139. print('{name}: Success!'.format(name=name))
  140. result1 = subprocess.run(['cp', main_file, posted_file])
  141. else:
  142. print('{name}: FAIL! Error Message: {error}'.format(name=name, error=R2.json()['error']['info']))
  143. else:
  144. print('{name}: FAIL!'.format(name=name))
  145. # break