-
Notifications
You must be signed in to change notification settings - Fork 7
/
export_links.py
175 lines (134 loc) · 5.1 KB
/
export_links.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
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
import csv
import json
import re
import requests
import signal
import sys
from getpass import getpass
from lxml import html
BASE_URL = "https://news.ycombinator.com"
def signal_handler(signal, frame):
print("\nStopping...\n")
sys.exit(130)
def save_json(saved_links, file_name):
result = {"total": len(saved_links), "links": saved_links}
with open(file_name, "w") as f:
json.dump(result, f)
print(f"\nLinks saved to: {file_name}")
def save_csv(saved_links, file_name):
data = [
(
link["number"],
link["title"],
link["url"],
link["points"],
link["comments"],
link["comments_url"],
link["author"],
link["age"],
)
for link in saved_links
]
with open(file_name, "w") as f:
writer = csv.writer(f, quoting=csv.QUOTE_NONNUMERIC, delimiter=",")
writer.writerows(data)
print(f"\nLinks saved to: {file_name}")
def main():
# Gracefully handle KeyboardInterrupt (Ctrl + C)
signal.signal(signal.SIGINT, signal_handler)
session = requests.Session()
print("Enter your HN account details:")
username = input("Username: ")
password = getpass()
try:
print("\nLogging in...")
r = session.post(f"{BASE_URL}/login", data={"acct": username, "pw": password})
if session.cookies.get("user", None) is None:
print("Error logging in. Verify the credentials and try again.")
sys.exit(1)
print("Logged in successfully.\n")
except:
print("Error logging in.\n")
sys.exit(1)
url = f"{BASE_URL}/upvoted?id={username}&p="
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:75.0) Gecko/20100101 Firefox/75.0",
}
saved_links = list()
links_processed = 0
i = 1
while True:
try:
print("Getting url: ", url + str(i))
r = session.get(url + str(i), headers=headers)
tree = html.fromstring(r.text)
# Part that contains the title and url for the stories
tree_title = tree.cssselect(".title")
# Part that contains metadata such as author, no. of comments, etc.
tree_subtext = tree.cssselect(".subtext")
# Part that contains score
tree_score = tree.cssselect(".subtext span.score")
# Number of links on the page
n = len(tree_score)
if n == 0:
print(f"Processing page {i}. No links found.")
break
print(f"Processing page {i}. Number of links found: {n}")
for j in range(n):
tree_subtext_each = tree_subtext[j].cssselect("a")
tree_title_each = tree_title[2 * j + 1].cssselect("a")
link_dict = {
"number": int(tree_title[2 * j].text_content()[:-1]),
"title": tree_title_each[0].text_content(),
"url": tree_title_each[0].values()[0],
"points": int(tree_score[j].text_content().split()[0]),
"author": tree_subtext_each[0].text_content(),
"age": tree_subtext_each[1].text_content(),
}
# Use correct URL for stories without links (e.g., Ask HN, Show HN, etc.)
if link_dict["url"].startswith("item?"):
link_dict["url"] = f"{BASE_URL}/{link_dict['url']}"
# This is to take care of situations where flag link may not be
# present in the subtext. So number of links could be either 3
# or 4.
num_subtext = len(tree_subtext_each)
link_dict["comments_url"] = (
BASE_URL + tree_subtext_each[num_subtext - 1].values()[0]
)
if (
tree_subtext_each[num_subtext - 1].text_content().strip()
== "discuss"
):
link_dict["comments"] = 0
else:
link_dict["comments"] = int(
tree_subtext_each[num_subtext - 1].text_content().split()[0]
)
saved_links.append(link_dict)
links_processed += 1
if n < 30:
break
except:
print(f"Error getting data for page {i}")
break
i += 1
if links_processed < 1:
print(
"Could not retrieve any of the links. Check if you actually have any saved links."
)
sys.exit(1)
else:
print(f"Processed {links_processed} links")
print(
"\nEnter the file name in the next line. Use extension '.json' for JSON, or '.csv' for CSV."
)
file_name = input("File name (default: links.json): ")
if file_name == "":
file_name = "links.json"
if file_name.split(".")[-1].lower() == "json":
save_json(saved_links, file_name)
elif file_name.split(".")[-1].lower() == "csv":
save_csv(saved_links, file_name)
if __name__ == "__main__":
main()