Jace & BingeStacker
Hey Jace, I just set up a new smart watchlist system on a Raspberry Pi and it can auto schedule your binge with perfect episode order. Would love your tinkering skills to make it work.
That sounds wicked, just drop the code and logs here and I'll see where the glitch is hiding in the schedule algorithm.
Here’s a quick Python script that pulls the episode list from an API, sorts it by season and episode, and writes a schedule to a CSV. The logs are a simple text dump that shows each step the script takes. Feel free to tweak the sleep intervals or add your own snack buffer times.
import requests, csv, time, logging
logging.basicConfig(filename='binge_schedule.log', level=logging.INFO, format='%(asctime)s %(message)s')
API_URL = "https://api.tvmaze.com/shows/1234/episodes" # replace 1234 with your show ID
def fetch_episodes():
logging.info("Fetching episodes from API")
resp = requests.get(API_URL)
resp.raise_for_status()
episodes = resp.json()
logging.info(f"Fetched {len(episodes)} episodes")
return episodes
def sort_episodes(episodes):
logging.info("Sorting episodes by season and number")
return sorted(episodes, key=lambda e: (e['season'], e['number']))
def write_schedule(sorted_eps, filename='binge_schedule.csv'):
logging.info(f"Writing schedule to {filename}")
with open(filename, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Season', 'Episode', 'Name', 'Runtime'])
for ep in sorted_eps:
writer.writerow([ep['season'], ep['number'], ep['name'], ep['runtime']])
logging.info("Schedule written successfully")
if __name__ == "__main__":
logging.info("Starting binge schedule generator")
eps = fetch_episodes()
sorted_eps = sort_episodes(eps)
write_schedule(sorted_eps)
logging.info("Done")
Nice loop, but I’d add a retry logic if the API hiccups, and maybe use `asyncio` so the Pi can handle other tasks while it sleeps. Also, write a quick alert to Telegram when a new episode drops so you don’t miss the premiere. Let me know if you want the exact snippet.
import asyncio, aiohttp, json, logging, time
from pathlib import Path
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
API_URL = "https://api.tvmaze.com/shows/1234/episodes" # replace with actual show id
TELEGRAM_BOT_TOKEN = "YOUR_BOT_TOKEN"
TELEGRAM_CHAT_ID = "YOUR_CHAT_ID"
async def fetch_episodes(session, retries=3):
for attempt in range(1, retries+1):
try:
async with session.get(API_URL, timeout=10) as resp:
resp.raise_for_status()
data = await resp.json()
logging.info(f"Fetched {len(data)} episodes (attempt {attempt})")
return data
except Exception as e:
logging.warning(f"Attempt {attempt} failed: {e}")
if attempt == retries:
raise
await asyncio.sleep(2 ** attempt)
def sort_episodes(episodes):
return sorted(episodes, key=lambda e: (e["season"], e["number"]))
async def send_telegram(message):
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {"chat_id": TELEGRAM_CHAT_ID, "text": message}
async with aiohttp.ClientSession() as session:
async with session.post(url, json=payload) as resp:
await resp.text()
logging.info("Telegram alert sent")
async def main():
async with aiohttp.ClientSession() as session:
episodes = await fetch_episodes(session)
sorted_eps = sort_episodes(episodes)
Path("binge_schedule.csv").write_text("Season,Episode,Name,Runtime\n")
for ep in sorted_eps:
line = f"{ep['season']},{ep['number']},{ep['name']},{ep['runtime']}\n"
Path("binge_schedule.csv").write_text(line, append=True)
latest = sorted_eps[-1]
await send_telegram(f"New episode queued: S{latest['season']}E{latest['number']} – {latest['name']} ({latest['runtime']} min)")
logging.info("Schedule updated and alert sent")
if __name__ == "__main__":
asyncio.run(main())
Looks solid—just swap the append logic to use `write_text(..., mode='a')` so you don’t overwrite the header each time, and maybe cache the last episode ID so you only send a Telegram message when a brand‑new episode appears. Happy coding!
Nice tweak, that’s exactly how I keep the logs clean. I’ll add a tiny cache file to track the last ID and only trigger the Telegram alert when it changes. Thanks for the pointers!
Glad it helped—good luck with the cache, and hit me up if you hit any weird race conditions. Happy hacking!
Thanks, will do—let me know if the Pi starts spitting out extra snack alerts! Happy hacking.