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 email
|
||||
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()
|
||||
@ -31,30 +36,49 @@ def get_gpx_files_from_mail():
|
||||
|
||||
mail.create(config['mail']['mailbox_dir'])
|
||||
mail.select()
|
||||
(resp, ids) = mail.search(None, fitotrack_msg_filter)
|
||||
_, ids = mail.search(None, fitotrack_msg_filter)
|
||||
ids = ids[0].split()
|
||||
for i in ids:
|
||||
resp, fetched = mail.fetch(i, '(RFC822)')
|
||||
_, fetched = mail.fetch(i, '(RFC822)')
|
||||
email_message = email.message_from_bytes(fetched[0][1])
|
||||
for part in email_message.walk():
|
||||
if part.get_content_maintype() == 'multipart' or part.get_content_disposition() is None:
|
||||
continue
|
||||
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:
|
||||
f.write(part.get_payload(decode=True))
|
||||
|
||||
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()
|
||||
get_gpx_files_from_mail()
|
||||
db.transaction(process_gpx_files, owner)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
main()
|
||||
main(sys.argv[1])
|
||||
except IndexError:
|
||||
print('Run the script with "python main.py OWNER_NAME"')
|
||||
except (KeyboardInterrupt, EOFError):
|
||||
pass
|
||||
|
Loading…
x
Reference in New Issue
Block a user