feat: Add script to download Talking Postgres podcast
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
		
					parent
					
						
							
								367dc76c83
							
						
					
				
			
			
				commit
				
					
						6a11a9fe26
					
				
			
		
					 1 changed files with 73 additions and 0 deletions
				
			
		
							
								
								
									
										73
									
								
								dl-talkingpostgres.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										73
									
								
								dl-talkingpostgres.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,73 @@
 | 
				
			||||||
 | 
					#!/usr/bin/env python
 | 
				
			||||||
 | 
					# Download Talking Postgres podcast
 | 
				
			||||||
 | 
					# https://talkingpostgres.com/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					import os
 | 
				
			||||||
 | 
					import feedparser
 | 
				
			||||||
 | 
					import requests
 | 
				
			||||||
 | 
					from time import mktime
 | 
				
			||||||
 | 
					from unidecode import unidecode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					from pprint import pprint
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def convert_title(title):
 | 
				
			||||||
 | 
					    """Replace special characters in a title"""
 | 
				
			||||||
 | 
					    m = {
 | 
				
			||||||
 | 
					        ")": "",
 | 
				
			||||||
 | 
					        "(": "",
 | 
				
			||||||
 | 
					        "_": "-",
 | 
				
			||||||
 | 
					        "&": "",
 | 
				
			||||||
 | 
					        " ": "-",
 | 
				
			||||||
 | 
					        "'": "",
 | 
				
			||||||
 | 
					        "/": "",
 | 
				
			||||||
 | 
					        ":": "",
 | 
				
			||||||
 | 
					        "--": "-",
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    for i in m:
 | 
				
			||||||
 | 
					        title = title.replace(i, m[i])
 | 
				
			||||||
 | 
					    title = unidecode(title)
 | 
				
			||||||
 | 
					    return title.lower()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def download(url, filename, timestamp):
 | 
				
			||||||
 | 
					    print(f"Downloading {url} to {filename}")
 | 
				
			||||||
 | 
					    with requests.get(url, stream=True) as r:
 | 
				
			||||||
 | 
					        r.raise_for_status()
 | 
				
			||||||
 | 
					        with open(filename, "wb") as f:
 | 
				
			||||||
 | 
					            for chunk in r.iter_content(chunk_size=8192):
 | 
				
			||||||
 | 
					                f.write(chunk)
 | 
				
			||||||
 | 
					    os.utime(filename, (timestamp, timestamp))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def main():
 | 
				
			||||||
 | 
					    feed = feedparser.parse("https://feeds.transistor.fm/talkingpostgres")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for entry in feed["entries"]:
 | 
				
			||||||
 | 
					        title = convert_title(entry["title"])
 | 
				
			||||||
 | 
					        timestamp = entry["published_parsed"]
 | 
				
			||||||
 | 
					        date = f"{timestamp.tm_year:04}-{timestamp.tm_mon:02}-{timestamp.tm_mday:02}"
 | 
				
			||||||
 | 
					        url = None
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        for link in entry["links"]:
 | 
				
			||||||
 | 
					            if link["type"] == "audio/mpeg":
 | 
				
			||||||
 | 
					                url = link["href"]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if not url:
 | 
				
			||||||
 | 
					            print("Could not find url")
 | 
				
			||||||
 | 
					            pprint(entry)
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        filename = f"{date}-{title}.mp3"
 | 
				
			||||||
 | 
					        if not os.path.isfile(filename):
 | 
				
			||||||
 | 
					            try:
 | 
				
			||||||
 | 
					                download(url, filename, mktime(timestamp))
 | 
				
			||||||
 | 
					            except KeyboardInterrupt:
 | 
				
			||||||
 | 
					                if os.path.isfile(filename):
 | 
				
			||||||
 | 
					                    os.unlink(filename)
 | 
				
			||||||
 | 
					                break
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if __name__ == "__main__":
 | 
				
			||||||
 | 
					    main()
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue