Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import click
- import feedparser
- import requests
- import time
- def sanitize(f):
- Badswede = ["å","Å", "ä","Ä", "ö","Ö"]
- Goodswede = ["a","A","a","A","o","O"]
- Dividers = [" ", "/", ","]
- Specchar = ["-", "–", "-","#", "!", ":"]
- for i, c in enumerate(f):
- if c in Badswede:
- f = f.replace(Badswede[Badswede.index(c)], Goodswede[Badswede.index(c)])
- if c in Dividers:
- f = f.replace(c, "_")
- if c in Specchar:
- f = f.replace(c, "")
- return f
- def filenamer(filename, date, filetype, f, d):
- if f is True:
- filename = sanitize(filename)
- if d is True and f is False:
- filename = date
- elif d is True and f is True:
- filename = date+"_" + filename
- filename += filetype
- return filename
- @click.command()
- @click.argument('feed',
- #help='RSS-feed att ladda ned'
- )
- @click.option('--filetype',
- default='.mp3',
- help='Free text for filetype, ie \'.mp3\'')
- @click.option('--tag',
- default='enclosure',
- #help='Specificera url-taggen'
- )
- @click.option('-f',is_flag=True, default=False,
- help='Set to include filename in output file'
- )
- @click.option('-d', is_flag=True, default=False,
- help='Set to include date in output file'
- )
- def __main__(feed, tag, filetype, f, d):
- PodFeed = feedparser.parse(feed)
- for post in PodFeed.entries:
- print("Processing "+post.title)
- date = time.strftime("%y%m%d", post.published_parsed)
- filename = filenamer(post.title,date,filetype, f, d)
- url=post.enclosures[0].href
- try:
- with open(filename, 'r') :
- exists=True
- print(filename + " already exists")
- except IOError:
- exists=False
- if exists != True:
- print("Downloading and saving as "+filename)
- r = requests.get(url, allow_redirects=True)
- open(filename, 'wb').write(r.content)
- if __name__ == "__main__":
- __main__()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement