-
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathmain.py
109 lines (91 loc) · 3.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import json
import os
import sys
import aiohttp
import asyncio
import time
print("Initializing!, Please wait...\n")
working_cookies_path = "working_cookies"
exceptions = 0
working_cookies = 0
expired_cookies = 0
start = time.time()
num_threads = 5 # Define the number of threads here
# ___________________________________________
# | Network Speed | Recommended no. threads |
# |---------------|-------------------------|
# | < 5 Mbps | 1-3 |
# | 5-20 Mbps | 3-5 |
# | 20-100 Mbps | 5-10 |
# | > 100 Mbps | 10-20 |
# |_________________________________________|
async def load_cookies_from_json(json_cookies_path):
with open(json_cookies_path, "r", encoding="utf-8") as cookie_file:
cookie = json.load(cookie_file)
return cookie
async def open_webpage_with_cookies(session, link, json_cookies, filename):
global working_cookies
global expired_cookies
# Request the page
await session.get(link)
# Clear all existing cookies
session.cookie_jar.clear()
for cookie in json_cookies:
session.cookie_jar.update_cookies({cookie["name"]: cookie["value"]})
async with session.get(link) as response:
content = await response.text()
if "Sign In" in content or "btn" in content:
print(f"Cookie Not working - {filename}")
expired_cookies += 1
else:
print(f"Working cookie found! - {filename}")
try:
os.mkdir(working_cookies_path)
working_cookies += 1
return content # Return content if the cookie is working
except FileExistsError:
working_cookies += 1
return content # Return content if the cookie is working
async def process_cookie_file(filename):
filepath = os.path.join("json_cookies", filename)
if os.path.isfile(filepath):
with open(filepath, "r", encoding="utf-8"):
url = "https://netflix.com/login"
try:
cookies = await load_cookies_from_json(filepath)
async with aiohttp.ClientSession() as session:
content = await open_webpage_with_cookies(
session, url, cookies, filename
)
if content:
# Save working cookies to JSON file
with open(f"working_cookies/{filename}.json", "w") as json_file:
json.dump(cookies, json_file)
except json.decoder.JSONDecodeError:
print(
f"Please use cookie_converter.py to convert your cookies to json format! (File: {filename})\n"
)
global exceptions
exceptions += 1
except Exception as e:
print(f"Error occurred: {str(e)} - {filename}\n")
exceptions += 1
async def main():
tasks = []
for filename in os.listdir("json_cookies"):
task = asyncio.create_task(process_cookie_file(filename))
tasks.append(task)
if len(tasks) >= num_threads:
await asyncio.gather(*tasks)
tasks = []
if tasks:
await asyncio.gather(*tasks)
try:
asyncio.run(main())
end = time.time()
print(
f"\nSummary:\nTotal cookies: {len(os.listdir('json_cookies'))}\nWorking cookies: {working_cookies}\nExpired cookies: {expired_cookies}\nInvalid cookies: {exceptions}\nTime Elapsed: {end - start} Seconds"
)
except KeyboardInterrupt:
print("\n\nProgram Interrupted by user")
sys.exit()