This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
ffxiv_api.py
214 lines (159 loc) · 6.46 KB
/
ffxiv_api.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import logging
import json
import os
import random
import string
import sys
import pprint
import threading
import tempfile
import modules.urllib3 as urllib3
from urllib.parse import parse_qs
from typing import Dict, List
from http.server import HTTPServer, BaseHTTPRequestHandler
from enum import Enum
modules = os.path.join(os.path.dirname(os.path.realpath(__file__)),'modules\\')
if modules not in sys.path:
sys.path.insert(0, modules)
import modules.requests as requests
class FFXIVAuthorizationResult(Enum):
FAILED = 0
FAILED_INVALID_CHARACTER_ID = 1
FINISHED = 2
class FFXIVAuthorizationServer(BaseHTTPRequestHandler):
backend = None
def do_HEAD(self):
return
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
post_data = parse_qs(post_data)
except:
pass
if self.path == '/login':
self.do_POST_login(post_data)
else:
self.send_response(302)
self.send_header('Location','/404')
self.end_headers()
def do_POST_login(self, data):
data_valid = True
if b'character_id' not in data:
data_valid = False
auth_result = False
if data_valid:
try:
auth_result = self.backend.do_auth_character(data[b'character_id'][0].decode("utf-8"))
except Exception:
logging.exception("error on doing auth:")
self.send_response(302)
self.send_header('Content-type', "text/html")
if auth_result == FFXIVAuthorizationResult.FINISHED:
self.send_header('Location','/finished')
else:
self.send_header('Location','/login_failed')
self.end_headers()
def do_GET(self):
status = 200
content_type = "text/html"
response_content = ""
try:
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'html\\%s.html' % self.path)
if os.path.isfile(filepath):
response_content = open(filepath).read()
else:
filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),'html\\404.html')
if os.path.isfile(filepath):
response_content = open(filepath).read()
else:
response_content = 'ERROR: FILE NOT FOUND'
self.send_response(status)
self.send_header('Content-type', content_type)
self.end_headers()
self.wfile.write(bytes(response_content, "UTF-8"))
except Exception:
logging.exception('FFXIVAuthorizationServer/do_GET: error on %s' % self.path)
class FFXIVAPI(object):
API_DOMAIN = 'https://xivapi.com/'
API_URL_CHARACTER = 'character/'
LOCALSERVER_HOST = '127.0.0.1'
LOCALSERVER_PORT = 13338
INSTALL_URL = "http://gdl.square-enix.com/ffxiv/inst/ffxivsetup.exe"
def __init__(self):
self._server_thread = None
self._server_object = None
self._character_id = None
self._account_info = None
#
# Getters
#
def get_character_id(self) -> str:
return self._character_id
def get_character(self) -> List[str]:
return self._account_info['Character']
def get_character_name(self) -> str:
return self._account_info['Character']['Name']
def get_account_achievements(self) -> List[str]:
return self._account_info['Achievements']['List']
def get_account_friends(self) -> List[str]:
return self._account_info['Friends']
#
# Authorization server
#
def auth_server_uri(self) -> str:
return 'http://%s:%s/login' % (self.LOCALSERVER_HOST, self.LOCALSERVER_PORT)
def auth_server_start(self) -> bool:
if self._server_thread is not None:
logging.warning('FFXIVAuthorization/auth_server_start: Auth server thread is already running')
return False
if self._server_object is not None:
logging.warning('FFXIVAuthorization/auth_server_start: Auth server object is exists')
return False
FFXIVAuthorizationServer.backend = self
self._server_object = HTTPServer((self.LOCALSERVER_HOST, self.LOCALSERVER_PORT), FFXIVAuthorizationServer)
self._server_thread = threading.Thread(target = self._server_object.serve_forever)
self._server_thread.daemon = True
self._server_thread.start()
return True
def auth_server_stop(self) -> bool:
if self._server_object is not None:
self._server_object.shutdown()
self._server_object = None
else:
logging.warning('FFXIVAuthorization/auth_server_stop: Auth server object is not exits')
return False
if self._server_thread is not None:
self._server_thread.join()
self._server_thread = None
else:
logging.warning('FFXIVAuthorization/auth_server_stop: Auth server thread is not running')
return False
def do_auth_character(self, character_id : str) -> FFXIVAuthorizationResult:
(status_code, account_info) = self.__api_get_account_info(character_id)
if account_info is None:
return FFXIVAuthorizationResult.FAILED
if status_code != 200:
if 'Error' not in account_info:
return FFXIVAuthorizationResult.FAILED
if account_info['Ex'] == 'Lodestone\\Exceptions\\LodestoneNotFoundException':
return FFXIVAuthorizationResult.FAILED_INVALID_CHARACTER_ID
return FFXIVAuthorizationResult.FAILED
self._character_id = character_id
self._account_info = account_info
return FFXIVAuthorizationResult.FINISHED
def __api_get_account_info(self, character_id : str):
resp = requests.get(self.API_DOMAIN + self.API_URL_CHARACTER + character_id, params={'data': 'AC,FR'})
result = None
try:
result = json.loads(resp.text)
except Exception:
logging.error('ffxivapi/__api_get_account_info: %s' % resp.text)
return (resp.status_code, result)
def get_installer(self):
installer_path = os.path.join(tempfile.mkdtemp(), "ffxivsetup.exe")
resp = requests.get(self.INSTALL_URL)
if resp.status_code == 200:
with open(installer_path, 'wb') as f:
f.write(resp.content)
return installer_path