Compare commits

...

2 Commits

Author SHA1 Message Date
Mike Lynch cd37766dc2 Fetch most recent N files for each feed if they aren'y already downloaded 2023-12-09 12:48:54 +11:00
Mike Lynch 606a278d28 Downgraded python to 3.7 so that it works on the Pi 2023-12-09 12:47:51 +11:00
3 changed files with 19 additions and 16 deletions

View File

@ -4,6 +4,7 @@ import argparse
import json import json
import pycurl import pycurl
import subprocess import subprocess
import sys
from pathlib import Path from pathlib import Path
import traceback import traceback
@ -13,7 +14,7 @@ def download_audio(link, destdir):
ptarget = Path(destdir) / Path(parts[-1]) ptarget = Path(destdir) / Path(parts[-1])
target = ptarget.resolve() target = ptarget.resolve()
if target.exists(): if target.exists():
print("File already exists, skipping") print(f"File {target} already downloaded, skipping")
else: else:
with open(target, "wb") as fp: with open(target, "wb") as fp:
curl = pycurl.Curl() curl = pycurl.Curl()
@ -21,7 +22,7 @@ def download_audio(link, destdir):
curl.setopt(pycurl.FOLLOWLOCATION, 1) curl.setopt(pycurl.FOLLOWLOCATION, 1)
curl.setopt(pycurl.MAXREDIRS, 5) curl.setopt(pycurl.MAXREDIRS, 5)
curl.setopt(pycurl.CONNECTTIMEOUT, 30) curl.setopt(pycurl.CONNECTTIMEOUT, 30)
curl.setopt(pycurl.TIMEOUT, 300) curl.setopt(pycurl.TIMEOUT, 3000)
curl.setopt(pycurl.NOSIGNAL, 1) curl.setopt(pycurl.NOSIGNAL, 1)
curl.setopt(pycurl.WRITEDATA, fp) curl.setopt(pycurl.WRITEDATA, fp)
try: try:
@ -39,14 +40,14 @@ def looks_audio(link):
return False return False
def get_latest(url, dir): def get_latest(url, dir, max):
d = feedparser.parse(url) d = feedparser.parse(url)
title = d.feed.get('title', "[no title]") title = d.feed.get('title', "[no title]")
entries = d.get('entries', []) entries = d.get('entries', [])
if entries: urls = []
latest = entries[0] for entry in entries[:max]:
if 'links' in latest: if 'links' in entry:
sounds = [ l for l in latest['links'] if looks_audio(l) ] sounds = [ l for l in entry['links'] if looks_audio(l) ]
if len(sounds) < 0: if len(sounds) < 0:
print("No audio links") print("No audio links")
if len(sounds) > 1: if len(sounds) > 1:
@ -54,8 +55,8 @@ def get_latest(url, dir):
for s in sounds: for s in sounds:
audio_url = s.get('href', '') audio_url = s.get('href', '')
if audio_url: if audio_url:
return audio_url urls.append(audio_url)
return None return urls
def main(): def main():
ap = argparse.ArgumentParser("autoradio - download audio from RSS feeds") ap = argparse.ArgumentParser("autoradio - download audio from RSS feeds")
@ -68,10 +69,11 @@ def main():
args = ap.parse_args() args = ap.parse_args()
with open(args.config, 'r') as cfh: with open(args.config, 'r') as cfh:
cf = json.load(cfh) cf = json.load(cfh)
m = int(cf.get("max", "5"))
for name, config in cf['feeds'].items(): for name, config in cf['feeds'].items():
print(f"Checking {name}") print(f"Checking {name}")
url = get_latest(config['url'], config['dir']) urls = get_latest(config['url'], config['dir'], m)
if url: for url in urls:
print(f"content = {url}") print(f"content = {url}")
download_audio(url, config['dir']) download_audio(url, config['dir'])

View File

@ -2,11 +2,12 @@
"feeds": { "feeds": {
"Utility Fog": { "Utility Fog": {
"url": "https://www.frogworth.com/utilityfog/feed/", "url": "https://www.frogworth.com/utilityfog/feed/",
"dir": "./output/UFog" "dir": "/media/pi/Storage/Music/Utility Fog"
}, },
"RA Podcast": { "RA Podcast": {
"url": "https://ra.co/xml/podcast.xml", "url": "https://ra.co/xml/podcast.xml",
"dir": "./output/RA" "dir": "/media/pi/Storage/Music/RA_Podcast"
} }
} },
} "max": 10
}

View File

@ -6,7 +6,7 @@ authors = ["Mike Lynch <m.lynch@sydney.edu.au>"]
readme = "README.md" readme = "README.md"
[tool.poetry.dependencies] [tool.poetry.dependencies]
python = "^3.9" python = "^3.7"
feedparser = "^6.0.10" feedparser = "^6.0.10"
[tool.poetry.scripts] [tool.poetry.scripts]