Add session to_file and from_file
Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
@@ -19,7 +19,8 @@ SERVER_JS_DEFINE_REGEX = re.compile(
|
||||
r'(?:"ServerJS".{,100}\.handle\({.*"define":)'
|
||||
r'|(?:ServerJS.{,100}\.handleWithCustomApplyEach\(ScheduledApplyEach,{.*"define":)'
|
||||
r'|(?:require\("ServerJSDefine"\)\)?\.handleDefines\()'
|
||||
r'|(?:"require":\[\["ScheduledServerJS".{,100}"define":)')
|
||||
r'|(?:"require":\[\["ScheduledServerJS".{,100}"define":)'
|
||||
)
|
||||
SERVER_JS_DEFINE_JSON_DECODER = json.JSONDecoder()
|
||||
|
||||
|
||||
@@ -112,7 +113,7 @@ def login_cookies(at: datetime.datetime):
|
||||
|
||||
|
||||
def client_id_factory() -> str:
|
||||
return hex(int(random.random() * 2 ** 31))[2:]
|
||||
return hex(int(random.random() * 2**31))[2:]
|
||||
|
||||
|
||||
def find_form_request(html: str):
|
||||
@@ -324,8 +325,8 @@ class Session:
|
||||
"sec-fetch-mode": "navigate",
|
||||
"sec-fetch-site": "same-origin",
|
||||
"sec-fetch-user": "?1",
|
||||
"upgrade-insecure-requests": "1"
|
||||
}
|
||||
"upgrade-insecure-requests": "1",
|
||||
},
|
||||
)
|
||||
except requests.RequestException as e:
|
||||
_exception.handle_requests_error(e)
|
||||
@@ -554,3 +555,30 @@ class Session:
|
||||
data["__user"] = self.user.id
|
||||
j = self._post("/message_share_attachment/fromURI/", data)
|
||||
return j["payload"]["share_data"]
|
||||
|
||||
def to_file(self, filename):
|
||||
"""Save the session to a file.
|
||||
|
||||
Args:
|
||||
filename: The file to save the session to
|
||||
|
||||
Example:
|
||||
>>> session = fbchat.Session.from_cookies(cookies)
|
||||
>>> session.to_file("session.json")
|
||||
"""
|
||||
with open(filename, "w") as f:
|
||||
json.dump(self.get_cookies(), f)
|
||||
|
||||
@classmethod
|
||||
def from_file(cls, filename):
|
||||
"""Load a session from a file.
|
||||
|
||||
Args:
|
||||
filename: The file to load the session from
|
||||
|
||||
Example:
|
||||
>>> session = fbchat.Session.from_file("session.json")
|
||||
"""
|
||||
with open(filename, "r") as f:
|
||||
cookies = json.load(f)
|
||||
return cls.from_cookies(cookies)
|
||||
|
Reference in New Issue
Block a user