-
Notifications
You must be signed in to change notification settings - Fork 0
/
pull_links_and_redirects.py
165 lines (139 loc) · 5.76 KB
/
pull_links_and_redirects.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from threading import Thread
import requests
import queue
import csv
import time
import logging
logging.basicConfig(filename="pull_links_and_redirects_log.txt", level=logging.DEBUG)
pageids_file = open("pull_nonredirect_pages_out.txt", newline="\n", encoding="utf-8")
csv_reader = csv.reader(pageids_file, delimiter=',')
link_file = open("pull_links_out.txt", "w", newline="\n", encoding="utf-8")
link_writer = csv.writer(link_file, delimiter=',')
redirect_file = open("pull_redirects_out.txt", "w", newline="\n", encoding="utf-8")
redirect_writer = csv.writer(redirect_file, delimiter=',')
q = queue.Queue()
printed_link_success_count = 0
printed_link_fail_count = 0
printed_redirect_success_count = 0
printed_redirect_fail_count = 0
def print_into_file():
while True:
page_links = q.get()
pageid = page_links[0]
links = page_links[1]
actual_links = [link for link in links if not link[2]]
redirects = [link for link in links if link[2]]
if len(actual_links) != 0:
print_links(pageid, actual_links)
if len(redirects) != 0:
print_redirects(pageid, redirects)
q.task_done()
def print_links(pageid, links):
global printed_link_success_count, printed_link_fail_count
while True:
try:
link_text = ",".join([str(link[0]) for link in links])
link_writer.writerow([pageid, link_text])
link_file.flush()
print()
log_debug_message("Successfully printed ({}) links for: {}".format(len(links), pageid))
printed_link_success_count += len(links)
break
except:
log_error_message("Failed to print ({}) links for {}".format(len(links), pageid))
printed_link_fail_count += len(links)
time.sleep(5)
continue
log_debug_message("# of links succesfully printed: " + str(printed_link_success_count))
log_debug_message("# of links failed to print: " + str(printed_link_fail_count))
def print_redirects(pageid, redirects):
global printed_redirect_success_count, printed_redirect_fail_count
while True:
try:
redirect_ids = [str(redirect[0]) for redirect in redirects]
redirect_text = ",".join(redirect_ids)
redirect_writer.writerow([pageid, redirect_text])
redirect_file.flush()
print()
log_debug_message("Successfully printed ({}) redirects for: {}".format(len(redirects), pageid))
printed_redirect_success_count += len(redirects)
break
except:
print()
log_error_message("Failed to print ({}) redirects for {}".format(len(redirects), pageid))
printed_redirect_fail_count += len(redirects)
time.sleep(5)
continue
log_debug_message("# of redirects succesfully printed: " + str(printed_redirect_success_count))
log_debug_message("# of redirects failed to print: " + str(printed_redirect_fail_count))
def request_pages(pageid, lhcontinue=""):
# https://en.wikipedia.org/w/api.php?action=query&format=json&
# prop=linkshere&pageids=2327951&lhprop=pageid%7Ctitle%7Credirect
# &lhnamespace=0&lhlimit=max
base_url = "https://en.wikipedia.org/w/api.php"
payload = {
"action": "query",
"format": "json",
"prop": "linkshere",
"pageids": pageid,
"lhprop": "pageid|title|redirect",
"lhnamespace" : "0",
"lhlimit" : "max"
}
if lhcontinue != "":
payload["lhcontinue"] = lhcontinue
response = requests.get(base_url, params=payload)
return response
def log_error_message(message):
logging.exception(message)
print(message)
def log_debug_message(message):
logging.debug(message)
print(message)
for i in range(1):
worker = Thread(target=print_into_file, args=())
worker.setDaemon(True)
worker.start()
succesful_request_count = 0
failed_request_count = 0
retrieved_link_count = 0
for row in csv_reader:
curr_pageid = row[0]
print("Reading: ({}) {}".format(curr_pageid, row[1]))
lhcontinue = ""
running_link_list = []
while True:
identifier = curr_pageid
if lhcontinue != "":
identifier += "|{}".format(lhcontinue)
try:
response = request_pages(pageid=curr_pageid, lhcontinue=lhcontinue)
except:
log_error_message("Failed request for " + identifier)
failed_request_count += 1
time.sleep(5)
continue
if not response or response.status_code != requests.codes.ok:
log_error_message("Unsuccessful response retrieved for " + identifier)
failed_request_count += 1
time.sleep(5)
continue
print()
log_debug_message("Successfully obtained response for " + identifier)
succesful_request_count += 1
log_debug_message("# of successful requests: " + str(succesful_request_count))
log_debug_message("# of failed requests: " + str(failed_request_count))
json = response.json()
linkshere = json["query"]["pages"][curr_pageid]
linkshere = linkshere["linkshere"] if "linkshere" in linkshere else []
retrieved_link_count += len(linkshere)
log_debug_message("# of links retrieved: " + str(retrieved_link_count))
new_links = [(link["pageid"], link["title"], "redirect" in link) for link in linkshere]
running_link_list.extend(new_links)
if "continue" in json and "lhcontinue" in json["continue"]:
lhcontinue = json["continue"]["lhcontinue"]
else:
q.put((curr_pageid, running_link_list))
break
q.join() # Wait until the queue is done being processed.
print("Program has successfully completed.")