Skip to content

Commit

Permalink
persistence cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
moyada committed May 19, 2024
1 parent 8f4e560 commit 211f059
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
19 changes: 15 additions & 4 deletions browser/browser.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import threading

import json
from playwright.sync_api import sync_playwright, Browser, Playwright
import atexit
from core import config
Expand Down Expand Up @@ -29,8 +29,17 @@
'--disable-blink-features=AutomationControlled'
]

ck_path = '/tmp/dy.json'
cks = [{'name': '', 'value': 'douyin.com', 'domain': 'www.douyin.com', 'path': '/video', 'expires': -1, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'device_web_cpu_core', 'value': '8', 'domain': 'www.douyin.com', 'path': '/video', 'expires': -1, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'device_web_memory_size', 'value': '8', 'domain': 'www.douyin.com', 'path': '/video', 'expires': -1, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'ttwid', 'value': '1%7CBaOuINUvKIDzpYaxE9kiGZDiPNvjkCzeq0Opbdl5VIk%7C1716110266%7Ce4314ba7773b6ce02d308eb1d05f0d259aa8fc52a95d8b2c8f417826c9dc2014', 'domain': '.iesdouyin.com', 'path': '/', 'expires': 1747214266.836294, 'httpOnly': True, 'secure': False, 'sameSite': 'Lax'}, {'name': '__ac_nonce', 'value': '06649c3ba0024ddf07136', 'domain': 'www.douyin.com', 'path': '/', 'expires': 1716112066.968196, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': '__ac_signature', 'value': '_02B4Z6wo00f01lokgZAAAIDCmYf-lRRpOWZaBIUAAPDPf6', 'domain': 'www.douyin.com', 'path': '/', 'expires': 1747646267, 'httpOnly': False, 'secure': True, 'sameSite': 'None'}, {'name': 'ttwid', 'value': '1%7CuCmgL7j4f44mWkHwC8EJ4iFp3ApyvEaVeaJj9XE8tyQ%7C1716110267%7Cb6317aa1a0cdb8ae5d3ad58d6f36ad7eeef085e8e2fafe0bc721ac7ae944340a', 'domain': '.douyin.com', 'path': '/', 'expires': 1747214267.365654, 'httpOnly': True, 'secure': False, 'sameSite': 'Lax'}, {'name': 'IsDouyinActive', 'value': 'true', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715068.032838, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'home_can_add_dy_2_desktop', 'value': '%220%22', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715068.033804, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'dy_swidth', 'value': '1280', 'domain': 'www.douyin.com', 'path': '/', 'expires': 1716715068, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'dy_sheight', 'value': '720', 'domain': 'www.douyin.com', 'path': '/', 'expires': 1716715068, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'stream_recommend_feed_params', 'value': '%22%7B%5C%22cookie_enabled%5C%22%3Atrue%2C%5C%22screen_width%5C%22%3A1280%2C%5C%22screen_height%5C%22%3A720%2C%5C%22browser_online%5C%22%3Atrue%2C%5C%22cpu_core_num%5C%22%3A8%2C%5C%22device_memory%5C%22%3A8%2C%5C%22downlink%5C%22%3A10%2C%5C%22effective_type%5C%22%3A%5C%224g%5C%22%2C%5C%22round_trip_time%5C%22%3A150%7D%22', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715068.038004, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'strategyABtestKey', 'value': '%221716110268.177%22', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715068.17778, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'csrf_session_id', 'value': '5e1f3d33adf5c0883052d57bcfef7c11', 'domain': 'www.douyin.com', 'path': '/', 'expires': -1, 'httpOnly': False, 'secure': True, 'sameSite': 'None'}, {'name': 'FORCE_LOGIN', 'value': '%7B%22videoConsumedRemainSeconds%22%3A180%7D', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715068.422286, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 's_v_web_id', 'value': 'verify_lwdbqthk_y54Yk72i_crn9_4nuZ_A7Ep_7maLEOojitjI', 'domain': 'www.douyin.com', 'path': '/', 'expires': 1721294268, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'passport_csrf_token', 'value': '74a651d7ecb7ec478c7b9780e854296f', 'domain': '.douyin.com', 'path': '/', 'expires': 1721294268.496005, 'httpOnly': False, 'secure': True, 'sameSite': 'None'}, {'name': 'passport_csrf_token_default', 'value': '74a651d7ecb7ec478c7b9780e854296f', 'domain': '.douyin.com', 'path': '/', 'expires': 1721294268.496039, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'bd_ticket_guard_client_data', 'value': 'eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCRktIN3J4cXlUdzErL2tsUG9ReWVuQmpCTVo0SndVMWp5emtZTzNmRGpGQ2lyZ3I2ZFFuMEhjeWFYSWtaZTQxeElMamVvbFZUd1VlcG9tQnMzSHprSWM9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoxfQ%3D%3D', 'domain': '.douyin.com', 'path': '/', 'expires': 1721294268.796166, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'bd_ticket_guard_client_web_domain', 'value': '2', 'domain': '.douyin.com', 'path': '/', 'expires': 1721294268.796263, 'httpOnly': False, 'secure': False, 'sameSite': 'Lax'}, {'name': 'msToken', 'value': 'pHD31bx0jhrQmCl2bRjG_k-UN4daiPfOz8z8Ldg0v_BAoaDnuqhEbrwVsTXFFY4pgTAHgtXXeVrjQSrSWrhEO8vj4sNtx31BZNPglZ5c', 'domain': '.douyin.com', 'path': '/', 'expires': 1716715070.120579, 'httpOnly': False, 'secure': True, 'sameSite': 'None'}]

if os.path.exists(ck_path):
try:
with open(ck_path, 'r') as file:
data = file.read()
cks = json.loads(data)
except Exception as e:
print(f"An error occurred: {e}")


class PageContext:
browser = None
Expand All @@ -44,16 +53,18 @@ def __init__(self, p: Playwright):
self.context.add_cookies(cks)

def sync(self):
print("sync")
try:
cks = self.context.cookies()
print(cks)
except Exception as e:
print(e)
pass
return

with open(ck_path, 'w') as file:
json.dump(cks, file, indent=4)


def close(self):
self.sync()
try:
self.browser.close()
self.context.close()
Expand Down
1 change: 0 additions & 1 deletion browser/douyin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def get_info(cls, url: str) -> any:
p.page.wait_for_timeout(100)

path = get_path(p.page.url)
# p.sync()
p.close()

if path == "/":
Expand Down

0 comments on commit 211f059

Please sign in to comment.