Advertisement
Guest User

Untitled

a guest
Mar 24th, 2021
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import click
  3. import feedparser
  4. import requests
  5. import time
  6.  
  7.  
  8. def sanitize(f):
  9.     Badswede = ["å","Å", "ä","Ä", "ö","Ö"]
  10.     Goodswede = ["a","A","a","A","o","O"]
  11.     Dividers = [" ", "/", ","]
  12.     Specchar = ["-", "–", "-","#", "!", ":"]
  13.     for i, c in enumerate(f):
  14.        
  15.         if c in Badswede:
  16.             f = f.replace(Badswede[Badswede.index(c)], Goodswede[Badswede.index(c)])
  17.         if c in Dividers:
  18.             f = f.replace(c, "_")
  19.  
  20.         if c in Specchar:
  21.             f = f.replace(c, "")
  22.     return f
  23.  
  24. def filenamer(filename, date, filetype, f, d):
  25.     if f is True:
  26.         filename = sanitize(filename)
  27.     if d is True and f is False:
  28.         filename = date
  29.     elif d is True and f is True:
  30.         filename = date+"_" + filename
  31.     filename += filetype
  32.     return filename
  33.  
  34. @click.command()
  35. @click.argument('feed',
  36.         #help='RSS-feed att ladda ned'
  37.         )
  38. @click.option('--filetype',
  39.         default='.mp3',
  40.         help='Free text for filetype, ie \'.mp3\'')
  41. @click.option('--tag',
  42.         default='enclosure',
  43.         #help='Specificera url-taggen'
  44.         )
  45. @click.option('-f',is_flag=True, default=False,
  46.         help='Set to include filename in output file'
  47.         )
  48. @click.option('-d', is_flag=True, default=False,
  49.         help='Set to include date in output file'
  50.         )
  51. def __main__(feed, tag, filetype, f, d):
  52.     PodFeed = feedparser.parse(feed)
  53.     for post in PodFeed.entries:
  54.         print("Processing "+post.title)
  55.         date = time.strftime("%y%m%d", post.published_parsed)
  56.         filename = filenamer(post.title,date,filetype, f, d)
  57.         url=post.enclosures[0].href        
  58.         try:
  59.             with open(filename, 'r') :
  60.                exists=True
  61.                print(filename + " already exists")
  62.            
  63.         except IOError:
  64.                  exists=False
  65.        
  66.         if exists != True:
  67.             print("Downloading and saving as "+filename)
  68.             r = requests.get(url, allow_redirects=True)
  69.             open(filename, 'wb').write(r.content)
  70.        
  71.  
  72. if __name__ == "__main__":
  73.     __main__()
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement