Skip to content

Commit b72e45c

Browse files
authored
fix(translator): Change IP detection from JSON to XML format (#117)
Updated IP detection to use XML instead of JSON for country code retrieval. See renfei/feedback#3, the api now only support xml format.
1 parent d6cf7d1 commit b72e45c

1 file changed

Lines changed: 11 additions & 11 deletions

File tree

tools/translation/translator.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Author: Elysia
55
'''
66
import locale
7-
import json
7+
import xml.etree.ElementTree as ET
88
import os
99
import tools.base
1010
from tools.base import CmdTask
@@ -77,21 +77,21 @@ def isCN(self) -> bool:
7777

7878
def getLocalFromIP(self) -> str:
7979
local_str = ""
80-
temp_file = "/tmp/fishros_check_country.json"
80+
temp_file = "/tmp/fishros_check_country.xml"
8181
try:
8282
# Add timeout for IP detection
83-
result = subprocess.run(["wget", "--header=Accept: application/json", "--no-check-certificate",
83+
result = subprocess.run(["wget", "--header=Accept: application/xml", "--no-check-certificate",
8484
"https://ip.renfei.net/", "-O", temp_file, "-qq", "--timeout=10"],
8585
capture_output=True, text=True, timeout=15)
8686
if result.returncode == 0:
87-
with open(temp_file, 'r') as json_file:
88-
data = json.loads(json_file.read())
89-
self.ip_info = data
90-
self.country = data['location']['countryCode']
91-
if data['location']['countryCode'] in COUNTRY_CODE_MAPPING:
92-
local_str = COUNTRY_CODE_MAPPING[data['location']['countryCode']]
93-
else:
94-
local_str = "en_US"
87+
with open(temp_file, 'r') as xml_file:
88+
self.ip_info = xml_file.read()
89+
root = ET.fromstring(self.ip_info)
90+
self.country = root.find('location/countryCode').text
91+
if self.country in COUNTRY_CODE_MAPPING:
92+
local_str = COUNTRY_CODE_MAPPING[self.country]
93+
else:
94+
local_str = "en_US"
9595
else:
9696
local_str = "en_US"
9797
except Exception:

0 commit comments

Comments
 (0)