-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurls.pyx
140 lines (114 loc) · 3.88 KB
/
urls.pyx
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import os
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.action_chains import ActionChains
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument("window-size=1200x600")
options.add_argument('--disable-web-security')
options.add_argument('--allow-running-insecure-content')
options.add_argument('--allow-cross-origin-auth-prompt')
def jscode(int iteration):
return f"""
function saveToFile(content, filename) {{
const blob = new Blob([content], {{ type: 'application/json' }});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}}
function getToken() {{
const scripts = document.querySelectorAll('script');
for (const script of scripts) {{
const scriptContent = script.textContent;
const tokenMatch = scriptContent.match(/"_token":"(.*?)"/);
if (tokenMatch) {{
return tokenMatch[1];
}}
}}
return null;
}}
function fetchData(nextValue, callback, errorCallback) {{
const token = getToken();
if (!token) {{
errorCallback('Token not found');
return;
}}
const xhr = new XMLHttpRequest();
xhr.open("POST", "https://www.alinea.id/more", true);
xhr.setRequestHeader("accept", "text/html, */*; q=0.01");
xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.setRequestHeader("x-requested-with", "XMLHttpRequest");
xhr.onload = function () {{
if (xhr.status >= 200 && xhr.status < 300) {{
callback(xhr.responseText);
}} else {{
errorCallback(`HTTP error: ${{xhr.status}}`);
}}
}};
xhr.onerror = function () {{
errorCallback("Network error");
}};
const body = new URLSearchParams({{
"_token": token,
"next": nextValue,
"categorynext": ""
}}).toString();
xhr.send(body);
}}
function extractUrls(text) {{
const urlPattern = /https:\\/\\/www\\.alinea\\.id\\/peristiwa\\/[^\s'"]+/g;
return text.match(urlPattern) || [];
}}
function fetchMultiplePages(iteration, filename) {{
const uniqueUrls = new Set();
let completedRequests = 0;
for (let i = 1; i <= iteration; i++) {{
const nextValue = (i * 10).toString();
console.log(`Fetching URLs [${{nextValue}}]`);
fetchData(nextValue,
(text) => {{
const urls = extractUrls(text);
urls.forEach(url => uniqueUrls.add(url));
console.log(`URLs found: ${{uniqueUrls.size}}`);
completedRequests++;
if (completedRequests === iteration) {{
finalize();
}}
}},
(error) => {{
console.error(`Error fetching data for next=${{nextValue}}:`, error);
completedRequests++;
if (completedRequests === iteration) {{
finalize();
}}
}}
);
}}
function finalize() {{
if (uniqueUrls.size > 0) {{
const content = {{ links: Array.from(uniqueUrls) }};
const jsonString = JSON.stringify(content, null, 2);
saveToFile(jsonString, filename);
}} else {{
console.log('No URLs found');
}}
}}
}}
const iteration = {iteration};
const filename = "links";
fetchMultiplePages(iteration, filename);
"""
def save_urls(int iteration):
browser = webdriver.Chrome(options = options)
browser.get("https://www.alinea.id/")
browser.execute_script(jscode(iteration))
while True:
if os.path.exists("links.json"): break
else: time.sleep(1)