<commit_before>#! /usr/bin/python
#-*. coding: utf-8 -*-
#authors: <NAME> y <NAME>

import sys
import time
import threading
from Queue import Queue
import telepot
import telepot.helper
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton
from telepot.delegate import (
    per_chat_id, per_callback_query_origin, create_open, pave_event_space)

"""
Deberíamos empezar de más simple a más complejo, del palo de primero la interfaz
con algún botón, que nos manden su ubicación y mostrar los locales cercanos.
Luego para la base de datos yo preferiría usar mongodb, parece más adecuado para este caso
que sql. De primeras a la base de datos irían los usuarios que entren y luego los locales de
los que tengamos algún dato. O dos colecciones de mongo o todo en la misma, por evitar los 
join si hubiese.
Vamos a intentar desde el principio poner todo el código en inglés, y eso que nos ahorramos
para luego.
Para mirar cosas del telepot la mejor documentacion es esta:
http://telepot.readthedocs.io/en/latest/reference.html
Ahí se puede buscar las funciones en concreto, lo que devuelven, lo que necesitan,...
No está muy allá en algunos casos pero bueno, menos es nada. Entre eso y los ejemplos del tio
va bien.
"""

clients = {}
q = Queue()
# The manager takes everything about creating conexions and to finish them
class Manager(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.num = 0
        self.ids = [1,2]
    
    def run(self):
        while(True):
            message = self.queue.get()
            if message[0] is 'pair':
                self.request(message[1])
            elif message[0] is 'delPair':
                del clients[message[1]]
            time.sleep(3)
            
    def request(self, x):
        if self.num == 1 and self.ids[0] != x:
            self.ids[1] = x
            q1 = Queue()
            q1.put([0,' '])
            q2 = Queue()
            partidas[self.ids[0]] = [self.ids[1],'x',q1]
            partidas[self.ids[1]] = [self.ids[0],'o',q2]
            self.num = 0
        else:
            self.ids[0] = x
            self.num = 1

# One UserHandler created per chat_id. May be useful for sorting out users
# Handles chat messages, we should sort out with telepot.glance what to do with the
# message depending on its type (text, image, location,...)
class UserHandler(telepot.helper.ChatHandler):
    def __init__(self, *args, **kwargs):
        super(UserHandler, self).__init__(*args, **kwargs)

    def on_chat_message(self, msg):
		content_type, chat_type, chat_id = telepot.glance(msg,flavor='chat')
		if content_type == 'text':	
			markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Location', request_location=True)],])
			 
		bot.sendMessage(chat_id, 'Share your location', reply_markup=markup)
		# Some content_type values: text, location, photo
		#			
        #elif content_type == 'location':
        

# One ButtonHandler created per message that has a button pressed.
# There should only be one message from the bot at a time in a chat, so that
# you modify the same message over and over again.
class ButtonHandler(telepot.helper.CallbackQueryOriginHandler):
    def __init__(self, *args, **kwargs):
        super(ButtonHandler, self).__init__(*args, **kwargs)
        
    def on_callback_query(self, msg):
        query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
        bot.answerCallbackQuery(query_id)

            
# El token si quieres puedo ponerlo para que lo pongamos por consola como argumento al 
# lanzar el bot
TOKEN = '255866015:AAFvI3sUR1sOFbeDrUceVyAs44KlfKgx-UE'
manage = Manager(q)
manage.setDaemon = True
manage.start()

bot = telepot.DelegatorBot(TOKEN, [
    pave_event_space()(
        per_chat_id(), create_open, UserHandler, timeout=10),
    pave_event_space()(
        per_callback_query_origin(), create_open, ButtonHandler, timeout=30),
])

bot.message_loop()<commit_msg>Added first aproach to nearby search of places with https://github.com/googlemaps/google-maps-services-python<commit_after>#! /usr/bin/python
#-*. coding: utf-8 -*-
#authors: <NAME> y <NAME>

import sys
import time
import threading
from Queue import Queue
import telepot
import telepot.helper
from telepot.namedtuple import InlineKeyboardMarkup, InlineKeyboardButton
from telepot.namedtuple import ReplyKeyboardMarkup, KeyboardButton
from telepot.delegate import (
    per_chat_id, per_callback_query_origin, create_open, pave_event_space)

import googlemaps
from datetime import datetime

"""
He empezado a usar esto para sacar los locales cercanos a la ubicación que te manden:
https://github.com/googlemaps/google-maps-services-python

Con 'pip install -U googlemaps' basta para poder usarlo
La clave de la api de google la introduzco por consola, para no tenerla publicada en github

Aquí está la documentación de las funciones que tiene:
https://googlemaps.github.io/google-maps-services-python/docs/2.4.5/

Por ahora he estado probando el de places_nearby, que funciona devolviendo un json con los 
resultados de la búsqueda que hayas puesto. Los parámetros de la función vienen explicados
en la documentación


Documentacion de telepot:
http://telepot.readthedocs.io/en/latest/reference.html

"""

# Readying the google maps client

mapclient = googlemaps.Client(key=sys.argv[1]) # Input the api key as the first argument when launching

#clients = {}
#q = Queue()
# The manager takes everything about creating conexions and to finish them
class Manager(threading.Thread):
    def __init__(self, queue):
        threading.Thread.__init__(self)
        self.queue = queue
        self.num = 0
        self.ids = [1,2]
    
    def run(self):
        while(True):
            message = self.queue.get()
            if message[0] is 'pair':
                self.request(message[1])
            elif message[0] is 'delPair':
                del clients[message[1]]
            time.sleep(3)
            
    def request(self, x):
        if self.num == 1 and self.ids[0] != x:
            self.ids[1] = x
            q1 = Queue()
            q1.put([0,' '])
            q2 = Queue()
            partidas[self.ids[0]] = [self.ids[1],'x',q1]
            partidas[self.ids[1]] = [self.ids[0],'o',q2]
            self.num = 0
        else:
            self.ids[0] = x
            self.num = 1

# One UserHandler created per chat_id. May be useful for sorting out users
# Handles chat messages, we should sort out with telepot.glance what to do with the
# message depending on its type (text, image, location,...)
class UserHandler(telepot.helper.ChatHandler):
    def __init__(self, *args, **kwargs):
        super(UserHandler, self).__init__(*args, **kwargs)

    def on_chat_message(self, msg):
        content_type, chat_type, chat_id = telepot.glance(msg,flavor='chat')
        if content_type == 'text':	
            markup = ReplyKeyboardMarkup(keyboard=[[KeyboardButton(text='Location', request_location=True)],])
            bot.sendMessage(chat_id, 'Share your location', reply_markup=markup)
					
        elif content_type == 'location':
            print(msg['location']['latitude'])
            print(mapclient.places_nearby(location=(msg['location']['latitude'], msg['location']['longitude']),
                           type='restaurant', language='es-ES', radius=2000,
                           min_price=1, max_price=4, open_now=True))

            

# One ButtonHandler created per message that has a button pressed.
# There should only be one message from the bot at a time in a chat, so that
# you modify the same message over and over again.
class ButtonHandler(telepot.helper.CallbackQueryOriginHandler):
    def __init__(self, *args, **kwargs):
        super(ButtonHandler, self).__init__(*args, **kwargs)
        
    def on_callback_query(self, msg):
        query_id, from_id, query_data = telepot.glance(msg, flavor='callback_query')
        bot.answerCallbackQuery(query_id)

            
# El token si quieres puedo ponerlo para que lo pongamos por consola como argumento al 
# lanzar el bot
TOKEN = '255866015:AAFvI3sUR1sOFbeDrUceVyAs44KlfKgx-UE'
#manage = Manager(q)
#manage.setDaemon = True
#manage.start()

bot = telepot.DelegatorBot(TOKEN, [
    pave_event_space()(
        per_chat_id(), create_open, UserHandler, timeout=10),
    pave_event_space()(
        per_callback_query_origin(), create_open, ButtonHandler, timeout=30),
])

bot.message_loop(run_forever='Listening')
{'fraction_non_alphanumeric': 0.09945202285181298, 'fraction_numerical': 0.010959542963740236, 'mean_word_length': 3.213163064833006, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1010351', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2658, 'n_words': 813}