~bronikowski/radiogopher

ref: ed1f1ae2ec157ee66aa5c1abca69431646ef977c radiogopher/collector.py -rw-r--r-- 2.1 KiB
ed1f1ae2Emil Oppeln-Bronikowski It was terrible idea to write this configuration module like that. 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import feedparser
import exceptions
import pprint
import hashlib
import requests
import os
import progressbar
import filetype
import config

widgets = [progressbar.Percentage(), progressbar.Bar()]

configuration = config.Application('application')
subscribed_feeds = config.Feeds('feeds')

def parse_feed(feed_uri):

    parsed = feedparser.parse(feed_uri)

    if not parsed['entries']:
        raise exceptions.FeedUrlError(parsed['bozo_exception'])

    return parsed

def feeds():
    return subscribed_feeds['podcasts'].keys()

def extract_media(feed):
    if not feed.entries:
        raise exceptions.NoEntries()
    print("→ {}".format(feed.feed.title))
    for entry in feed.entries:
        if not entry.links:
            continue
        for link in entry.links:
            if link['rel'] == 'enclosure' and link['type'].startswith('audio/'):
                download_media(entry['id'], link['href'], entry['title'])
        break

def download_media(media_id, media_uri, title=None):

    hashed_media_id = hashlib.sha1()
    hashed_media_id.update(media_id.encode('utf-8'))

    with requests.get(media_uri, stream=True) as resp:
        bar = progressbar.ProgressBar(
            max_value=int(resp.headers['content-length']),
            widgets=widgets
        )

        title = title or hashed_media_id.hexdigest()
        print(title)

        path = configuration['download_directory']
        filename = os.path.join(path, title)

        with open(filename, 'wb') as f:
            chunks_downloaded = 0
            for chunk in resp.iter_content(chunk_size=configuration['chunks']):
                if chunk:
                    chunks_downloaded = chunks_downloaded + len(chunk)
                    f.write(chunk)
                    bar.update(chunks_downloaded)
        bar.finish()

        rename_file(title, path, filename)

        return True

def rename_file(title, path, filename):

    guess = filetype.guess(filename)
    if not guess:
        return False
    new_filename = os.path.join(path, "{}.{}".format(
        title,
        guess.extension
    ))
    os.rename(
        filename,
        new_filename
    )
    return True