From 256a1b496cc07a9453320766657f79fb5bef42a7 Mon Sep 17 00:00:00 2001 From: Taehoon Kim Date: Sun, 19 Apr 2015 01:10:04 +0900 Subject: [PATCH] login added --- README.rst | 3 +- fbchat/__init__.py | 2 +- fbchat/client.py | 98 ++++++++++++++++++++++++++++++++++++++++++++++ fbchat/core.py | 51 ------------------------ setup.py | 3 +- 5 files changed, 103 insertions(+), 54 deletions(-) create mode 100644 fbchat/client.py delete mode 100644 fbchat/core.py diff --git a/README.rst b/README.rst index f4a7db0..75e0f8a 100644 --- a/README.rst +++ b/README.rst @@ -14,7 +14,8 @@ fbchat .. image:: https://pypip.in/license/fbchat/badge.svg?style=flat :target: https://pypi.python.org/pypi/fbchat -Facebook Chat (`Messenger `__) for Python. +Facebook Chat (`Messenger `__) for Python. This project was inspired by `facebook-chat-api `__. + Installation ============ diff --git a/fbchat/__init__.py b/fbchat/__init__.py index 7de5559..408b5a0 100644 --- a/fbchat/__init__.py +++ b/fbchat/__init__.py @@ -5,7 +5,7 @@ Facebook Chat (Messenger) for Python """ -from .core import * +from .client import * __version__ = '0.0.1' diff --git a/fbchat/client.py b/fbchat/client.py new file mode 100644 index 0000000..7167f41 --- /dev/null +++ b/fbchat/client.py @@ -0,0 +1,98 @@ +# -*- coding: UTF-8 -*- + + +""" +Core components for fbchat +""" + + +import requests +import time +from bs4 import BeautifulSoup as bs + +CHROME = "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" +SAFARI = "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" + +class Client(object): + """A client for the Facebook Chat (Messenger). + + See http://github.com/carpedm20/fbchat for complete + documentation for the API. + + """ + + def __init__(self, email, password, debug=True, user_agent=None): + """A client for the Facebook Chat (Messenger). + + :param email: Facebook `email` or `id` or `phone number` + :param password: Facebook account password + + import fbchat + chat = fbchat.Client(email, password) + + """ + + if not (email and password): + raise Exception("id and password or config is needed") + + self.email = email + self.password = password + self.debug = debug + self._session = requests.session() + + if not user_agent: + user_agent = CHROME + + self._header = { + 'Content-Type' : 'application/x-www-form-urlencoded', + 'Referer' : 'https://www.facebook.com/', + 'Origin' : 'https://www.facebook.com', + 'User-Agent' : user_agent, + 'Connection' : 'keep-alive', + } + + self.console("Logging in...") + + if not self.login(): + raise Exception("id or password is wrong") + + def console(self, msg): + if self.debug: print(msg) + + def login(self): + if not (self.email and self.password): + raise Exception("id and password or config is needed") + + soup = bs(self._get("https://m.facebook.com/").text) + data = dict((elem['name'], elem['value']) for elem in soup.findAll("input") if elem.has_attr('value')) + data['email'] = self.email + data['pass'] = self.password + data['login'] = 'Log In' + + r = self._post("https://m.facebook.com/login.php?login_attempt=1", data) + + if 'expires' in r.headers.keys(): + return True + else: + return False + + def _get(self, url, query=None, timeout=30): + return self._session.get(url, headers=self._header, params=query, timeout=timeout) + + def _post(self, url, query=None, timeout=30): + return self._session.post(url, headers=self._header, data=query, timeout=timeout) + + def listen(self): + pass + + def getUserId(self): + pass + + def sendMessage(self): + pass + + def sendSticker(self): + pass + + def markAsRead(self): + pass diff --git a/fbchat/core.py b/fbchat/core.py deleted file mode 100644 index 7226669..0000000 --- a/fbchat/core.py +++ /dev/null @@ -1,51 +0,0 @@ -# -*- coding: UTF-8 -*- - - -""" -Core components for fbchat -""" - - -import requests -import time - -class Client: - """A client for the Facebook Chat (Messenger). - - See http://github.com/carpedm20/fbchat for complete - documentation for the API. - - """ - - def __init__(self, email, password): - """A client for the Facebook Chat (Messenger). - - :param email: Facebook `email` or `id` or `phone number` - :param password: Facebook account password - - import fbchat - chat = fbchat.Client(email, password) - - """ - - if not (email and password): - raise Exception("id and password or config is needed") - - - self.email = email - self.password = password - - def listen(self): - pass - - def getUserId(self): - pass - - def sendMessage(self): - pass - - def sendSticker(self): - pass - - def markAsRead(self): - pass diff --git a/setup.py b/setup.py index 5f6aea6..80aafdf 100644 --- a/setup.py +++ b/setup.py @@ -71,7 +71,8 @@ setup( long_description=readme_content, packages=['fbchat'], install_requires=[ - 'requests' + 'requests', + 'bs4' ], url=source, version=version,