Last active
January 2, 2019 21:41
-
-
Save CGenie/f5d8284cb6ed70fc9131acc4ad3975e6 to your computer and use it in GitHub Desktop.
libretime -- script to post files from a given directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO: Could use GET request to
/rest/media
to query for current tracks, then probablyposted.txt
wouldn't be needded (provided you have a way to match the filenames only).