Browse Source

Terminèw le parsage des requêtes liées aux messages, (presque) terminé le serveuw

master
hadware 8 years ago
parent
commit
18c3d02044
  1. 77
      tools/coco/client.py
  2. 50
      tools/coco/requests.py

77
tools/coco/client.py

@ -1,12 +1,14 @@
from typing import List
from .requests import LoginRequest, PostLoginRequest
import random
from typing import List, Dict, Tuple, Union
from .requests import LoginRequest, PostLoginRequest, PulseRequest, SendMsgRequest
from ..commons import BotMessage, Message, AbstractResponse
import logging
class Interlocutor:
def __init__(self, nick: str, age:int, city: str, is_male: bool, conv_id: str):
def __init__(self, nick: str, age: int, city: str, is_male: bool, conv_id: str):
self.nick = nick
self.age = age
self.is_male = is_male
@ -17,27 +19,58 @@ class Interlocutor:
def from_string(cls, str):
# 47130922100412004Rastapopoulos
# 47 (age) 1 (sexe) 30922 (city id) 100412(conv id)
age = str[:2]
age = int(str[:2])
is_male = int(str[2:3]) in (1, 6)
city_id = str[3:8]
conv_id = str[8:14]
nick = str[17:]
return cls(nick, age, city_id, is_male, conv_id)
def to_botmessage(self):
sex_indic = "un homme" if self.is_male else "une femme"
return BotMessage("%s est %s de %i ans" %(self.nick, sex_indic, self.age))
def __eq__(self, other):
return other.nick == self.nick
def __hash__(self):
return hash(self.nick)
class CocoClient:
def __init__(self):
self.interlocutors = [] # type: List[Interlocutor]
self.current_interlocutor = None # type: Interlocutor
self.histories = {} # type:Dict[Interlocutor,List[Tuple]]
self.user_id = None # type:str
self.user_pass = None # type:str
self.nick = None # type:str
def _format_history(self, interlocutor: Interlocutor):
if interlocutor in self.histories:
return [BotMessage("💬 %s: %s" % (nick, msg))
for nick, msg in self.histories[interlocutor][:5]]
else:
return []
def __process_and_format_received_msg(self, received_msgs):
out = []
for user_code, msg in received_msgs:
user = Interlocutor.from_string(user_code)
self.histories[user].append((user.nick, msg))
if user == self.current_interlocutor:
out.append(Message("💬 %s: %s" % (user.nick, msg)))
else:
out.append(BotMessage("💬 %s: %s" % (user.nick, msg)))
return out
def connect(self, nick: str, age: int, is_female: bool, zip_code: str):
self.nick = nick
self.histories = {}
self.current_interlocutor = None
login_req = LoginRequest(nick, age, is_female, zip_code)
self.user_id, self.user_pass = login_req.retrieve()
logging.info("Logged in to coco as %s" % self.nick)
@ -45,11 +78,35 @@ class CocoClient:
post_login_req.retrieve()
logging.info("Post login successful")
def pulse(self):
pass
def pulse(self) -> List[AbstractResponse]:
pulse_req = PulseRequest(self.user_id, self.user_pass)
received_msg = pulse_req.retrieve()
return self.__process_and_format_received_msg(received_msg)
def send_msg(self, msg: str) -> List[AbstractResponse]:
sendmsg_req = SendMsgRequest(self.user_id, self.user_pass, self.current_interlocutor.id, msg)
output = sendmsg_req.retrieve()
self.histories[self.current_interlocutor].append((self.nick, msg))
out_msg = Message("💬 %s: %s" % (self.nick, msg))
if output:
return [out_msg] + self.__process_and_format_received_msg(output)
else:
return [out_msg]
def send_msg(self):
pass
def switch_conv(self, nick: str=None) -> Union[List[BotMessage], BotMessage]:
new_interlocutor = None
if nick is not None:
for usr in self.interlocutors:
if usr.nick.upper() == nick.upper():
new_interlocutor = usr
break
else:
new_interlocutor = random.choice(self.interlocutors)
def switch_conv(self, nick: str=None):
pass
if new_interlocutor is None:
return BotMessage("Impossible de trouver l'utilisateur")
else:
self.current_interlocutor = new_interlocutor
return [new_interlocutor.to_botmessage()] + \
self._format_history(self.current_interlocutor)

50
tools/coco/requests.py

@ -1,8 +1,11 @@
from urllib.request import Request, urlopen
from random import randint, choice, random
from string import ascii_uppercase
from typing import Tuple, List
import re
from .tools import get_city_id, coco_cipher, encode_msg, decode_msg
from .tools import get_city_id, coco_cipher, encode_msg
class BaseCocoRequest:
host = 'http://cloud.coco.fr/'
@ -61,28 +64,37 @@ class PostLoginRequest(LoggedInRequest):
def _parse_response(self, response):
"""Checks if the post-login was successful"""
# TODO
pass
return response[:5] == "99556"
class PulseRequest(LoggedInRequest):
# typical response :
# process1('#669276787#30130916276787003HotelDiscret#salut_toi#47130922100412004Rastapopoulos#Mes_hommages,_Mademoiselle...#47130922100412004Rastapopoulos#Jamais_un_mari_ne_sera_si_bien_veng*r_que_par_l*8amant_de_sa_femme.#40636427396758003leo913#cam.!7w2738702leo913#396758#');
# process1('#669276787#30130916276787003HotelDiscret#chaude=#292223#32130926292223003HDirect#Salut,_te_faire_payer_pour_un_plan_sexe_ca_te_plairais_=#');
# process1('#66945630927183748003WolfiSoDentelle#en_manque_de_sommeil_peut_etre_=_#');
# ^ le type a 45 balais donc il doit falloir couper après 669
# process1('#66929630926396791003Clouds#bonsoir,_comment_vas-tu_=_que_cherches_tu_=#');
# idem avec lui, il a 29 ans
user_match = re.compile(r"[0-9]{17}[A-Za-z0-9]+")
def _get_url(self):
return self.host + "95" + self.token + "?" + str(random())
def _parse_response(self, response):
"""Can either be a single message or several messages"""
# TODO
pass
class SendMsgRequest(LoggedInRequest):
if response == "Z":
return []
response = response[3:] # cutting the 669
split = response.split("#")
it = iter(split)
output_msgs = [] # type: List[Tuple[str,str]]
while True:
try:
current = next(it)
if re.match(self.user_match, current):
msg = next(it)
decoded_msg = decode_msg(msg)
output_msgs.append((current, decoded_msg))
except StopIteration:
break
return output_msgs
class SendMsgRequest(PulseRequest):
def __init__(self, user_id, password, conv_id: str, msg: str):
super().__init__(user_id, password)
@ -90,10 +102,12 @@ class SendMsgRequest(LoggedInRequest):
self.msg = msg
def _get_url(self):
return self.host + "99" + self.token + self.conv_id + encode_msg(self.msg)
return self.host + "99" + self.token + self.conv_id + str(randint(0, 6)) + encode_msg(self.msg)
def _parse_response(self, response):
"""Response to a send message request can either just be #97x or an
actual message (like in pulse request)"""
# TODO
pass
if response[:2] == "97":
return []
else:
return super()._parse_response(response)
Loading…
Cancel
Save