Skip to content

Instantly share code, notes, and snippets.

@RonjaPonja
Created June 30, 2017 16:49
Show Gist options
  • Select an option

  • Save RonjaPonja/2de0e44c02f664b28822ac3bdd538bd8 to your computer and use it in GitHub Desktop.

Select an option

Save RonjaPonja/2de0e44c02f664b28822ac3bdd538bd8 to your computer and use it in GitHub Desktop.
Find orphant stubnitz media archive dirs not linked to any event in the db.
#!/usr/bin/env python
import os, re
from mysql.connector import MySQLConnection, Error
__author__ = 'Patrick <space> Meyer'
ARCHIVE_MOUNT = '/var/www/'
EVENT_DIR_RE = ARCHIVE_MOUNT + r'current-archive\/(audio|video|bilder)\/[0-9]+\/[0-9]+\/[0-9]+\/[0-9]+'
def get_db_events():
try:
dbconfig = {
'database': '<db>',
'host': '<host>',
'user': '<user>',
'password': '<pass>',
}
conn = MySQLConnection(**dbconfig)
cursor = conn.cursor()
cursor.execute("SELECT id, datum, zeit FROM veranstaltung")
rows = cursor.fetchall()
# Return a dict with YYYYMMDDHHHH as key and the id as the value for all events.
return {
str(row[1]) + str(row[2]).zfill(4): row[0]
for row in rows
}
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
def get_fs_event_dirs():
# Example: /mnt/stubnitz/cl/bilder/2016/10/02/2200/
event_dirs = {}
event_dir_cre = re.compile(EVENT_DIR_RE)
for dirpath, _, _ in os.walk(ARCHIVE_MOUNT, followlinks=True):
if event_dir_cre.search(dirpath):
disk, media_type, year, month, day, hour = dirpath[len(ARCHIVE_MOUNT):].split('/')
event_dirs[dirpath] = year + month + day + hour.zfill(4)
# Return a dict with the path as key and YYYYMMDDHHHH as value.
return event_dirs
def main():
print("Querying FS for events..")
fs_event_dirs = get_fs_event_dirs()
print("{} event dirs found in the FS.".format(len(fs_event_dirs)))
print("Querying DB for events..")
db_events = get_db_events()
print("{} events found in the DB.".format(len(db_events)))
for path, date in fs_event_dirs.items():
if date not in db_events.keys():
print("Error: No db event matches dir: {}".format(path))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment