-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiscrap.py
132 lines (93 loc) · 3.04 KB
/
iscrap.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
"""
Iscrap | Scrap archlinux.org/news before upgrading
---
v0.0.0+
"""
import argparse
import re
import bs4
import requests
class Soupers:
"""
Collect of soup-related functions.
"""
def __init__(self):
"""
Get requests.
"""
self.res = requests.get('https://www.archlinux.org/news/')
def return_arch_soup(self):
"""
Returns main arch soup.
"""
res = self.res
res.raise_for_status()
arch_soup = bs4.BeautifulSoup(res.text, 'lxml')
return arch_soup
def fetch_func(number):
'''
Parse archlinux.org/news and print news titles.
'''
number = int(number)
soups = Soupers()
arch_soup = soups.return_arch_soup()
soup = arch_soup.find_all('td', attrs={'class': 'wrap'})
print('\n\n')
for position, news in enumerate(soup[:number], start=1):
print('{} - {}'.format(position, news.getText()))
print('\n\n')
def read_func(number):
'''
Print the new choosed by the user.
'''
number = int(number) - 1
soups = Soupers()
arch_soup = soups.return_arch_soup()
all_news = []
for a_new in enumerate(arch_soup.find('tbody').find_all('a', href=True)):
href = re.search(r'(href=")(?P<href>.+)("\s)', str(a_new))
all_news.append(href[2])
the_new = all_news[number]
res = requests.get('https://www.archlinux.org/' + the_new)
res.raise_for_status()
newsoup = bs4.BeautifulSoup(res.text, 'lxml')
header_soup = newsoup.find('h2').getText()
soup = newsoup.find('div', attrs={'class': 'article-content'}).getText()
# find_all('p')
print('\n\n\t\t\t', header_soup, '\n\n')
print(soup.ljust(1), '\n')
def parse_arguments():
"""
Parse and return arguments.
"""
parser = argparse.ArgumentParser("cliclock [COMMAND] [ARGUMENTS]")
parser.add_argument("--version", "-v", action="version", version="v0.0.0+")
subparsers = parser.add_subparsers(help="Types of commands")
fetch_parser = subparsers.add_parser("fetch", help="Shows the title of the \
3 latest news")
fetch_parser.add_argument("number", type=int, help="An integer of how many \
news' titles will be fetched.")
fetch_parser.set_defaults(func=fetch_func)
read_parser = subparsers.add_parser("read", help="Shows the title of the 3 \
latest news")
read_parser.add_argument("number", type=int, help="An integer representing \
which new will be read.")
read_parser.set_defaults(func=read_func)
return vars(parser.parse_args())
def main():
'''
Argparser organizer.
'''
args_dict = parse_arguments()
if re.search("fetch_func", str(args_dict["func"])):
number = str(args_dict["number"])
fetch_func(number)
elif re.search("read_func", str(args_dict["func"])):
number = str(args_dict["number"])
read_func(number)
elif re.search("version", str(args_dict["func"])):
print(args_dict)
else:
print("Ooops!")
if __name__ == '__main__':
main()