-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaur-vote
executable file
·162 lines (116 loc) · 4.77 KB
/
aur-vote
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
#!/usr/bin/env python
import argparse
import bs4
import collections
import getpass
import os
import pickle
import requests
import sys
LOGIN_URL = "https://aur.archlinux.org/#/"
PACKAGES_URL = "https://aur.archlinux.org/packages/"
SEARCH_URL_TEMPLATE = "https://aur.archlinux.org/packages/?O=%d&SO=d&PP=250&do_Search=Go&SB=w"
PACKAGE_URL_TEMPLATE = "https://aur.archlinux.org/packages/%s/"
VOTE_URL_TEMPLATE = "https://aur.archlinux.org/pkgbase/%s/vote/"
UNVOTE_URL_TEMPLATE = "https://aur.archlinux.org/pkgbase/%s/unvote/"
DATA_PATH = os.getenv("XDG_DATA_HOME", os.path.expanduser("~/.local/share")) + "/aurvote-utils"
COOKIE_PATH = f"{DATA_PATH}/cookie"
Package = collections.namedtuple("Package", ("name", "version", "votes", "popularity", "voted", "notify", "description", "maintainer"))
class CustomHelpFormatter(argparse.HelpFormatter):
""" Used to customize multiparameter metavars """
def _format_args(self, action, default_metavar):
return "<%s>..." % self._metavar_formatter(action, default_metavar)(1)
def decode(response):
""" Parse an HTML response string """
return bs4.BeautifulSoup(response.text, "html.parser")
def load_cookie(session, path):
""" Load a cookie jar
Returns:
True if the cookie exists and is accepted, False otherwise
"""
if not os.path.isfile(path):
return False
with open(path, "rb") as file:
session.cookies.update(pickle.load(file))
return is_logged_in(decode(session.get(PACKAGES_URL)))
def dump_cookie(session, path):
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "wb") as file:
pickle.dump(session.cookies, file)
def login(session, username, password):
""" Login to the AUR
Returns:
True if the login was successful, False otherwise
"""
soup = decode(session.post(LOGIN_URL, {
"user": username,
"passwd": password,
"remember_me": "on",
}))
return is_logged_in(soup)
def is_logged_in(soup):
return soup.select_one("a[href=\"/logout/\"]") != None
def get_token(session):
soup = decode(session.get(PACKAGES_URL))
return soup.select_one("input[name=token]")["value"]
def get_voted(session):
""" Get the voted packages
Returns:
A tuple of Package objects
"""
offset = 0
while True:
# Load the search page and parse the rows in the results table
rows = decode(session.get(SEARCH_URL_TEMPLATE % offset)).select("tbody > tr")
# In case we reach the last page
if not rows:
return
for row in rows:
pkg = Package(*(column.get_text(strip=True) for column in row.find_all("td")[1:]))
if not pkg.voted:
return
yield pkg
offset += 250
def get_base(session, pkg):
soup = decode(session.get(PACKAGE_URL_TEMPLATE % pkg))
return soup.select_one("#pkginfo > tr:nth-of-type(2) a").get_text()
def vote(session, token, pkg):
response = session.post(VOTE_URL_TEMPLATE % get_base(session, pkg), {
"token": token, "do_Vote": pkg
}, allow_redirects=False)
return response.status_code == requests.codes.found
def unvote(session, token, pkg):
response = session.post(UNVOTE_URL_TEMPLATE % get_base(session, pkg), {
"token": token, "do_UnVote": pkg
}, allow_redirects=False)
return response.status_code == requests.codes.found
def main():
parser = argparse.ArgumentParser(usage="%(prog)s [-h | -a | -l | (-v | -u) <pkg>...]", formatter_class=CustomHelpFormatter)
parser.add_argument("-a", "--login", action="store_true", help="login to the AUR")
parser.add_argument("-l", "--list", action="store_true", help="list the voted packages (default)")
parser.add_argument("-v", "--vote", nargs="+", metavar="pkg", help="vote for package(s)")
parser.add_argument("-u", "--unvote", nargs="+", metavar="pkg", help="unvote package(s)")
if len(sys.argv) > 1:
args = parser.parse_args()
session = requests.Session()
if not load_cookie(session, COOKIE_PATH):
if not login(session, input("Username: "), getpass.getpass()):
parser.error("unable to login")
dump_cookie(session, COOKIE_PATH)
if args.list:
for pkg in sorted(get_voted(session)):
print(pkg.name)
elif args.vote != None:
token = get_token(session)
for pkg in args.vote:
if not vote(session, token, pkg):
print("unable to vote %s" % pkg)
elif args.unvote != None:
token = get_token(session)
for pkg in args.unvote:
if not unvote(session, token, pkg):
print("unable to unvote %s" % pkg)
else:
parser.print_help()
if __name__ == "__main__":
main()