-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathaur-auto-vote
executable file
·129 lines (99 loc) · 4.78 KB
/
aur-auto-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
#!/usr/bin/env python
import argparse
import collections
import os
import re
import subprocess
import time
import getpass
import bs4
import requests
EXIT_FAILURE = 1
LOGIN_URL = 'https://aur.archlinux.org/login'
SEARCH_URL_TEMPLATE = 'https://aur.archlinux.org/packages/?O=%d&SB=w&SO=d&PP=250&do_Search=Go'
PACKAGES_URL = 'https://aur.archlinux.org/packages/'
VOTE_URL_TEMPLATE = 'https://aur.archlinux.org/pkgbase/%s/vote/'
UNVOTE_URL_TEMPLATE = 'https://aur.archlinux.org/pkgbase/%s/unvote/'
PACKAGES_PER_PAGE = 250
argument_parser = argparse.ArgumentParser()
argument_parser.add_argument('username')
argument_parser.add_argument('--ignore', '-i', action='append', default=[],
help='Regex for packages that should not be voted. Can be passed multiple times.')
argument_parser.add_argument('--unvote-all', '-u', action='store_true',
help='Unvote all voted-for packages, all other arguments are ignored.')
argument_parser.add_argument('--no-unvote', '-n', action="store_true", help="Don't unvote uninstalled packages.")
argument_parser.add_argument('--delay', '-d', type=float, default=0, help='Delay between voting actions (seconds).')
Package = collections.namedtuple(
'Package', ('name', 'version', 'votes', 'popularity', 'voted', 'notify', 'description', 'maintainer', 'updated'))
def login(session, username, password):
response = session.post(LOGIN_URL, {'user': username, 'passwd': password, 'next': '/'}, headers={'referer': 'https://aur.archlinux.org/login'})
soup = bs4.BeautifulSoup(response.text, 'html5lib')
return bool(soup.select_one('#archdev-navbar').find('form', action=lambda h: h and h.rstrip('/').endswith('/logout')))
def get_foreign_packages():
return subprocess.check_output(('pacman', '-Qmq'), universal_newlines=True).splitlines()
def get_voted_packages(session):
offset = 0
while True:
response = session.get(SEARCH_URL_TEMPLATE % offset)
soup = bs4.BeautifulSoup(response.text, 'html5lib')
for row in soup.select('.results > tbody > tr'):
package = Package(*(c.get_text(strip=True) for c in row.select(':scope > td')[1:]))
if not package.voted:
return
yield package
offset += PACKAGES_PER_PAGE
def vote_package(session, package):
response = session.post(VOTE_URL_TEMPLATE % package, {'do_Vote': 'Vote for this package'},
allow_redirects=True)
return response.status_code == requests.codes.ok
def unvote_package(session, package):
response = session.post(UNVOTE_URL_TEMPLATE % package, {'do_UnVote': 'Remove vote'},
allow_redirects=True)
return response.status_code == requests.codes.ok
# TODO: Handle split packages better
def main(arguments):
password = os.environ.get('AUR_AUTO_VOTE_PASSWORD') or getpass.getpass('Password: ')
ignores = tuple(re.compile(r) for r in arguments.ignore)
session = requests.Session()
if not login(session, arguments.username, password):
argument_parser.exit(EXIT_FAILURE, 'Could not login.\n')
voted_packages = tuple(p.name for p in sorted(get_voted_packages(session)))
# Unvote all packages and return immediately if --unvote-all was passed
if arguments.unvote_all:
for package in voted_packages:
print('Unvoting package: %s... ' % package, end='', flush=True)
if unvote_package(session, package):
print('done.')
else:
print('failed.')
time.sleep(arguments.delay)
return
foreign_packages = set(get_foreign_packages())
voted_packages = set(voted_packages)
for package in sorted(foreign_packages.difference(voted_packages)):
if any(i.match(package) for i in ignores):
print('Not voting for ignored package:', package)
continue
print('Voting for package: %s... ' % package, end='', flush=True)
if vote_package(session, package):
print('done.')
else:
print('failed.')
time.sleep(arguments.delay)
# Unvote packages that are voted for in the AUR but not available on the system anymore, i.e. uninstalled since the
# last voting
if arguments.no_unvote:
return
for package in sorted(voted_packages.difference(foreign_packages)):
if any(i.match(package) for i in ignores):
print('Not unvoting ignored package:', package)
continue
print('Unvoting removed package: %s... ' % package, end='', flush=True)
if unvote_package(session, package):
print('done.')
else:
print('failed.')
time.sleep(arguments.delay)
if __name__ == '__main__':
arguments = argument_parser.parse_args()
main(arguments)