amit0987 commited on
Commit
2ab9c74
·
1 Parent(s): 74bfe3b

Create app.py

Browse files
Files changed (6) hide show
  1. .gitignore +2 -0
  2. ImportantVariables.py +30 -0
  3. app.py +12 -0
  4. cookies.json +192 -0
  5. requirements.txt +0 -0
  6. selinium.py +182 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ /Chrome
2
+ /.idea
ImportantVariables.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+
4
+
5
+ file_path=os.path.abspath(__file__)
6
+ # print(file_path)
7
+ current_Folder_Path = os.path.dirname(file_path)
8
+ # print(current_Folder_Path)
9
+ # print(os.path.join(current_Folder_Path,"my_money_pointer"))
10
+
11
+ # print(current_Folder_Path)
12
+ yt_links_for_facebook = "../json_files/yt_links_for_facebook.json"
13
+ cookies_file=current_Folder_Path +'/cookies.json'
14
+ save_cookies_json=current_Folder_Path +'/save_cookies.json'
15
+
16
+ screenshot_path =current_Folder_Path +'/reports/screenshot'
17
+ reports_path=current_Folder_Path +'/reports/'
18
+ os.makedirs(os.path.dirname(reports_path), exist_ok=True)
19
+
20
+ downloaded_videos_folder = current_Folder_Path + "/downloaded_videos"
21
+ os.makedirs(downloaded_videos_folder, exist_ok=True)
22
+ new_user_data_directory = current_Folder_Path +'/Chrome/new_user_data_directory'
23
+
24
+ print("new_user_data_directory : \t",new_user_data_directory)
25
+
26
+ if not os.path.exists(new_user_data_directory):
27
+ os.makedirs(new_user_data_directory)
28
+
29
+
30
+
app.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+
4
+ import selinium
5
+
6
+
7
+ def get_image(name):
8
+ selinium.main()
9
+ return Image.open("reports/screenshot.png3_1.png") # Replace with your image file
10
+
11
+ demo = gr.Interface(fn=get_image, inputs="text", outputs="image")
12
+ demo.launch()
cookies.json ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "domain": ".youtube.com",
4
+ "expirationDate": 1757961628.043392,
5
+ "hostOnly": false,
6
+ "httpOnly": true,
7
+ "name": "VISITOR_PRIVACY_METADATA",
8
+ "partitionKey": {
9
+ "hasCrossSiteAncestor": false,
10
+ "topLevelSite": "https://youtube.com"
11
+ },
12
+ "path": "/",
13
+
14
+ "secure": true,
15
+ "session": false,
16
+ "storeId": null,
17
+ "value": "CgJJThIEGgAgNg%3D%3D"
18
+ },
19
+ {
20
+ "domain": ".youtube.com",
21
+ "expirationDate": 1774521424.380226,
22
+ "hostOnly": false,
23
+ "httpOnly": true,
24
+ "name": "__Secure-3PSID",
25
+ "path": "/",
26
+
27
+ "secure": true,
28
+ "session": false,
29
+ "storeId": null,
30
+ "value": "g.a000twhZR7dQqFhJ3z_aLQ07b29sG3V_8ccEOv1Hq2IhIAm7Uit0bjO8LrUN6vwD7u7rv76R9QACgYKAbsSARcSFQHGX2Mi2eWvoVKms26tvfXW98zPPRoVAUF8yKqBO1taDNNdgBiEJhMbMbe80076"
31
+ },
32
+ {
33
+ "domain": ".youtube.com",
34
+ "expirationDate": 1773945627.990071,
35
+ "hostOnly": false,
36
+ "httpOnly": true,
37
+ "name": "__Secure-1PSIDTS",
38
+ "path": "/",
39
+
40
+ "secure": true,
41
+ "session": false,
42
+ "storeId": null,
43
+ "value": "sidts-CjIB7pHptcqHEzMZ-jNVXoje9Sx3UJ5TKDHiU_l_Ij0iGv6PG04HJnjpucaqhyPXzF_U_xAA"
44
+ },
45
+ {
46
+ "domain": ".youtube.com",
47
+ "expirationDate": 1774521424.379595,
48
+ "hostOnly": false,
49
+ "httpOnly": false,
50
+ "name": "SAPISID",
51
+ "path": "/",
52
+
53
+ "secure": true,
54
+ "session": false,
55
+ "storeId": null,
56
+ "value": "Y7fUVZGIEqnOqMmy/Aq_7cf-ccf32hbDsE"
57
+ },
58
+ {
59
+ "domain": ".youtube.com",
60
+ "expirationDate": 1773945629.043497,
61
+ "hostOnly": false,
62
+ "httpOnly": true,
63
+ "name": "__Secure-1PSIDCC",
64
+ "path": "/",
65
+
66
+ "secure": true,
67
+ "session": false,
68
+ "storeId": null,
69
+ "value": "AKEyXzXMXCsexRu3e59B6phQixvRHPn3CbHj2OQGs2WIqAa_UpM6uwjekMM--_HYtKShn48Ueks"
70
+ },
71
+ {
72
+ "domain": ".youtube.com",
73
+ "expirationDate": 1774521424.379462,
74
+ "hostOnly": false,
75
+ "httpOnly": true,
76
+ "name": "SSID",
77
+ "path": "/",
78
+
79
+ "secure": true,
80
+ "session": false,
81
+ "storeId": null,
82
+ "value": "A_CS9fYTsJ5rfI_xy"
83
+ },
84
+ {
85
+ "domain": ".youtube.com",
86
+ "expirationDate": 1774521424.37969,
87
+ "hostOnly": false,
88
+ "httpOnly": false,
89
+ "name": "__Secure-1PAPISID",
90
+ "path": "/",
91
+
92
+ "secure": true,
93
+ "session": false,
94
+ "storeId": null,
95
+ "value": "Y7fUVZGIEqnOqMmy/Aq_7cf-ccf32hbDsE"
96
+ },
97
+ {
98
+ "domain": ".youtube.com",
99
+ "expirationDate": 1774521424.380155,
100
+ "hostOnly": false,
101
+ "httpOnly": true,
102
+ "name": "__Secure-1PSID",
103
+ "path": "/",
104
+
105
+ "secure": true,
106
+ "session": false,
107
+ "storeId": null,
108
+ "value": "g.a000twhZR7dQqFhJ3z_aLQ07b29sG3V_8ccEOv1Hq2IhIAm7Uit0c0baZdrPI2W1_IhZANaM3AACgYKAboSARcSFQHGX2MiSTJOAW2JVCWyx-NGCZW1LxoVAUF8yKqLbHRTnVn4upxweNCvSi-y0076"
109
+ },
110
+ {
111
+ "domain": ".youtube.com",
112
+ "expirationDate": 1774521424.379729,
113
+ "hostOnly": false,
114
+ "httpOnly": false,
115
+ "name": "__Secure-3PAPISID",
116
+ "path": "/",
117
+
118
+ "secure": true,
119
+ "session": false,
120
+ "storeId": null,
121
+ "value": "Y7fUVZGIEqnOqMmy/Aq_7cf-ccf32hbDsE"
122
+ },
123
+ {
124
+ "domain": ".youtube.com",
125
+ "expirationDate": 1773945629.043551,
126
+ "hostOnly": false,
127
+ "httpOnly": true,
128
+ "name": "__Secure-3PSIDCC",
129
+ "path": "/",
130
+
131
+ "secure": true,
132
+ "session": false,
133
+ "storeId": null,
134
+ "value": "AKEyXzUEoHLmaHYcVNt0mdkzTQKTs7Jkk2uiDfMqV3yPNrpw5Yhbj_5lpidsrgfx7_Ok8oRguw"
135
+ },
136
+ {
137
+ "domain": ".youtube.com",
138
+ "expirationDate": 1773945627.990268,
139
+ "hostOnly": false,
140
+ "httpOnly": true,
141
+ "name": "__Secure-3PSIDTS",
142
+ "path": "/",
143
+
144
+ "secure": true,
145
+ "session": false,
146
+ "storeId": null,
147
+ "value": "sidts-CjIB7pHptcqHEzMZ-jNVXoje9Sx3UJ5TKDHiU_l_Ij0iGv6PG04HJnjpucaqhyPXzF_U_xAA"
148
+ },
149
+ {
150
+ "domain": ".youtube.com",
151
+ "expirationDate": 1774191531.70517,
152
+ "hostOnly": false,
153
+ "httpOnly": true,
154
+ "name": "LOGIN_INFO",
155
+ "path": "/",
156
+
157
+ "secure": true,
158
+ "session": false,
159
+ "storeId": null,
160
+ "value": "AFmmF2swRQIhAPS96kgdUqxyRl5-iWxQZt7tFovLO6MFyDMgeIuf-ZD6AiBPPTZRve9TuGUzC60DGsilisirR3lnlaZB9xrnOcnE3A:QUQ3MjNmdzB3OV9KcEVaaG9NNHpIM3d2UnNFNFpERGplZmFKOHpQNVJTT1lmaS0zUEVubVdhZjRWZlM5Z0xyejJWdldhNmdYVkpDdHZRdDlTNk1FR283TU91OUF1dDBmcGIwUjFZRU1TbHZidVdrNlVLamFNYng1QUJXdzF0blNjeXkwWXVmVXBleVcyVnp4cDhLYUlHdkExY0k4VUNZcmxn"
161
+ },
162
+ {
163
+ "domain": ".youtube.com",
164
+ "expirationDate": 1776969625.522401,
165
+ "hostOnly": false,
166
+ "httpOnly": false,
167
+ "name": "PREF",
168
+ "path": "/",
169
+
170
+ "secure": true,
171
+ "session": false,
172
+ "storeId": null,
173
+ "value": "f7=4100&tz=Asia.Calcutta&f4=4000000"
174
+ },
175
+ {
176
+ "domain": ".youtube.com",
177
+ "expirationDate": 1757961628.043263,
178
+ "hostOnly": false,
179
+ "httpOnly": true,
180
+ "name": "VISITOR_INFO1_LIVE",
181
+ "partitionKey": {
182
+ "hasCrossSiteAncestor": false,
183
+ "topLevelSite": "https://youtube.com"
184
+ },
185
+ "path": "/",
186
+
187
+ "secure": true,
188
+ "session": false,
189
+ "storeId": null,
190
+ "value": "kA9JyZsoclQ"
191
+ }
192
+ ]
requirements.txt ADDED
Binary file (282 Bytes). View file
 
selinium.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # General imports
2
+ import json
3
+ import threading
4
+ import time
5
+ import undetected_chromedriver as uc
6
+ from selenium import webdriver
7
+
8
+ from selenium.webdriver.common.by import By
9
+ # Chrome driver setup
10
+ from selenium.webdriver.chrome.service import Service
11
+ from webdriver_manager.chrome import ChromeDriverManager
12
+
13
+ # Firefox driver setup
14
+ from selenium.webdriver.firefox.service import Service as FirefoxService
15
+ from webdriver_manager.firefox import GeckoDriverManager
16
+
17
+ # Selenium options for headless mode
18
+ from selenium.webdriver.chrome.options import Options
19
+
20
+ # Import important variables (make sure you have this file or define them manually)
21
+ import ImportantVariables as imp_val
22
+
23
+ # Imp variables
24
+ user_data_directory = imp_val.new_user_data_directory
25
+ profile_directory = "Default"
26
+ driver = None
27
+
28
+
29
+ def is_logged_in_youtube(driver):
30
+ try:
31
+ # Open YouTube
32
+ driver.get("https://www.youtube.com")
33
+
34
+ # Check for the profile icon (appears only when logged in)
35
+ driver.find_element(By.XPATH, "//button[@id='avatar-btn']")
36
+ print("✅ You are already logged in to YouTube!")
37
+ return True
38
+ except:
39
+ print("❌ You are NOT logged in to YouTube.")
40
+ return False
41
+
42
+
43
+ def save_cookies_json(driver, filename="cookies.json"):
44
+ if driver is None:
45
+ print("Driver is not initialized. Skipping cookie save.")
46
+ return
47
+ cookies = driver.get_cookies()
48
+
49
+ with open(filename, "w") as file:
50
+ json.dump(cookies, file, indent=4)
51
+
52
+
53
+
54
+ def get_youtube_cookies(driver):
55
+ driver.get("https://www.youtube.com/account")
56
+ driver.implicitly_wait(5)
57
+ save_cookies_json(driver, imp_val.save_cookies_json)
58
+ driver.save_screenshot(imp_val.screenshot_path + '3_1.png')
59
+
60
+ print("cookies")
61
+
62
+ def load_cookies(driver):
63
+ try:
64
+ with open(imp_val.cookies_file, "r") as file:
65
+ cookies = json.load(file)
66
+ for cookie in cookies:
67
+ driver.add_cookie(cookie)
68
+ print("Cookies loaded successfully!")
69
+
70
+ # Refresh to apply cookies
71
+ driver.refresh()
72
+ time.sleep(5) # Wait for the page to reload with cookies
73
+
74
+ except Exception as e:
75
+ print(f"Error loading cookies: {e}")
76
+
77
+ def cookie_capture_loop():
78
+ """Runs in a separate thread to capture cookies every 5 minutes."""
79
+ global driver
80
+
81
+ while True:
82
+ time.sleep(300) # Wait for 5 minutes
83
+ save_cookies_json(driver, imp_val.save_cookies_json)
84
+ print("Cookies saved to JSON file.")
85
+
86
+ # Refresh browser to keep session active
87
+ if driver is not None:
88
+ try:
89
+ driver.refresh()
90
+ print("🔄 Browser refreshed successfully!")
91
+ except Exception as e:
92
+ print(f"⚠️ Error refreshing browser: {e}")
93
+
94
+ def setup_browser(browser="chrome"):
95
+ """ Set up the browser (Chrome or Firefox) for automation """
96
+ global driver # Ensure we use the same instance
97
+
98
+ if driver is not None:
99
+ print("Browser is already running. Returning existing driver instance.")
100
+ return driver
101
+
102
+ # Chrome options
103
+ chrome_options = uc.ChromeOptions()
104
+
105
+ if user_data_directory:
106
+ chrome_options.add_argument(f'--user-data-dir={user_data_directory}')
107
+ if profile_directory:
108
+ chrome_options.add_argument(f'--profile-directory={profile_directory}')
109
+
110
+ # Additional options for stability
111
+ chrome_options.add_argument("--no-sandbox")
112
+ chrome_options.add_argument("--disable-gpu")
113
+
114
+ chrome_options.add_argument("--disable-blink-features=AutomationControlled")
115
+ chrome_options.add_argument('--headless') # Run in headless mode
116
+ chrome_options.add_argument('--no-sandbox') # Recommended for CI environments
117
+ chrome_options.add_argument('--disable-dev-shm-usage') # Avoid /dev/shm issues in CI
118
+ chrome_options.add_argument('--disable-gpu') # Optional: Disable GPU usage
119
+ chrome_options.add_argument('--window-size=1920,1080') # Ensure proper resolution
120
+
121
+
122
+ print(f"\nSetting up: {browser} driver")
123
+
124
+ if browser == "chrome":
125
+ print("Using ChromeDriverManager:", ChromeDriverManager().install())
126
+ driver = uc.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
127
+
128
+ elif browser == "firefox":
129
+ print("Using GeckoDriverManager:", GeckoDriverManager().install())
130
+ driver = webdriver.Firefox(service=FirefoxService(GeckoDriverManager().install()))
131
+
132
+ else:
133
+ raise ValueError("Unsupported browser. Use 'chrome' or 'firefox'.")
134
+
135
+ driver.implicitly_wait(10) # Set implicit wait
136
+ return driver
137
+
138
+
139
+ def main():
140
+ """ Main function to run browser automation """
141
+
142
+ # browser_choice = input("Enter browser (chrome/firefox): ").strip().lower()
143
+ global driver
144
+ browser_choice = "chrome"
145
+
146
+
147
+ try:
148
+ driver = setup_browser(browser_choice)
149
+
150
+ # Example: Open YouTube
151
+ driver.get("https://www.youtube.com")
152
+ print("Opened YouTube successfully!")
153
+
154
+ time.sleep(5) # Keep browser open for demonstration
155
+ load_cookies(driver)
156
+ time.sleep(5)
157
+ driver.save_screenshot(imp_val.screenshot_path + '3_1.png')
158
+ is_logged_in_youtube(driver)
159
+
160
+ # Start cookie capture thread
161
+ cookie_thread = threading.Thread(target=cookie_capture_loop, daemon=True)
162
+ cookie_thread.start()
163
+
164
+ print("Driver is running. Cookie capture thread started.")
165
+
166
+ # Keep the browser open indefinitely
167
+ while True:
168
+ time.sleep(1)
169
+
170
+
171
+
172
+
173
+ except Exception as e:
174
+ print(f"Error: {e}")
175
+
176
+ finally:
177
+ print("\nClosing browser...")
178
+ # driver.quit()
179
+
180
+
181
+ if __name__ == "__main__":
182
+ main()