@@ -19,6 +19,12 @@ from bs4 import BeautifulSoup as bs
|
|||||||
from .utils import *
|
from .utils import *
|
||||||
from .models import *
|
from .models import *
|
||||||
|
|
||||||
|
# URLs
|
||||||
|
LoginURL ="https://m.facebook.com/login.php?login_attempt=1"
|
||||||
|
SearchURL ="https://www.facebook.com/ajax/typeahead/search.php"
|
||||||
|
SendURL ="https://www.facebook.com/ajax/mercury/send_messages.php"
|
||||||
|
MessagesURL="https://www.facebook.com/ajax/mercury/threadlist_info.php"
|
||||||
|
|
||||||
class Client(object):
|
class Client(object):
|
||||||
"""A client for the Facebook Chat (Messenger).
|
"""A client for the Facebook Chat (Messenger).
|
||||||
|
|
||||||
@@ -46,6 +52,7 @@ class Client(object):
|
|||||||
self.debug = debug
|
self.debug = debug
|
||||||
self._session = requests.session()
|
self._session = requests.session()
|
||||||
self.req_counter = 1;
|
self.req_counter = 1;
|
||||||
|
self.payloadDefault={}
|
||||||
|
|
||||||
if not user_agent:
|
if not user_agent:
|
||||||
user_agent = choice(USER_AGENTS)
|
user_agent = choice(USER_AGENTS)
|
||||||
@@ -64,32 +71,48 @@ class Client(object):
|
|||||||
raise Exception("id or password is wrong")
|
raise Exception("id or password is wrong")
|
||||||
|
|
||||||
self.threads = []
|
self.threads = []
|
||||||
self.threads = []
|
|
||||||
|
|
||||||
|
|
||||||
def _console(self, msg):
|
def _console(self, msg):
|
||||||
if self.debug: print(msg)
|
if self.debug: print(msg)
|
||||||
|
|
||||||
|
def _setttstamp(self):
|
||||||
|
for i in self.fb_dtsg:
|
||||||
|
self.ttstamp += str(ord(i))
|
||||||
|
self.ttstamp += '2'
|
||||||
|
|
||||||
|
def _generatePayload(self, query):
|
||||||
|
if query:
|
||||||
|
payload=self.payloadDefault.copy()
|
||||||
|
payload.update(query)
|
||||||
|
payload['__req'] = str_base(self.req_counter, 36)
|
||||||
|
else:
|
||||||
|
payload = None
|
||||||
|
self.req_counter+=1
|
||||||
|
return payload
|
||||||
|
|
||||||
def _get(self, url, query=None, timeout=30):
|
def _get(self, url, query=None, timeout=30):
|
||||||
self.req_counter += 1
|
payload=self._generatePayload(query)
|
||||||
return self._session.get(url, headers=self._header, params=query, timeout=timeout)
|
return self._session.get(url, headers=self._header, params=payload, timeout=timeout)
|
||||||
|
|
||||||
def _post(self, url, query=None, timeout=30):
|
def _post(self, url, query=None, timeout=30):
|
||||||
self.req_counter += 1
|
payload=self._generatePayload(query)
|
||||||
|
return self._session.post(url, headers=self._header, data=payload, timeout=timeout)
|
||||||
|
|
||||||
|
def _cleanPost(self, url, query=None, timeout=30):
|
||||||
|
self.req_counter+=1
|
||||||
return self._session.post(url, headers=self._header, data=query, timeout=timeout)
|
return self._session.post(url, headers=self._header, data=query, timeout=timeout)
|
||||||
|
|
||||||
def login(self):
|
def login(self):
|
||||||
if not (self.email and self.password):
|
if not (self.email and self.password):
|
||||||
raise Exception("id and password or config is needed")
|
raise Exception("id and password or config is needed")
|
||||||
|
|
||||||
soup = bs(self._get("https://m.facebook.com/").text)
|
soup = bs(self._get("https://m.facebook.com/").text, "lxml")
|
||||||
data = dict((elem['name'], elem['value']) for elem in soup.findAll("input") if elem.has_attr('value') and elem.has_attr('name'))
|
data = dict((elem['name'], elem['value']) for elem in soup.findAll("input") if elem.has_attr('value') and elem.has_attr('name'))
|
||||||
data['email'] = self.email
|
data['email'] = self.email
|
||||||
data['pass'] = self.password
|
data['pass'] = self.password
|
||||||
data['login'] = 'Log In'
|
data['login'] = 'Log In'
|
||||||
|
|
||||||
r = self._post("https://m.facebook.com/login.php?login_attempt=1", data)
|
r = self._cleanPost(LoginURL, data)
|
||||||
self.r = r
|
|
||||||
|
|
||||||
if 'home' in r.url:
|
if 'home' in r.url:
|
||||||
self.client_id = hex(int(random()*2147483648))[2:]
|
self.client_id = hex(int(random()*2147483648))[2:]
|
||||||
@@ -99,15 +122,12 @@ class Client(object):
|
|||||||
self.ttstamp = ''
|
self.ttstamp = ''
|
||||||
|
|
||||||
r = self._get('https://www.facebook.com/')
|
r = self._get('https://www.facebook.com/')
|
||||||
self.rev = int(r.text.split('"revision":',1)[1].split(",",1)[0])
|
self.payloadDefault['__rev']= int(r.text.split('"revision":',1)[1].split(",",1)[0])
|
||||||
#self.rev = int(random()*100000)
|
self.payloadDefault['__user']= self.uid
|
||||||
|
|
||||||
soup = bs(r.text)
|
soup = bs(r.text, "lxml")
|
||||||
self.fb_dtsg = soup.find("input", {'name':'fb_dtsg'})['value']
|
self.fb_dtsg = soup.find("input", {'name':'fb_dtsg'})['value']
|
||||||
|
self._setttstamp()
|
||||||
for i in self.fb_dtsg:
|
|
||||||
self.ttstamp += str(ord(i))
|
|
||||||
self.ttstamp += '2'
|
|
||||||
|
|
||||||
self.form = {
|
self.form = {
|
||||||
'channel' : self.user_channel,
|
'channel' : self.user_channel,
|
||||||
@@ -146,14 +166,10 @@ class Client(object):
|
|||||||
'path' : "/home.php",
|
'path' : "/home.php",
|
||||||
'request_id' : str(uuid1()),
|
'request_id' : str(uuid1()),
|
||||||
'__a' : '1',
|
'__a' : '1',
|
||||||
'__user' : self.uid,
|
|
||||||
'__req' : str_base(self.req_counter, 36),
|
|
||||||
'__rev' : self.rev,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
r = self._get("https://www.facebook.com/ajax/typeahead/search.php", payload)
|
r = self._get(SearchURL, payload)
|
||||||
self.j = j = get_json(r.text)
|
self.j = j = get_json(r.text)
|
||||||
self.r = r
|
|
||||||
|
|
||||||
users = []
|
users = []
|
||||||
for entry in j['payload']['entries']:
|
for entry in j['payload']['entries']:
|
||||||
@@ -174,9 +190,6 @@ class Client(object):
|
|||||||
'fb_dtsg' : self.fb_dtsg,
|
'fb_dtsg' : self.fb_dtsg,
|
||||||
'ttstamp' : self.ttstamp,
|
'ttstamp' : self.ttstamp,
|
||||||
'__a' : '1',
|
'__a' : '1',
|
||||||
'__user' : self.uid,
|
|
||||||
'__req' : str_base(self.req_counter, 36),
|
|
||||||
'__rev' : self.rev,
|
|
||||||
'message_batch[0][action_type]' : 'ma-type:user-generated-message',
|
'message_batch[0][action_type]' : 'ma-type:user-generated-message',
|
||||||
'message_batch[0][author]' : 'fbid:' + str(self.uid),
|
'message_batch[0][author]' : 'fbid:' + str(self.uid),
|
||||||
'message_batch[0][specific_to_list][0]' : 'fbid:' + str(thread_id),
|
'message_batch[0][specific_to_list][0]' : 'fbid:' + str(thread_id),
|
||||||
@@ -202,9 +215,10 @@ class Client(object):
|
|||||||
'message_batch[0][has_attachment]' : False
|
'message_batch[0][has_attachment]' : False
|
||||||
}
|
}
|
||||||
|
|
||||||
r = self._post("https://www.facebook.com/ajax/mercury/send_messages.php", data)
|
r = self._post(SendURL, data)
|
||||||
return r.ok
|
return r.ok
|
||||||
|
|
||||||
|
|
||||||
def getThreadList(self, start, end=None):
|
def getThreadList(self, start, end=None):
|
||||||
"""Get thread list of your facebook account.
|
"""Get thread list of your facebook account.
|
||||||
|
|
||||||
@@ -221,22 +235,31 @@ class Client(object):
|
|||||||
'fb_dtsg' : self.fb_dtsg,
|
'fb_dtsg' : self.fb_dtsg,
|
||||||
'ttstamp' : self.ttstamp,
|
'ttstamp' : self.ttstamp,
|
||||||
'__a' : '1',
|
'__a' : '1',
|
||||||
'__user' : self.uid,
|
|
||||||
'__req' : str_base(self.req_counter, 36),
|
|
||||||
'__rev' : self.rev,
|
|
||||||
'inbox[offset]' : start,
|
'inbox[offset]' : start,
|
||||||
'inbox[limit]' : end,
|
'inbox[limit]' : end,
|
||||||
}
|
}
|
||||||
|
|
||||||
r = self._post("https://www.facebook.com/ajax/mercury/threadlist_info.php", data)
|
r = self._post(MessagesURL, data)
|
||||||
if not r.ok:
|
if not r.ok or len(r.text) == 0:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
j = get_json(r.text)
|
j = get_json(r.text)
|
||||||
|
|
||||||
|
# Get names for people
|
||||||
|
participants={}
|
||||||
|
for participant in j['payload']['participants']:
|
||||||
|
participants[participant["fbid"]] = participant["name"]
|
||||||
|
|
||||||
|
# Prevent duplicates in self.threads
|
||||||
|
threadIDs=[getattr(x, "thread_id") for x in self.threads]
|
||||||
for thread in j['payload']['threads']:
|
for thread in j['payload']['threads']:
|
||||||
t = Thread(**thread)
|
if thread["thread_id"] not in threadIDs:
|
||||||
self.threads.append(t)
|
try:
|
||||||
|
thread["other_user_name"] = participants[int(thread["other_user_fbid"])]
|
||||||
|
except:
|
||||||
|
thread["other_user_name"] = ""
|
||||||
|
t = Thread(**thread)
|
||||||
|
self.threads.append(t)
|
||||||
|
|
||||||
return self.threads
|
return self.threads
|
||||||
|
|
||||||
|
@@ -1,7 +1,7 @@
|
|||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
from time import time
|
from time import time
|
||||||
from random import random, choice
|
from random import random
|
||||||
USER_AGENTS = [
|
USER_AGENTS = [
|
||||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36",
|
||||||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/601.1.10 (KHTML, like Gecko) Version/8.0.5 Safari/601.1.10",
|
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/601.1.10 (KHTML, like Gecko) Version/8.0.5 Safari/601.1.10",
|
||||||
|
Reference in New Issue
Block a user