Skip to content

Instantly share code, notes, and snippets.

@gar3thjon3s
Created March 1, 2014 21:19
Show Gist options
  • Save gar3thjon3s/9297530 to your computer and use it in GitHub Desktop.
Save gar3thjon3s/9297530 to your computer and use it in GitHub Desktop.
A script to scan through mp3s on my synology ds410 NAS and add any missing stuff to the index. Amazing it doesn't do this by default O_O
#!/usr/bin/env python
import sys
import os
import os.path
import time
import re
from datetime import date, timedelta, datetime
from subprocess import check_output
mp3_dir = "/volume1/music/mp3"
mp3_re = re.compile("\.mp3$", re.I)
missing_re = re.compile("Path:\s+$", re.M)
def index_if_missing(f):
res = check_output("synoindex -g \"{}\" -t music".format(f), shell=True)
if re.match(missing_re, res):
print "adding {} to index".format(f)
check_output("synoindex -a \"{}\"".format(f), shell=True)
def main(days_back):
print "searching {} days back".format(days_back)
start_date = (date.today()-timedelta(days=days_back))
start_time = datetime(start_date.year, start_date.month, start_date.day)
for root, dirs, files in os.walk(mp3_dir):
if "@eaDir" in dirs:
dirs.remove("@eaDir")
for f in files:
_, ext = os.path.splitext(f)
if re.search(mp3_re, ext):
modt = datetime.fromtimestamp(os.path.getctime(os.path.join(root, f)))
if modt > start_time:
index_if_missing(os.path.join(root, f))
if __name__ == "__main__":
usage = "usage: ./reindex_music.sh DAYS_BACK"
if len(sys.argv) != 2:
print usage
exit(1)
days_back = int(sys.argv[1])
if days_back is None or days_back < 0:
print usage
exit(1)
main(days_back)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment