Esempio n. 1
0
def cinemas_where_film(film,
                       number_to_display=None,
                       to_show=settings.CINEMAS_TO_DISPLAY,
                       chat_id=None):

    film_cinemas, city_id = detect_film_cinemas(chat_id=chat_id,
                                                movie_id=film.kinohod_id,
                                                date=None)

    cinemas = []
    for i, p in enumerate(film_cinemas):

        if number_to_display and to_show and i < (number_to_display - to_show):
            continue

        cinema = p['cinema']
        if cinema.get('city') != city_id:
            continue

        cinemas.append(
            settings.RowCinema(
                cinema.get('shortTitle'), cinema.get('address'),
                cinema.get('mall'), '/c{}m{}'.format(cinema.get('id'),
                                                     film.kinohod_id)))

        if number_to_display and to_show and len(cinemas) == to_show:
            return cinemas

    return cinemas
Esempio n. 2
0
def callback_seance_text(tuid, bot, chat_id, text, cmd, profile):

    # in profile.cmd should be movie id
    # need concatenate movie with place

    if 'num' not in profile.cmd:
        return

    i_n = profile.cmd.index('num')
    movie_id = profile.cmd[len('/location'):i_n]

    film = Film.get_by_id(movie_id)

    cmd = cmd.encode('utf-8')

    # poor place of the code - every time new request to kinohod db
    film_cinemas, city_id = detect_film_cinemas(chat_id, movie_id, date=None)

    if not film_cinemas or len(film_cinemas) < 1:
        bot.sendMessage(chat_id, settings.NO_FILM_SEANCE)
        return

    parser = Parser(request=cmd, state='cinema', city_id=city_id)
    parser.parse()

    bot.sendChatAction(chat_id, action='typing')

    seances = []

    if not parser.data.place:
        pass
        # bot.sendMessage(chat_id, settings.CINEMA_NOT_FOUND)
    else:
        for p in parser.data.place:
            if not p or p.key not in film_cinemas:
                continue

            seances.append(
                settings.RowCinema(p.shortTitle, p.address, p.mall,
                                   '/c{}m{}'.format(p.kinohod_id, movie_id)))

    if len(seances) < 1:
        bot.sendMessage(chat_id, settings.NO_FILM_SEANCE)
        send_reply(bot, chat_id, get_cinemas_where_film, film,
                   settings.CINEMAS_TO_DISPLAY, chat_id)
        return

    template = settings.JINJA_ENVIRONMENT.get_template('cinema_where_film.md')
    msg = template.render({'film_name': film.title, 'seances': seances})

    mark_up = InlineKeyboardMarkup(inline_keyboard=[[
        dict(text=settings.MORE,
             callback_data='/where_film{}num{}'.format(
                 film.kinohod_id, settings.CINEMAS_TO_DISPLAY))
    ]])

    bot.sendMessage(chat_id, msg, parse_mode='Markdown', reply_markup=mark_up)
Esempio n. 3
0
def display_info(bot,
                 payload,
                 cmd,
                 chat_id,
                 full=False,
                 prefix_length=len('/info')):

    t_uid = payload['message']['from']['id']
    movie_id = cmd[prefix_length:len(cmd)]

    if not str(movie_id).isdigit():
        bot.sendMessage(chat_id, settings.INFO_NOT_FULL.format(movie_id))
        return

    film = Film.get_by_id(str(movie_id))
    now = datetime.now()
    two_weeks = timedelta(days=14)

    if not film:
        return bot.sendMessage(chat_id, settings.DONT_UNDERSTAND)

    # poor place of whole code
    film_cinemas, city_id = detect_film_cinemas(chat_id, movie_id, date=None)

    if (len(film_cinemas) < 1
            and not ((film.premiereDateRussia and
                      (now < film.premiereDateRussia <
                       (now + two_weeks))) or (film.premiereDateWorld and
                                               (now < film.premiereDateWorld <
                                                (now + two_weeks))))):
        return

    message, mark_up, movie_poster = display_movie_info(movie_id,
                                                        t_uid,
                                                        next_url='/location',
                                                        full=full)

    if not message:
        bot.sendMessage(chat_id, settings.SERVER_NOT_VALID)
        return

    if movie_poster:
        bot.sendChatAction(chat_id, 'upload_photo')
        bot.sendPhoto(chat_id, ('poster.jpg', movie_poster))

    bot.sendMessage(chat_id,
                    message,
                    reply_markup=mark_up,
                    parse_mode='Markdown')
Esempio n. 4
0
def get_seances(chat_id, movie_id, number_of_seances,
                date=datetime.now().strftime('%d%m%Y')):

    u = get_model(UserProfile, chat_id)

    f = Film.get_by_id(str(movie_id))

    f_cinemas, city_id = detect_film_cinemas(chat_id, movie_id, date=date)

    if f and not f_cinemas:
        return settings.NO_FILM_SEANCE

    cinema_ids = [k['cinema']['id'] for k in f_cinemas]

    if u:
        l = json.loads(u.location)
        latitude, longitude = l['latitude'], l['longitude']

        url = '{}?lat={}&long={}&sort=distance&cityId=1'.format(
            settings.URL_WIDGET_CINEMAS,
            latitude,
            longitude,
        )

        seances = []

        html_data = get_data(url)

        if not html_data:
            return settings.DONT_UNDERSTAND, None

        if 'data' not in html_data:
            return settings.CANNOT_FIND_SEANCE, None

        for info in html_data['data']:

            cinema_id = info.get('id')

            if str(cinema_id) not in cinema_ids:
                continue

            seances.append(
                settings.RowDist(
                    settings.uncd(info['shortTitle']),
                    info['distance'],
                    '/c{}m{}'.format(cinema_id, movie_id)
                )
            )

        if len(seances) < 1:
            return settings.NO_FILM_SEANCE, None

        correct, n = False, settings.SEANCES_TO_DISPLAY
        while not correct:
            if len(seances) > number_of_seances:
                n = settings.SEANCES_TO_DISPLAY
                seances = seances[number_of_seances - n: number_of_seances]
                correct = True
            elif len(seances) > (number_of_seances - n):
                seances = seances[number_of_seances - n:]
                correct = True
            else:
                number_of_seances = n

        empty_data(html_data=html_data)
        mark_up = gen_markup(movie_id, number_of_seances)
        template = settings.JINJA_ENVIRONMENT.get_template(
            'seances_distance.md'
        )

    else:

        url = settings.URL_SEANCES.format(
            str(movie_id), settings.KINOHOD_API_KEY,
            (number_of_seances - settings.SEANCES_TO_DISPLAY),
            number_of_seances
        )

        seances = []
        html_data = get_data(url)

        if not html_data:
            return settings.DONT_UNDERSTAND, None

        for info in html_data:

            cinema_json = info.get('cinema')
            cinema_id = cinema_json.get('id')
            if str(cinema_id) not in cinema_ids:
                continue

            seances.append(
                settings.RowCinema(
                    settings.uncd(cinema_json['shortTitle']),
                    cinema_json.get('address'),
                    cinema_json.get('mall'),
                    '/c{}m{}'.format(cinema_id, info['movie']['id']))
            )

        empty_data(html_data=html_data)
        mark_up = gen_markup(movie_id, number_of_seances)
        template = settings.JINJA_ENVIRONMENT.get_template('seances.md')

    return template.render({
        'date': datetime.strptime(date, '%d%m%Y').strftime('%d.%m.%Y'),
        'sign_point': settings.SIGN_POINT,
        'sign_tip': settings.SIGN_TIP,
        'seances': seances
    }), mark_up
Esempio n. 5
0
def display_seances_part(chat_id, text, movie_id, number_of_seances,
                         date=datetime.now().strftime('%d%m%Y')):

    url = settings.URL_FULL_SEANCES.format(
        str(movie_id), settings.KINOHOD_API_KEY, date
    )

    seances = []

    f = Film.get_by_id(str(movie_id))

    f_cinemas, city_id = detect_film_cinemas(chat_id, movie_id, date=date)

    if f and not f_cinemas:
        return settings.NO_FILM_SEANCE

    cinema_ids = [k['cinema']['id'] for k in f_cinemas]

    html_data = get_data(url)

    if not html_data:
        return settings.DONT_UNDERSTAND

    for info in html_data:

        cinema_json = info.get('cinema')
        cinema_id = cinema_json.get('id')

        if cinema_id not in cinema_ids:
            continue

        if (('shortTitle' in cinema_json and
                cinema_json['shortTitle'].find(text) > -1) or
            ('address' in info['cinema'] and
                cinema_json['address'].find(text) > -1) or
            ('subway_stations' in cinema_json and
                'name' in cinema_json['subway_stations'] and
                cinema_json['subway_stations']['name'].find(text) > -1)):

            seances.append(
                settings.RowCinema(
                    cinema_json['shortTitle'],
                    cinema_json.get('address'),
                    cinema_json.get('mall'),
                    '/c{}m{}'.format(cinema_json['id'],
                                       info['movie']['id']))
            )

    empty_data(html_data=html_data)
    if len(seances) < 1:
        template = settings.JINJA_ENVIRONMENT.get_template('no_seances.md')
        return template.render({})

    correct, n = False, settings.SEANCES_TO_DISPLAY
    while not correct:
        if len(seances) > number_of_seances:
            n = settings.SEANCES_TO_DISPLAY
            seances = seances[number_of_seances - n: number_of_seances]
            correct = True
        elif len(seances) > (number_of_seances - n):
            seances = seances[number_of_seances - n:]
            correct = True
        else:
            number_of_seances = n

    template = settings.JINJA_ENVIRONMENT.get_template('seances.md')

    return template.render({
        'date': datetime.strptime(date, '%d%m%Y').strftime('%d.%m.%Y'),
        'sign_point': settings.SIGN_POINT,
        'sign_tip': settings.SIGN_TIP,
        'seances': seances
    })