Exemple #1
0
def _load_podcasts_from_database(term):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        podcasts = mongodb.podcast.find({
            "$or": [{
                "artistName": {
                    "$regex": term,
                    "$options": "i"
                }
            }, {
                "collectionName": {
                    "$regex": term,
                    "$options": "i"
                }
            }, {
                "feedUrl": {
                    "$regex": term,
                    "$options": "i"
                }
            }]
        })
        if podcasts.count() > 0:
            return (True, ujson.loads(dumps(podcasts)))
    else:
        # TODO(Better error handler)
        print("Error: No database found")
    return (False, [])
Exemple #2
0
def background_rss():
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcasts = mongodb.podcast.find()
        for podcast in podcasts:
            parser_podcast_rss.delay(podcast['collectionId'],
                                     podcast['feedUrl'])
Exemple #3
0
def record_search_term(term):
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        mongodb.term.replace_one({'term': term}, {'term': term}, upsert=True)
    else:
        # TODO(Better error handler)
        print("Error: No database found")
Exemple #4
0
def background_itunes():
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcasts = mongodb.podcast.find()
        for podcast in podcasts:
            success, response = _load_podcast_info_from_itunes(
                podcast['collectionId'])
            if success:
                parser_itunes_response.delay(response)
Exemple #5
0
def _load_podcast_episode_by_id(episode_id):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        try:
            return mongodb.podcast_episode.find_one({'id': episode_id},
                                                    {'_id': False})
        except Exception as e:
            # TODO(Better error handler)
            print("Error: No episode found (%s)" % str(e))
            return None
    return None
Exemple #6
0
def parser_itunes_response(itunes_response):
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcast_collection = mongodb.podcast
        for item in itunes_response:
            podcast_collection.replace_one(
                {'collectionId': item['collectionId']}, item, upsert=True)
            parser_podcast_rss.delay(item['collectionId'], item['feedUrl'])
    else:
        # TODO(Better error handler)
        print("Error: No database found")
Exemple #7
0
def _load_podcast_info_by_id(id):
    mongodb = Database.get_mongodb_database()
    if mongodb:
        podcast = mongodb.podcast.find_one({'collectionId': id},
                                           {'_id': False})
        episodes = mongodb.podcast_episode.find({
            'collectionId': id
        }, {
            '_id': False
        }).sort([('number', -1)])
        return {
            'info': ujson.loads(dumps(podcast)),
            'episodes': ujson.loads(dumps(episodes))
        }
    return None
Exemple #8
0
def parser_podcast_rss(podcast_id, url):
    # Select shows! By order...
    # podcast_episode.find(
    #    {'collectionId': 979020229}).sort( { number: -1 } )[0]
    mongodb = Database.get_mongodb_database(False)
    if mongodb:
        podcast_episode = mongodb.podcast_episode

        parsed = feedparser.parse(url)
        shows = parsed.get('entries', [])
        if len(shows) > 0:
            number = len(shows)
            for show in parsed['entries']:
                audio = None
                size = 0
                ext = 'mp3'
                for link in show.get('links'):
                    if 'audio' in link.get('type', ''):
                        audio_url = link.get('href', '')
                        size = link.get('length', 0)
                        if '.mp3' in audio_url:
                            ext = 'mp3'
                        elif '.ogg' in audio_url:
                            ext = 'ogg'
                        elif '.aac' in audio_url:
                            ext = 'aac'
                        elif '.wma' in audio_url:
                            ext = 'wma'
                        elif '.flac' in audio_url:
                            ext = 'flac'

                # In the future, we could get image from episode using
                # p = BeautifulSoup(show.get('content')[0].get('value'), 'lxml')
                # Hoping that the image bring back by this is the right one
                # p.find('img')

                guid = show.get('guid', str(randint(1000, 99999)) + '_')
                episode = {
                    'collectionId': podcast_id,
                    'id': str(sum([ord(i) for i in guid])),
                    'guid': guid,
                    'number': number,
                    'author': show.get('author', ''),
                    'title': show.get('title', ''),
                    'description': show.get('description', ''),
                    'published': show.get('published', ''),
                    'duration': show.get('itunes_duration', 0),
                    'audio_url': audio_url,
                    'audio_extension': ext,
                    'audio_size': size
                }

                podcast_episode.replace_one(
                    {
                        'collectionId': podcast_id,
                        'guid': episode['guid']
                    },
                    episode,
                    upsert=True)
                number -= 1
    else:
        # TODO(Better error handler)
        print("Error: No database found")