gpxpy parsing and DB transaction
This commit is contained in:
parent
b2364b6252
commit
2e133fdb3e
36
main.py
36
main.py
@ -3,8 +3,13 @@ from imaplib import IMAP4
|
|||||||
import ssl
|
import ssl
|
||||||
import email
|
import email
|
||||||
import os
|
import os
|
||||||
|
from glob import glob
|
||||||
|
import sys
|
||||||
|
from gpxpy import gpx
|
||||||
|
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, text
|
||||||
|
from sqlalchemy.engine import Connection
|
||||||
|
import gpxpy
|
||||||
|
|
||||||
|
|
||||||
config = configparser.ConfigParser()
|
config = configparser.ConfigParser()
|
||||||
@ -31,30 +36,49 @@ def get_gpx_files_from_mail():
|
|||||||
|
|
||||||
mail.create(config['mail']['mailbox_dir'])
|
mail.create(config['mail']['mailbox_dir'])
|
||||||
mail.select()
|
mail.select()
|
||||||
(resp, ids) = mail.search(None, fitotrack_msg_filter)
|
_, ids = mail.search(None, fitotrack_msg_filter)
|
||||||
ids = ids[0].split()
|
ids = ids[0].split()
|
||||||
for i in ids:
|
for i in ids:
|
||||||
resp, fetched = mail.fetch(i, '(RFC822)')
|
_, fetched = mail.fetch(i, '(RFC822)')
|
||||||
email_message = email.message_from_bytes(fetched[0][1])
|
email_message = email.message_from_bytes(fetched[0][1])
|
||||||
for part in email_message.walk():
|
for part in email_message.walk():
|
||||||
if part.get_content_maintype() == 'multipart' or part.get_content_disposition() is None:
|
if part.get_content_maintype() == 'multipart' or part.get_content_disposition() is None:
|
||||||
continue
|
continue
|
||||||
filename = part.get_filename()
|
filename = part.get_filename()
|
||||||
|
|
||||||
if filename:
|
if filename and not os.path.exists(f'gpx_files/{filename}'):
|
||||||
with open(os.path.join('gpx_files', filename), 'wb') as f:
|
with open(os.path.join('gpx_files', filename), 'wb') as f:
|
||||||
f.write(part.get_payload(decode=True))
|
f.write(part.get_payload(decode=True))
|
||||||
|
|
||||||
mail.logout()
|
mail.logout()
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def process_gpx_files(tx: Connection, owner: str):
|
||||||
|
for filepath in glob('gpx_files/*.gpx'):
|
||||||
|
filename = os.path.split(filepath)[-1]
|
||||||
|
if list(db.execute(text('select exists(select from training where filename = :filename)'), dict(filename=filename)))[0][0]:
|
||||||
|
continue
|
||||||
|
with open(filepath) as f:
|
||||||
|
gpx_file = gpxpy.parse(f)
|
||||||
|
if gpx_file.creator != 'FitoTrack':
|
||||||
|
raise ValueError('gpx file not generated by the FitoTrack app')
|
||||||
|
training_id = list(db.execute(
|
||||||
|
'insert into training (owner, filename, medium, description) values (:owner, :filename, :medium, :description) returning id',
|
||||||
|
(owner, filename, )
|
||||||
|
))[0][0]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def main(owner: str):
|
||||||
init_database()
|
init_database()
|
||||||
get_gpx_files_from_mail()
|
get_gpx_files_from_mail()
|
||||||
|
db.transaction(process_gpx_files, owner)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
try:
|
try:
|
||||||
main()
|
main(sys.argv[1])
|
||||||
|
except IndexError:
|
||||||
|
print('Run the script with "python main.py OWNER_NAME"')
|
||||||
except (KeyboardInterrupt, EOFError):
|
except (KeyboardInterrupt, EOFError):
|
||||||
pass
|
pass
|
||||||
|
Loading…
x
Reference in New Issue
Block a user