-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
aur.py
103 lines (83 loc) · 3.69 KB
/
aur.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
import os
import re
import requests
import pickle
class PackageInfo:
def __init__(self, info):
self.name = info['Name']
self.pkgbase = info['PackageBase']
self.last_modified = info['LastModified']
self.out_of_date = info['OutOfDate']
def __repr__(self):
return f'<PackageInfo {self.name}>'
class AUR:
base_url = 'https://aur.archlinux.org'
comment_id_re = re.compile(r'<a href="#comment-([^"]*)"')
def __init__(self, username, password, cookies=None):
self.username = username
self.password = password
self.session = requests.session()
self.cookies_file = cookies
if not self.cookies_file is None and os.path.exists(self.cookies_file):
with open(self.cookies_file, 'rb') as f:
self.session.cookies.update(pickle.load(f))
def login(self):
url = self.base_url + '/login'
data = {
'user': self.username,
'passwd': self.password,
'referer': AUR.base_url,
'next': "/account/" + self.username,
'remember_me': 'on'
}
headers = {'Referer': AUR.base_url + '/login?next=/account/' + self.username}
response = self.session.post(url, data=data, headers=headers, allow_redirects=False)
assert response.status_code == 303
if not self.cookies_file is None:
with open(self.cookies_file, 'wb') as f:
pickle.dump(self.session.cookies, f)
def get_profile(self):
url = '/'.join([AUR.base_url, 'account', self.username])
response = self.session.get(url)
if response.status_code != 200:
self.login()
response = self.session.get(url)
assert response.status_code == 200
return response.text
def search(self, by, keyword):
url = '/'.join([AUR.base_url, 'rpc', 'v5', 'search', keyword])
params = {'by': by}
response = self.session.get(url, params=params)
assert response.status_code == 200
return [PackageInfo(i) for i in response.json()['results']]
def flag(self, package, comment):
pass
def unflag(self, package, comment):
pass
def comment(self, pkgbase, comment):
url = '/'.join([AUR.base_url, 'pkgbase', pkgbase, 'comments'])
data = {'comment': comment}
response = self.session.post(url, data=data, allow_redirects=False)
assert response.status_code == 303
def get_latest_comment_id(self, pkgbase, username=None):
url = '/'.join([AUR.base_url, 'pkgbase', pkgbase])
response = self.session.get(url)
assert response.status_code == 200
username = self.username if username is None else username
lines = response.text.split('\n')
line = [line for line in lines if username in line and '#comment-' in line][0]
comment_id = AUR.comment_id_re.search(line).group(1)
return comment_id
def update_comment(self, pkgbase, comment_id, comment):
url = '/'.join([AUR.base_url, 'pkgbase', pkgbase, 'comments', comment_id])
data = {'comment': comment}
response = self.session.post(url, data=data, allow_redirects=False)
assert response.status_code == 303
def pin_comment(self, pkgbase, comment_id):
url = '/'.join([AUR.base_url, 'pkgbase', pkgbase, 'comments', comment_id, 'pin'])
response = self.session.post(url, allow_redirects=False)
assert response.status_code == 303
def unpin_comment(self, pkgbase, comment_id):
url = '/'.join([AUR.base_url, 'pkgbase', pkgbase, 'comments', comment_id, 'unpin'])
response = self.session.post(url, allow_redirects=False)
assert response.status_code == 303