Compare commits

..

4 Commits

Author SHA1 Message Date
Mads Marquart
8ac6dc4ae6 Update SERVER_JS_DEFINE_REGEX, so logging in on newer FB versions work 2020-06-14 22:27:26 +02:00
Mads Marquart
a6cf1d5c89 Add _util.now, fixing a few places where datetimes were incorrectly used 2020-06-14 22:26:52 +02:00
Mads Marquart
65b42e6532 Add example of replying to a message 2020-06-07 14:41:05 +02:00
Mads Marquart
8824a1c253 Set override Facebook's auto-locale detection during login
The locale is only used during error handling, and makes it harder for
users to report errors
2020-06-07 13:59:27 +02:00
7 changed files with 34 additions and 11 deletions

View File

@@ -419,7 +419,7 @@ class Client:
Warning: Warning:
This is not finished, and the API may change at any point! This is not finished, and the API may change at any point!
""" """
at = datetime.datetime.utcnow() at = _util.now()
form = { form = {
"folders[0]": "inbox", "folders[0]": "inbox",
"client": "mercury", "client": "mercury",

View File

@@ -16,7 +16,7 @@ from typing import Optional, Mapping, Callable, Any
SERVER_JS_DEFINE_REGEX = re.compile( SERVER_JS_DEFINE_REGEX = re.compile(
r'require(?:\("ServerJS"\).{,100}\.handle\({.*"define":)|(?:\("ServerJSDefine"\)\)?\.handleDefines\()' r'(?:"ServerJS".{,100}\.handle\({.*"define":)|(?:require\("ServerJSDefine"\)\)?\.handleDefines\()'
) )
SERVER_JS_DEFINE_JSON_DECODER = json.JSONDecoder() SERVER_JS_DEFINE_JSON_DECODER = json.JSONDecoder()
@@ -93,6 +93,12 @@ def session_factory() -> requests.Session:
from . import __version__ from . import __version__
session = requests.session() session = requests.session()
# Override Facebook's locale detection during the login process.
# The locale is only used when giving errors back to the user, so giving the errors
# back in English makes it easier for users to report.
session.cookies = session.cookies = requests.cookies.merge_cookies(
session.cookies, {"locale": "en_US"}
)
session.headers["Referer"] = "https://www.messenger.com/" session.headers["Referer"] = "https://www.messenger.com/"
# We won't try to set a fake user agent to mask our presence! # We won't try to set a fake user agent to mask our presence!
# Facebook allows us access anyhow, and it makes our motives clearer: # Facebook allows us access anyhow, and it makes our motives clearer:
@@ -185,7 +191,6 @@ def get_error_data(html: str) -> Optional[str]:
html, "html.parser", parse_only=bs4.SoupStrainer("form", id="login_form") html, "html.parser", parse_only=bs4.SoupStrainer("form", id="login_form")
) )
# Attempt to extract and format the error string # Attempt to extract and format the error string
# The error message is in the user's own language!
return " ".join(list(soup.stripped_strings)[1:3]) or None return " ".join(list(soup.stripped_strings)[1:3]) or None
@@ -481,7 +486,7 @@ class Session:
return self._post("/api/graphqlbatch/", data, as_graphql=True) return self._post("/api/graphqlbatch/", data, as_graphql=True)
def _do_send_request(self, data): def _do_send_request(self, data):
now = datetime.datetime.utcnow() now = _util.now()
offline_threading_id = _util.generate_offline_threading_id() offline_threading_id = _util.generate_offline_threading_id()
data["client"] = "mercury" data["client"] = "mercury"
data["author"] = "fbid:{}".format(self._user_id) data["author"] = "fbid:{}".format(self._user_id)

View File

@@ -116,8 +116,14 @@ class ThreadABC(metaclass=abc.ABCMeta):
reply_to_id: Optional message to reply to reply_to_id: Optional message to reply to
Example: Example:
Send a message with a mention to a thread.
>>> mention = fbchat.Mention(thread_id="1234", offset=5, length=2) >>> mention = fbchat.Mention(thread_id="1234", offset=5, length=2)
>>> thread.send_text("A message", mentions=[mention]) >>> message_id = thread.send_text("A message", mentions=[mention])
Reply to the message.
>>> thread.send_text("A reply", reply_to_id=message_id)
Returns: Returns:
The sent message The sent message

View File

@@ -56,7 +56,7 @@ def parse_json(text: str) -> Any:
def generate_offline_threading_id(): def generate_offline_threading_id():
ret = datetime_to_millis(datetime.datetime.utcnow()) ret = datetime_to_millis(now())
value = int(random.random() * 4294967295) value = int(random.random() * 4294967295)
string = ("0000000000000000000000" + format(value, "b"))[-22:] string = ("0000000000000000000000" + format(value, "b"))[-22:]
msgs = format(ret, "b") + string msgs = format(ret, "b") + string
@@ -158,3 +158,11 @@ def timedelta_to_seconds(td: datetime.timedelta) -> int:
The returned seconds will be rounded to the nearest whole number. The returned seconds will be rounded to the nearest whole number.
""" """
return round(td.total_seconds()) return round(td.total_seconds())
def now() -> datetime.datetime:
"""The current time.
Similar to datetime.datetime.now(), but returns a non-naive datetime.
"""
return datetime.datetime.now(tz=datetime.timezone.utc)

View File

@@ -1,6 +1,5 @@
import pytest import pytest
import fbchat import fbchat
import datetime
import os import os
pytestmark = pytest.mark.online pytestmark = pytest.mark.online
@@ -95,11 +94,11 @@ def test_upload_many(client, open_resource):
def test_mark_as_read(client, user, group): def test_mark_as_read(client, user, group):
client.mark_as_read([user, group], datetime.datetime.now()) client.mark_as_read([user, group], fbchat._util.now())
def test_mark_as_unread(client, user, group): def test_mark_as_unread(client, user, group):
client.mark_as_unread([user, group], datetime.datetime.now()) client.mark_as_unread([user, group], fbchat._util.now())
def test_move_threads(client, user, group): def test_move_threads(client, user, group):

View File

@@ -1,6 +1,6 @@
import datetime import datetime
import pytest import pytest
from fbchat import ParseError from fbchat import ParseError, _util
from fbchat._session import ( from fbchat._session import (
parse_server_js_define, parse_server_js_define,
base36encode, base36encode,
@@ -73,7 +73,7 @@ def test_prefix_url():
def test_generate_message_id(): def test_generate_message_id():
# Returns random output, so hard to test more thoroughly # Returns random output, so hard to test more thoroughly
assert generate_message_id(datetime.datetime.utcnow(), "def") assert generate_message_id(_util.now(), "def")
def test_session_factory(): def test_session_factory():

View File

@@ -15,6 +15,7 @@ from fbchat._util import (
seconds_to_timedelta, seconds_to_timedelta,
millis_to_timedelta, millis_to_timedelta,
timedelta_to_seconds, timedelta_to_seconds,
now,
) )
@@ -245,3 +246,7 @@ def test_timedelta_to_seconds():
assert timedelta_to_seconds(datetime.timedelta(seconds=1)) == 1 assert timedelta_to_seconds(datetime.timedelta(seconds=1)) == 1
assert timedelta_to_seconds(datetime.timedelta(hours=1)) == 3600 assert timedelta_to_seconds(datetime.timedelta(hours=1)) == 3600
assert timedelta_to_seconds(datetime.timedelta(days=1)) == 86400 assert timedelta_to_seconds(datetime.timedelta(days=1)) == 86400
def test_now():
assert datetime_to_millis(now()) == datetime_to_millis(datetime.datetime.now())