|
@@ -1,6 +1,6 @@
|
|
#!/data/data/com.termux/files/usr/bin/python3
|
|
#!/data/data/com.termux/files/usr/bin/python3
|
|
|
|
|
|
-import datetime
|
|
+import datetime as dt
|
|
import json
|
|
import json
|
|
import os
|
|
import os
|
|
import pytz
|
|
import pytz
|
|
@@ -14,20 +14,56 @@ def to_iso6801_basic(dt):
|
|
.replace('+0000', 'Z')
|
|
.replace('+0000', 'Z')
|
|
|
|
|
|
|
|
|
|
|
|
+class Position:
|
|
|
|
+
|
|
|
|
+ def __init__(self, timestamp, latitude, longitude, altitude):
|
|
|
|
+ self.timestamp = timestamp
|
|
|
|
+ self.latitude = latitude
|
|
|
|
+ self.longitude = longitude
|
|
|
|
+ self.altitude = altitude
|
|
|
|
+
|
|
|
|
+ @classmethod
|
|
|
|
+ def from_termux_location(cls, provider='network'):
|
|
|
|
+ loc_json = subprocess.check_output(['termux-location', '-p', provider]) \
|
|
|
|
+ .decode(sys.stdout.encoding)
|
|
|
|
+ req_ts = dt.datetime.now(pytz.utc)
|
|
|
|
+ loc_attr = yaml.load(loc_json)
|
|
|
|
+ if not loc_attr:
|
|
|
|
+ return None
|
|
|
|
+ else:
|
|
|
|
+ print(loc_attr)
|
|
|
|
+ return cls(
|
|
|
|
+ timestamp=req_ts -
|
|
|
|
+ dt.timedelta(milliseconds=loc_attr['elapsedMs']),
|
|
|
|
+ latitude=loc_attr['latitude'],
|
|
|
|
+ longitude=loc_attr['longitude'],
|
|
|
|
+ altitude=loc_attr['altitude'],
|
|
|
|
+ )
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def symlink(src, dst, force=False):
|
|
|
|
+ if force and os.path.lexists(dst):
|
|
|
|
+ os.unlink(dst)
|
|
|
|
+ os.symlink(src=src, dst=dst)
|
|
|
|
+
|
|
|
|
+
|
|
def compute(target_dir_path):
|
|
def compute(target_dir_path):
|
|
- loc_json = subprocess.check_output(['termux-location', '-p', 'network']) \
|
|
+ cur = Position.from_termux_location()
|
|
- .decode(sys.stdout.encoding)
|
|
+ target_name = '{}.yml'.format(to_iso6801_basic(cur.timestamp))
|
|
- req_time = datetime.datetime.now(pytz.utc)
|
|
+ with open(os.path.join(target_dir_path, target_name), 'w') as f:
|
|
- loc_attr = yaml.load(loc_json)
|
|
|
|
- target_path = os.path.join(
|
|
|
|
- target_dir_path,
|
|
|
|
- '{}.yml'.format(to_iso6801_basic(req_time)),
|
|
|
|
- )
|
|
|
|
- with open(target_path, 'w') as f:
|
|
|
|
f.write(yaml.dump(
|
|
f.write(yaml.dump(
|
|
- loc_attr,
|
|
+ {
|
|
|
|
+ 'lat': cur.latitude,
|
|
|
|
+ 'lon': cur.longitude,
|
|
|
|
+ 'alt': cur.altitude,
|
|
|
|
+ },
|
|
default_flow_style=False,
|
|
default_flow_style=False,
|
|
))
|
|
))
|
|
|
|
+ symlink(
|
|
|
|
+ src=target_name,
|
|
|
|
+ dst=os.path.join(target_dir_path, 'last.yml'),
|
|
|
|
+ force=True,
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
def _init_argparser():
|
|
def _init_argparser():
|