Esempio n. 1
0
def get_tracks_by_channel(channel):
    """Gets all tracks associated with the channel

    Accepts limit, and offset, orders by date (desc)

    :param channel:
    :return:
    """
    limit = request.args.get('limit', 20)
    offset = request.args.get('offset', 0)
    channel_obj = db.query(RSSChannel).filter(
        RSSChannel.channel_name == channel).first()

    if not channel_obj:
        return {
            'status': 'error',
            'message': f'RSS channel {channel} not found'
        }, 404

    # TODO: See if it's more efficient to query on channel_obj.tracks
    tracks = db.query(RSSTrack)\
        .filter(RSSTrack.channel == channel_obj) \
        .order_by(RSSTrack.published_date.desc())\
        .limit(limit)\
        .offset(offset)\
        .all()

    data = [c.to_json() for c in tracks]
    return {'data': data, 'status': 'ok'}
Esempio n. 2
0
def get_downloads(filter=None, sort=None):
    query = db.query(BaseAudio).order_by(BaseAudio.pretty_name)
    if filter:
        pass
    if sort:
        pass
    return query.all()
Esempio n. 3
0
def get_channel_by_name(channel_name):
    channel_name = unquote(channel_name)
    channel = db.query(RSSChannel).filter(
        RSSChannel.channel_name == channel_name).first()
    if not channel:
        return {
            'status': 'error',
            'message': f'Channel {channel_name} not found'
        }, 404
    data = channel.to_json()
    return {'status': 'ok', 'data': data}, 200
Esempio n. 4
0
    def search_translations(text, target_language='en-GB', source_language='sv-SE'):
        """Search for existing translations in the database
        This may

        :param text:
        :param target_language:
        :param source_language:
        :return:
        """
        ret = db.query(Translation).filter(Translation.original == text).filter(Translation.language== target_language).first()

        if ret:
            logger.debug(f'search_translation found:{ret.translation}')
        else:
            logger.debug('search_translation did not find anything in the database')

        return ret
Esempio n. 5
0
def save_file():
    """Downloads the file from the URL specified in POST/PUT body and saves on the filesystem and creates
    a record in the database

    Required fields:
    source_url: The URL to download it from (must be open, no auth will be attempted)
    Optional fields:
    language: BCP 47 language code. See https://cloud.google.com/speech-to-text/docs/languages for supported languages
    Files with more than one language should use string "MULTI"
    pretty_name: The name that the file will be displayed as

    :return: A JSON representation of the file or error message as appropriate
    """
    try:
        data = request.get_json()
        source_url = data.get('source_url')
        pretty_name = data.get('pretty_name')
        language = data.get('language')

        # RSS page field
        track_id = data.get('track_id')

        audio = BaseAudio.get(source_url=source_url)
        if BaseAudio.exists(audio):
            logger.info('Audio file already exists')
            return audio.to_json(), 409

        audio = BaseAudio(source_url=source_url,
                          pretty_name=pretty_name,
                          language=language)
        db.add(audio)
        db.flush()
        audio.save_file()
        db.commit()

        if track_id:
            track = db.query(RSSTrack).filter(RSSTrack.id == track_id).first()
            if track:
                track.is_added = True
                db.commit()

    except Exception as e:
        data = {'status': 'error', 'message': str(e)}
        return data, 500
    return audio.to_json(), 201
Esempio n. 6
0
 def get_all(**kwargs):
     return db.query(BaseAudio).order_by(BaseAudio.pretty_name).all()
Esempio n. 7
0
def get_base_info(id):
    """:rtype: BaseAudio"""
    return db.query(BaseAudio).filter(BaseAudio.id == id).first()
Esempio n. 8
0
 def get(domain):
     return db.query(ParsingRules).filter(ParsingRules.domain == domain).first()
Esempio n. 9
0
 def get(id=None, source_url=None):
     if id:
         return db.query(BaseAudio).filter(BaseAudio.id == id).first()
     elif source_url:
         return db.query(BaseAudio).filter(BaseAudio.source_url == source_url).first()
     return None
Esempio n. 10
0
 def get(id):
     return db.query(RSSTrack).filter(RSSTrack.id == id).first()
Esempio n. 11
0
 def get(id):
     return db.query(Segment).filter(Segment.id == id).first()
Esempio n. 12
0
 def get_segment(self, position):
     return db.query(Segment).filter(Segment.base_id == self.id, Segment.position == position).first()
Esempio n. 13
0
def get_article(article_id):
    article = db.query(Article).filter(Article.id == article_id).first()
    if not article:
        return {'status': 'error', 'details': 'Not Found'}, 404
    return {'status': 'ok', 'article': article.to_json()}
Esempio n. 14
0
def parse_feed():
    """Checks the content of an RSS URL and adds the episodes to the website

    :return:
    """
    data = request.get_json()
    feed_url = data.get('url')
    req = requests.get(feed_url)

    soup = BeautifulSoup(req.content, 'xml')

    channel = db.query(RSSChannel).filter(RSSChannel.url == feed_url).first()
    if not channel:
        name = soup.find('title').text
        description = soup.find('description').text
        channel = RSSChannel(url=feed_url,
                             channel_name=name,
                             channel_description=description)
        db.add(channel)
        db.commit()

    latest_track = db.query(RSSTrack)\
        .filter(RSSTrack.channel == channel)\
        .order_by(RSSTrack.published_date.desc())\
        .first()

    tracks = soup.find_all('item')
    ret = []
    for item in tracks:
        try:
            date_text = item.find('pubDate').text
        except AttributeError:
            date_text = item.find('pubdate').text
        rfc_date = datetime.datetime.fromtimestamp(
            email.utils.mktime_tz(email.utils.parsedate_tz(date_text)),
            pytz.utc)
        if latest_track:
            latest_track.published_date = latest_track.published_date.replace(
                tzinfo=rfc_date.tzinfo)
            if latest_track.published_date >= rfc_date:
                logger.debug(
                    f'Reached {latest_track.name}, which is already in the database'
                )
                break
        if channel.channel_type == 'audio':
            track_url = item.find('enclosure').get('url')
        elif channel.channel_type == 'text':
            track_url = item.find('link').text
        else:
            raise ValueError('Unrecognised channel type')
        name = item.find('title').text
        description = item.find('description').text
        pub_date = datetime.datetime.fromtimestamp(
            email.utils.mktime_tz(email.utils.parsedate_tz(date_text)),
            pytz.utc)
        track = RSSTrack(channel_id=channel.id,
                         url=track_url,
                         name=name,
                         description=description,
                         published_date=pub_date)
        ret.append(track)
        db.add(track)

    db.commit()
    return {'status': 'OK', 'data': [track.to_json() for track in ret]}, 200
Esempio n. 15
0
def get_channels():
    limit = request.args.get('limit', 20)
    offset = request.args.get('offset', 0)
    channels = db.query(RSSChannel).limit(limit).offset(offset).all()
    data = [c.to_json() for c in channels]
    return {'data': data, 'status': 'ok'}