Exemple #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
Exemple #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)
Exemple #3
0
def cinemas_from_data(data, movie_id=None):
    cinemas = []

    link = '/c{}m{}' if movie_id else '/show{}'

    for p in data:
        l = (link.format(p.kinohod_id, movie_id)
             if movie_id else link.format(p.kinohod_id))

        cinemas.append(settings.RowCinema(p.shortTitle, p.address, p.mall, l))

    template = settings.JINJA_ENVIRONMENT.get_template('cinema.md')
    return template.render({'cinemas': cinemas})
Exemple #4
0
def display_seances_all(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 = []

    html_data = get_data(url)

    if not html_data:
        return settings.DONT_UNDERSTAND

    for info in html_data:

        cinema_json = info.get('cinema')

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

    empty_data(html_data=html_data)
    if len(seances) < 1:
        return settings.NO_SEANCES_IN_DAY

    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
    })
Exemple #5
0
def get_nearest_cinemas(bot,
                        chat_id,
                        number_of_cinemas,
                        movie_id=None,
                        next_url='/show'):

    # TODO: REWRITE THIS PEACE OF SHIT, PLEASE

    u = get_model(UserProfile, chat_id)
    if not u.location:
        bot.sendMessage(chat_id, settings.CANNOT_FIND_YOU)
        return None, None

    l = json.loads(u.location)
    city_id = detect_city_id_by_location(l)
    url = '{}&latitude={}&longitude={}&sort=distance&city={}'.format(
        settings.URL_CINEMAS.format(settings.KINOHOD_API_KEY),
        l.get('latitude'), l.get('longitude'), city_id)

    film = None
    if movie_id:
        film = Film.get_by_id(str(movie_id))

    data = get_data(url)

    if not data:
        return settings.DONT_UNDERSTAND, None

    cinemas = []
    template = settings.JINJA_ENVIRONMENT.get_template('cinema_where_film.md')

    film_cinemas = get_schedule(movie_id, date=None, city_id=city_id)
    if film_cinemas:
        film_cinema_ids = [str(fc['cinema']['id']) for fc in film_cinemas]
    else:
        return settings.DONT_UNDERSTAND, None

    right_border = number_of_cinemas
    for film_counter in xrange(number_of_cinemas - settings.CINEMA_TO_SHOW,
                               right_border):

        if film_counter < len(data):
            cinema = data[film_counter]
        else:

            return (settings.NO_FILMS,
                    InlineKeyboardMarkup(inline_keyboard=[[
                        dict(text=settings.FIRST_THREE,
                             callback_data=(
                                 '/nearest{}'.format(settings.CINEMA_TO_SHOW)))
                    ]]))

        cinema_id = str(cinema.get('id'))
        cinema_title = cinema.get('shortTitle')

        # poor place because of long request

        # poor place because of double getter
        if film and cinema_id:
            if cinema_id not in film_cinema_ids:
                continue

            link = '{}{}m{}'.format(next_url, cinema_id, movie_id)

        else:
            link = '{}{}'.format(next_url, cinema_id)

        cinemas.append(
            settings.RowCinema(cinema_title, cinema.get('address'),
                               cinema.get('mall'), link))

    if film and len(cinemas) < 1:
        return get_cinemas_where_film(film, settings.CINEMAS_TO_DISPLAY,
                                      chat_id)

    if film:
        mark_up = InlineKeyboardMarkup(inline_keyboard=[[
            dict(text=settings.MORE,
                 callback_data='/nearest{}m{}'.format(
                     number_of_cinemas + settings.CINEMA_TO_SHOW, movie_id))
        ]])
    else:
        mark_up = InlineKeyboardMarkup(inline_keyboard=[[
            dict(text=settings.MORE,
                 callback_data='/nearest{}'.format(number_of_cinemas +
                                                   settings.CINEMA_TO_SHOW))
        ]])

    film_name = film.title if film else None
    return template.render({
        'film_name': film_name,
        'seances': cinemas
    }), mark_up
Exemple #6
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
Exemple #7
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
    })