Skip to content

Instantly share code, notes, and snippets.

@CGenie
Last active January 2, 2019 21:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CGenie/f5d8284cb6ed70fc9131acc4ad3975e6 to your computer and use it in GitHub Desktop.
Save CGenie/f5d8284cb6ed70fc9131acc4ad3975e6 to your computer and use it in GitHub Desktop.
libretime -- script to post files from a given directory
#!/usr/bin/env python
# Invocation: ./post-files.py <some-directory-with-mp3-files>
import os
import requests
from requests.auth import HTTPBasicAuth
import sys
URL = ':https://<hostname>:<port>/media/rest'
API_KEY = '<api-key>'
auth = HTTPBasicAuth(API_KEY, '')
posted = []
if os.path.exists('posted.txt'):
with open('posted.txt', 'rb') as f:
for line in f:
posted.append(line.strip())
with open('posted.txt', 'ab') as f:
for root, dirs, files in os.walk(sys.argv[1]):
for fname in files:
fpath = os.path.join(root, fname)
if fpath in posted:
print '{} posted already, skipping'.format(fpath)
continue
print '{} -- posting'.format(fpath)
r = requests.post(
URL,
auth=auth,
files={'file': open(fpath, 'rb')}
)
f.write('{}\n'.format(fpath))
@CGenie
Copy link
Author

CGenie commented Jan 2, 2019

TODO: Could use GET request to /rest/media to query for current tracks, then probably posted.txt wouldn't be needded (provided you have a way to match the filenames only).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment