[birds] Move importer to scrobbles and webdav
This commit is contained in:
@ -17,6 +17,7 @@ DEFAULT_GPX_PATH = "var/gpx/"
|
||||
DEFAULT_KOREADER_PATH = "var/koreader/"
|
||||
DEFAULT_RETROARCH_PATH = "var/retroarch/"
|
||||
DEFAULT_BGSTATS_PATH = "var/bgstats/"
|
||||
DEFAULT_EBIRD_PATH = "var/ebird/"
|
||||
|
||||
|
||||
def import_from_webdav_for_all_users(restart=False):
|
||||
@ -33,6 +34,7 @@ def import_from_webdav_for_all_users(restart=False):
|
||||
gpx_count = 0
|
||||
retro_count = 0
|
||||
bgstats_count = 0
|
||||
ebird_count = 0
|
||||
|
||||
for user_id in webdav_enabled_user_ids:
|
||||
client = get_webdav_client(user_id)
|
||||
@ -40,6 +42,7 @@ def import_from_webdav_for_all_users(restart=False):
|
||||
gpx_count += scan_webdav_for_gpx(client, user_id)
|
||||
retro_count += scan_webdav_for_retroarch(client, user_id)
|
||||
bgstats_count += scan_webdav_for_bgstats(client, user_id)
|
||||
ebird_count += scan_webdav_for_ebird(client, user_id)
|
||||
|
||||
logger.info(
|
||||
"WebDAV import complete",
|
||||
@ -48,9 +51,10 @@ def import_from_webdav_for_all_users(restart=False):
|
||||
"trail_gpx": gpx_count,
|
||||
"retroarch": retro_count,
|
||||
"bgstats": bgstats_count,
|
||||
"ebird": ebird_count,
|
||||
},
|
||||
)
|
||||
return ko_count, gpx_count, retro_count, bgstats_count
|
||||
return ko_count, gpx_count, retro_count, bgstats_count, ebird_count
|
||||
|
||||
|
||||
def scan_webdav_for_koreader(webdav_client, user_id, restart=False):
|
||||
@ -343,3 +347,60 @@ def scan_webdav_for_bgstats(webdav_client, user_id):
|
||||
os.unlink(tmp.name)
|
||||
|
||||
return new_imports
|
||||
|
||||
|
||||
def scan_webdav_for_ebird(webdav_client, user_id):
|
||||
"""Download .csv files from WebDAV var/ebird/ and queue imports for new files."""
|
||||
from scrobbles.models import EBirdCSVImport
|
||||
from scrobbles.tasks import process_ebird_csv_import
|
||||
|
||||
ebird_path = DEFAULT_EBIRD_PATH
|
||||
try:
|
||||
webdav_client.info(ebird_path)
|
||||
except:
|
||||
logger.info("No var/ebird/ directory on webdav", extra={"user_id": user_id})
|
||||
return 0
|
||||
|
||||
try:
|
||||
files = webdav_client.list(ebird_path)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"Could not list var/ebird/",
|
||||
extra={"user_id": user_id, "error": str(e)},
|
||||
)
|
||||
return 0
|
||||
|
||||
new_imports = 0
|
||||
already_imported = set(
|
||||
EBirdCSVImport.objects.filter(user_id=user_id).values_list(
|
||||
"original_filename", flat=True
|
||||
)
|
||||
)
|
||||
|
||||
for fname in files:
|
||||
fname = os.path.basename(fname)
|
||||
if not fname.lower().endswith(".csv"):
|
||||
continue
|
||||
if fname in already_imported:
|
||||
logger.debug(f"Skipping already-imported {fname}")
|
||||
continue
|
||||
|
||||
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=fname)
|
||||
try:
|
||||
webdav_client.download_sync(
|
||||
remote_path=f"{ebird_path}/{fname}", local_path=tmp.name
|
||||
)
|
||||
imp = EBirdCSVImport.objects.create(
|
||||
user_id=user_id,
|
||||
original_filename=fname,
|
||||
)
|
||||
with open(tmp.name, "rb") as f:
|
||||
imp.csv_file.save(fname, f, save=True)
|
||||
process_ebird_csv_import.delay(imp.id)
|
||||
new_imports += 1
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to import eBird CSV file {fname}: {e}")
|
||||
finally:
|
||||
os.unlink(tmp.name)
|
||||
|
||||
return new_imports
|
||||
|
||||
Reference in New Issue
Block a user