forked from LiaungYip/Cat-Herder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atlauncher_share_code.py
68 lines (55 loc) · 2.32 KB
/
atlauncher_share_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Li-aung "Lewis" Yip
"""
import urllib2
import re
import json
from atlauncher_import import atlauncher_to_catherder
def get_share_code_json (share_code):
assert re.match("[A-Za-z0-9]{8}", share_code)
url = "https://api.atlauncher.com/v1/share-codes/" + share_code
try:
d = urllib2.urlopen(url)
except urllib2.HTTPError, e:
if e.code == 404:
print "ERROR - HTTP 404 while retrieving share code - {u}".format(u = url)
raise
j = d.read()
d.close()
return j
def process_share_code_json (json_string):
# Returns a dict of 'pack', 'pack_version', and 'selected_mods'.
# 'selected_mods' is a list of the optional mods to be installed.
json_data = json.loads(json_string)
assert json_data['error'] == False
assert json_data['code'] == 200 # OK
# Note that the /data/mods/ node of the json_data itself contains JSON.
# It's JSON all the way down, man.
#json_mods_data = json.loads(json_data['data']['mods'])
# Apparently some recent upgrade to Python or the JSON module broke
# the above code. Try this instead.
json_mods_data = json_data['data']['mods']
optional_mods = json_mods_data['optional']
selected_mods = [m['name'] for m in optional_mods if m['selected'] == True]
return dict (pack = json_data['data']['pack'],
pack_version = json_data['data']['version'],
selected_mods = selected_mods)
def get_mod_pack_with_share_code (share_code, download_cache_folder, install_folder):
# Returns a Mod_Pack object.
#
# The pack name and pack version are implied by the share code, so these
# don't have to be provided externally.
#
# The 'install_optional?' flag is set on each of the Mod_Pack's
# optional mods, based on the 'selected' mods in the share code data.
sc_json_str = get_share_code_json (share_code)
r = process_share_code_json (sc_json_str)
mp = atlauncher_to_catherder(pack_name = r['pack'],
pack_version = r['pack_version'],
download_cache_folder=download_cache_folder,
install_folder=install_folder)
for m in mp.mod_files:
if m['optional?']:
m['install_optional?'] = m['name'] in r['selected_mods']
return mp