-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_geoloc.py
81 lines (71 loc) · 3.29 KB
/
util_geoloc.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
import requests
from flask import session
def get_country_code():
try:
# get ip
response = requests.get('https://api64.ipify.org?format=json', timeout=1).json()
except requests.exceptions.Timeout:
# default country
return 'GB'
ip_address = response["ip"]
response = requests.get(f'https://ipapi.co/{ip_address}/json/', timeout=1).json()
return response.get("country_code")
# response = requests.get(f'http://ip-api.com/json/{ip_address}', timeout=1).json()
# return response.get("countryCode")
def set_session_geo_lang():
if 'lang' not in session or 'country' not in session:
session['country'] = get_country_code()
# debug_out('geolocation:' + session['country'])
if session['country'] == 'TW':
session['lang'] = 'zh-tw'
# session['flag'] = session['country'].lower()
elif session['country'] == 'CN':
# session['flag'] = session['country'].lower()
session['lang'] = 'zh-cn'
elif session['country'] == 'JP':
# session['flag'] = session['country'].lower()
session['lang'] = 'ja'
elif session['country'] == 'ID':
# session['flag'] = session['country'].lower()
session['lang'] = 'id'
elif session['country'] == 'KO':
# session['flag'] = session['country'].lower()
session['lang'] = 'ko'
elif session['country'] == 'VN':
# session['flag'] = session['country'].lower()
session['lang'] = 'vn'
elif session['country'] == 'BR' or session['country'] == 'PT' or session['country'] == 'CV' or \
session['country'] == 'AO' or session['country'] == 'MZ' or session['country'] == 'GW' or \
session['country'] == 'TP':
session['lang'] = 'br'
# session['flag'] = 'br'
elif session['country'] == 'ES' or session['country'] == 'AR' or session['country'] == 'MX' or \
session['country'] == 'CO' or session['country'] == 'PE' or session['country'] == 'CL' or \
session['country'] == 'VE' or session['country'] == 'GT' or session['country'] == 'EC' or \
session['country'] == 'BO' or session['country'] == 'CU' or session['country'] == 'DM' or \
session['country'] == 'DO' or session['country'] == 'PY' or session['country'] == 'SV' or \
session['country'] == 'NI' or session['country'] == 'CR' or session['country'] == 'PA' or \
session['country'] == 'UY' or session['country'] == 'PR':
session['lang'] = 'es'
# session['flag'] = 'es'
else:
session['lang'] = 'en'
session['country'] = 'GB'
if session['lang'] == 'zh-tw':
session['flag'] = 'tw'
elif session['lang'] == 'zh-cn':
session['flag'] = 'cn'
elif session['lang'] == 'ja':
session['flag'] = 'jp'
elif session['lang'] == 'id':
session['flag'] = 'id'
elif session['lang'] == 'br':
session['flag'] = 'br'
elif session['lang'] == 'ko':
session['flag'] = 'ko'
elif session['lang'] == 'vn':
session['flag'] = 'vn'
elif session['lang'] == 'es':
session['flag'] = 'es'
elif session['lang'] == 'en':
session['flag'] = 'gb'