Compare commits

...

7 Commits

Author SHA1 Message Date
ugrnm 1c5b0fab9a typo 1 year ago
Claude Heiland-Allen 81b34db0e6 more todo 1 year ago
ugrnm bbf24441a6 more things to be done 2 years ago
Claude Heiland-Allen 60619aad6c per-year download directory 1 year ago
Claude Heiland-Allen b908fe7fc6 stop ffmpeg going beserk with rogue text input 1 year ago
Claude Heiland-Allen c481871bde simplify directory creation 1 year ago
Claude Heiland-Allen 1dfd002e53 transcode to opus 1 year ago
  1. 5
      README
  2. 4
      TODO
  3. 50
      download_loooooops.py

5
README

@ -15,6 +15,7 @@ There are two scripts:
* download_loooooops.py
* gets latest toots tagged with #looptoper
* download all the attachment if new
* transcodes to opus
* generates new playlist and tells ezstream to reload it
@ -22,7 +23,7 @@ USAGE
-----
Quick and dirty, two tmux panes:
* `while true; do ./download_loooooops.py && sleep 1d; done`
* `while true; do ./download_loooooops.py </dev/null && sleep 1d; done`
* `stream_loooooops.sh`
You can start whichever you want firt.
@ -31,7 +32,7 @@ You can start whichever you want firt.
AGPL + DONATIONWARE
-------------------
devi is dual licensed AGPL and donationware.
radio-looptober is dual licensed AGPL and donationware.
If you find radio-looptober useful and can afford it, a donation to LURK is
greatly appreciated :)

4
TODO

@ -2,9 +2,7 @@ TODO
====
* cache the json parsing/process locally for faster restart tests
* only download MP3 files
* extract relevant json stuff to fill in or update ID3 tags
* 80c :)
* convert the downloaded mp3 files to opus
* get rid of request, just use urlib or something
* build the loop inside the download script
* prune media if original toot was deleted

50
download_loooooops.py

@ -9,10 +9,31 @@ from urllib.parse import urlparse
# remote_url
# description
output_dir = "loooooops"
year = "2022"
output_dir = os.path.join("loooooops", year)
bitrate = "128k"
def transcode_media(path, media_item, metadata):
infile = os.path.join(path, media_item)
outfile = os.path.join(path, media_item + ".opus")
if not os.path.exists(outfile):
print("transcodeing to {}".format(outfile))
pid = os.fork()
if pid == 0:
artist = metadata["creator"]
title = metadata["url"]
comment = metadata["description"]
date = metadata["date"]
os.execlp("ffmpeg", "ffmpeg", "-hide_banner", "-loglevel", "error", "-i", infile, "-map_metadata", "-1", "-metadata", "artist={}".format(artist), "-metadata", "title={}".format(title), "-metadata", "creation_time={}".format(date), "-map_chapters", "-1", "-ac", "2", "-af", "loudnorm=dual_mono=true", "-b:a", bitrate, "-y", outfile)
# never reached
else:
os.wait()
def grab_media(path, url):
try:
media_item = urlparse(url).path.split('/')[-1]
headers = {
@ -20,13 +41,19 @@ def grab_media(path, url):
'From': 'post.lurk.org/@lurk' # This is another valid field
}
if not os.path.exists(os.path.join(path, media_item)):
if os.path.exists(os.path.join(path, media_item)):
return media_item
else:
response = requests.get(url, headers=headers, stream=True)
if response.ok:
with open(os.path.join(path, media_item), 'wb') as media_file:
shutil.copyfileobj(response.raw, media_file)
print('Downloaded media {} from {}'.format(media_item, urlparse(url).netloc))
return media_item
except requests.exceptions.ConnectionError as e:
# maybe transient network issues
print(e)
sleep(60)
#This pages through all the looptober tag and collects the json in 'data'
there_is_more = True
@ -63,7 +90,7 @@ for collection in data:
i['created_at'][:-1]).astimezone(
datetime.timezone.utc)
if creation_date.strftime('%Y') == "2022": #we only take entries from this year
if creation_date.strftime('%Y') == year: #we only take entries from this year
stuff = {}
stuff["url"] = i["url"]
stuff["description"] = i["content"]
@ -76,13 +103,9 @@ for collection in data:
i["account"]["username"],
len(i["media_attachments"])))
if not os.path.exists(output_dir):
os.mkdir(output_dir)
for l in looooops:
path = os.path.join(output_dir,"{}_{}".format(l['creator'], l['id']))
if not os.path.exists(path):
os.mkdir(path)
os.makedirs(path, exist_ok=True)
print("\n")
print("Downloading looops by ***{}***".format(l['creator']))
@ -92,10 +115,13 @@ for l in looooops:
else:
url = a['url']
grab_media(path, url)
media_item = grab_media(path, url)
if media_item:
transcode_media(path, media_item, l)
# Once we've done everythin we generate a playlist and ask ezstream
# to reload it
os.system('find . -iname "*mp3" > playlist_loooooops.m3u'\
'&& kill -s HUP `pidof ezstream`')
# this is not an injection vulnerability as output_dir is under
# our control
os.system('find {} -iname "*opus" > playlist_loooooops.m3u'\
'&& kill -s HUP `pidof ezstream`'.format(output_dir))

Loading…
Cancel
Save