Example #1
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        log.debug(
            f'--------------------------- Starting {self.BOT_NAME} v{self.VERSION} --------------------------'
        )

        self.expander = TeamExpander()
        self.tower_data = TowerOfDoomData(self.my_emojis)
        self.prefix = models.Prefix(CONFIG.get('default_prefix'))
        self.language = models.Language(CONFIG.get('default_language'))
        self.subscriptions = models.Subscriptions()
        self.views = Views(emojis={})
Example #2
0
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        log.debug(
            f'--------------------------- Starting {self.BOT_NAME} v{self.VERSION} --------------------------'
        )

        self.expander = TeamExpander()
        self.tower_data = TowerOfDoomData(self.my_emojis)
        self.prefix = models.Prefix(CONFIG.get('default_prefix'))
        self.language = models.Language(CONFIG.get('default_language'))
        self.subscriptions = models.Subscriptions()
        self.views = Views(emojis={})
        self.pet_rescues = []
        self.pet_rescue_config: PetRescueConfig = None
        token = CONFIG.get('dbl_token')
        self.dbl_client = None
        self.server_status_cache = {'last_updated': datetime.datetime.min}
        if token:
            self.dbl_client = dbl.DBLClient(self, token)
Example #3
0
def index():
    if request.method == 'POST':

        token = request.get_json()['token']

        if not token:
            return {'status': '401 unauthorized'}

        # new
        # TODO: Need to grab user object from owner_id
        owner_id = request.get_json()['owner_id']
        user = models.User.query.get(owner_id)

        if not user.verify_auth_token(token):
            return {'status': '401 invalid token'}
        # end new

        language = request.get_json()

        # sets local variables from request
        name = language['name']
        framework = language['framework']
        owner_id = language['owner_id']

        # creates a new model object, stores it in db, and persists it
        new_language = models.Language(name=name,
                                       framework=framework,
                                       owner_id=owner_id)
        db.session.add(new_language)
        db.session.commit()

        return jsonify({'status': '201'})

    elif request.method == 'GET':
        languages = models.Language.query.all()
        # returns custom formatted objects from the query
        return jsonify(
            {'languages': [language.to_json() for language in languages]})

    # if method is not GET/POST
    else:
        return jsonify({'status': 'method not supported'})
Example #4
0
def add_language(request):
    """
    This view adds a language
    :param request: a request object
    :return: None
    """
    if request.authenticated_userid is None:
        raise exc.HTTPNotFound()

    name = request.matchdict["language"]
    public_group_id = request.matchdict["public_group_id"]

    language = models.Language.get_by_name(name)

    if not language:
        language = models.Language(name=name)
        request.db.add(language)
        # We need to flush the db session here so that language.id will be generated.
        request.db.flush()
    url = request.route_url('translation_read', public_language_id=language.pubid, public_group_id=public_group_id)
    return exc.HTTPSeeOther(url)
def add_language():
    session = models.DBSession()
    language = models.Language(name='en')
    session.add(language)
    session.flush()
Example #6
0
def create_requests_and_save_datas(douban_id):
    session = sqla['session']
    cookies['bid'] = random_str(11)
    r = requests.get(douban_movie_url + str(douban_id),
                     cookies=cookies,
                     timeout=10)

    if r.status_code != 200:
        return

    data = parsers.movie.start_parser(r.text)
    data['douban_url'] = r.url

    directors = data.pop('directors', [])
    director_douban_ids = set(director['douban_id'] for director in directors)
    playwrights = data.pop('playwrights', [])
    playwright_douban_ids = set(playwright['douban_id']
                                for playwright in playwrights)
    actors = data.pop('actors', [])
    actor_douban_ids = set(actor['douban_id'] for actor in actors)
    celebrities = directors + playwrights + actors
    celebrity_douban_ids = \
        director_douban_ids | playwright_douban_ids | actor_douban_ids

    douban_id_celebrity_obj_dict = {}

    for celebrity in celebrities:
        celebrity_douban_id = celebrity['douban_id']
        if celebrity_douban_id is not None:
            try:
                celebrity_obj = models.Celebrity(**celebrity)
                session.add(celebrity_obj)
                session.commit()
            except (IntegrityError, InvalidRequestError):
                session.rollback()
                celebrity_obj = session.query(models.Celebrity).filter_by(
                    douban_id=celebrity_douban_id).first()
            douban_id_celebrity_obj_dict[celebrity_douban_id] = celebrity_obj

    video = session.query(models.Video).filter_by(douban_id=douban_id).one()
    video.directors.clear()
    video.playwrights.clear()
    video.actors.clear()

    for (celebrity_douban_id,
         celeBrity_obj) in douban_id_celebrity_obj_dict.items():
        if celebrity_douban_id in director_douban_ids:
            video.directors.append(celebrity_obj)
        if celebrity_douban_id in playwright_douban_ids:
            video.playwrights.append(celebrity_obj)
        if celebrity_douban_id in actor_douban_ids:
            video.actors.append(celebrity_obj)

    session.commit()
    """If use query.update(data), an error is raised,
    beacuse movie table is multiple table and we want to
    update movie table and subject table some columns.
    """

    video.genres.clear()
    video.countries.clear()
    video.languages.clear()
    session.commit()

    table_name = video.__tablename__
    if table_name == 'movies':
        genre_class = models.MovieGenre
    elif table_name == 'tvs':
        genre_class = models.TVGenre
    elif table_name == 'animations':
        genre_class = models.AnimationGenre
    for k, v in data.items():
        if k == 'genres':
            for genre in v:
                try:
                    genre_obj = genre_class(**genre)
                    session.add(genre_obj)
                    session.commit()
                except (IntegrityError, InvalidRequestError):
                    session.rollback()
                    genre_obj = session.query(genre_class).filter_by(
                        name=genre['name']).one()
                video.genres.append(genre_obj)
        elif k == 'countries':
            for country in v:
                try:
                    country_obj = models.Country(**country)
                    session.add(country_obj)
                    session.commit()
                except (IntegrityError, InvalidRequestError):
                    session.rollback()
                    country_obj = session.query(
                        models.Country).filter_by(name=country['name']).one()
                video.countries.append(country_obj)
        elif k == 'languages':
            for language in v:
                try:
                    language_obj = models.Language(**language)
                    session.add(language_obj)
                    session.commit()
                except (IntegrityError, InvalidRequestError):
                    session.rollback()
                    language_obj = session.query(models.Language).filter_by(
                        name=language['name']).one()
                video.languages.append(language_obj)
        session.commit()
    '''Why set other value not in above for cycle?
    Beacuse above "for cycle" have rollback.
    '''
    for k, v in data.items():
        if k != 'genres' and k != 'countries' and k != 'languages':
            if k == 'aliases' or k == 'thumbnail_photos':
                v = str(v)
            setattr(video, k, v)

    session.commit()

    # parser movie photo
    r = requests.get(douban_movie_url + str(douban_id) + '/all_photos',
                     cookies=cookies,
                     timeout=10)
    photo_data = parsers.movie_photo.start_parser(r.text)

    for k, v in photo_data.items():
        v = str(v)
        setattr(video, k, v)

    video.is_detail = True
    session.commit()

    print(','.join([table_name, douban_id, data.get('title')]))