Skip to content

Commit

Permalink
cookie_backup
Browse files Browse the repository at this point in the history
  • Loading branch information
madwind committed Aug 24, 2022
1 parent 2471e9c commit 903f009
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions auto_sign_in.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class PluginAutoSignIn:
'max_workers': {'type': 'integer'},
'get_messages': {'type': 'boolean', 'default': True},
'get_details': {'type': 'boolean', 'default': True},
'cookie_backup': {'type': 'boolean', 'default': True},
'aipocr': {
'type': 'object',
'properties': {
Expand Down
1 change: 1 addition & 0 deletions ptsites/base/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def request(self,
entry.fail_with_prefix('Detected CloudFlare DDoS-GUARD')
elif response is not None and response.status_code != 200:
entry.fail_with_prefix(f'response.status_code={response.status_code}')
entry['session_cookie'] = (' '.join(list(map(lambda x: f'{x[0]}={x[1]};', self.session.cookies.items()))))
return response
except Exception as e:
entry.fail_with_prefix(NetworkState.NETWORK_ERROR.value.format(url=url, error=e))
Expand Down
36 changes: 36 additions & 0 deletions ptsites/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import importlib
import pathlib
import pkgutil
import threading
import os

from flexget import plugin
from flexget.entry import Entry
Expand All @@ -14,6 +16,8 @@
from .base.reseed import Reseed
from .base.sign_in import SignIn

lock = threading.Semaphore(1)


def build_sign_in_schema() -> dict:
module = None
Expand All @@ -37,6 +41,33 @@ def build_sign_in_entry(entry: SignInEntry, config: dict) -> None:
raise plugin.PluginError(f"site: {entry['site_name']}, error: {e}")


def save_cookie(entry):
file_name = 'cookies.bak'
site_name = entry['site_name']
session_cookie = entry.get('session_cookie')
if not session_cookie:
return
with lock:
mode = 'r+'
if not os.path.exists(file_name):
mode = 'w+'

with open(file_name, mode, encoding='utf-8') as f:
lines = f.readlines()
updated = False
cookie = f"{site_name}: '{session_cookie}'\n"
for i in range(len(lines)):
if lines[i].startswith(f'{site_name}:'):
lines[i] = cookie
updated = True
break
if not updated:
lines.append(cookie)
lines.sort()
f.seek(0)
f.writelines(lines)


def sign_in(entry: SignInEntry, config: dict) -> None:
try:
site_class = get_site_class(entry['class_name'])
Expand Down Expand Up @@ -68,6 +99,11 @@ def sign_in(entry: SignInEntry, config: dict) -> None:
return
if entry['details']:
logger.info(f"site_name: {entry['site_name']}, details: {entry['details']}")

if config.get('cookie_backup', True):
if entry.failed:
return
save_cookie(entry)
clean_entry_attr(entry)


Expand Down

0 comments on commit 903f009

Please sign in to comment.