Debian Codegraph @

src/location/kylin-location

raw symbols SCIP
1 #!/usr/bin/python3
2 #
3 # libkysdk-system's Library
4 #
5 # Copyright (C) 2023-2025, KylinSoft Co., Ltd.
6 #
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 3 of the License, or (at your option) any later version.
11 #
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this library. If not, see <https://www.gnu.org/licenses/>.
19 #
20 # Authors: Yunhe Liu <[email protected]>
21 #
22 #
23
24 # -*- coding: UTF-8 -*-
25 from base64 import decode
26 from email.utils import getaddresses
27 from time import sleep
28 from traceback import print_tb
29 from urllib.request import urlopen
30 import requests
31 import json
32 import re
33
34 import dbus
35 import dbus.service
36 from dbus.mainloop.glib import DBusGMainLoop
37 from gi.repository import GLib
38
39 import logging
40
41 logging.basicConfig(filename='/var/log/kysdk-location.log',
42 level=logging.DEBUG,
43 format='%(asctime)s.%(msecs)03d - %(name)s - %(levelname)s - %(message)s',
44 datefmt='%Y-%m-%d %H:%M:%S')
45 loggre = logging.getLogger()
46
47 class AddressBus(dbus.service.Object):
48
49 def __init__(self, conn=None, object_path=None):
50 dbus.service.Object.__init__(self, conn, object_path)
51 self._last_input = None
52
53 def getaddr(self):
54 text = str(urlopen("http://txt.go.sohu.com/ip/soip").read())
55 ip = re.findall(r'\d+.\d+.\d+.\d+',text)
56 return ip[0]
57
58 @dbus.service.method('com.kysdk.location', out_signature='s')
59 def getAddress(self):
60 url = "http://api.map.baidu.com/location/ip"
61 myParams = {"ak":"4DPAeozisfSCUgSLuC14Ux4iXjeEYNGa","ip":self.getaddr(),"coor":"bd09ll"}
62 res = requests.get(url=url,params=myParams)
63 if res.status_code != 200:
64 return "Error Code" + res.status_code
65 decoded_u = res.text.encode('utf-8').decode('unicode_escape')
66 data = json.loads(decoded_u)
67 if data["status"] == 0 :
68 return decoded_u
69 else:
70 return data["message"]
71
72 if __name__ == '__main__' :
73 try:
74 loggre.info("location start")
75
76 # 初始化DBus主循环
77 DBusGMainLoop(set_as_default=True)
78
79 system_bus = dbus.SystemBus()
80 name = dbus.service.BusName("com.kysdk.location",system_bus)
81 object_path = "/com/kysdk/location"
82 obj = AddressBus(system_bus, object_path)
83 loggre.info("location started")
84
85 # 开始DBus主循环
86 mainloop = GLib.MainLoop()
87 mainloop.run()
88 except Exception as e:
89
90 loggre.info(e)