View difference between Paste ID: GC1Qkywa and WTPewVqX
SHOW: | | - or go back to the newest paste.
1-
# Disclaimer: 
1+
# Disclaimer: I don't write much Python, and this code is pretty messy
2
3
# setup: make sure you have a SQLite3 database named comments.db, and run
4
# `CREATE TABLE comments(id varchar(10) PRIMARY KEY NOT NULL);` to create the
5
# table.
6
7
import aur
8
import praw
9
import re
10
import sqlite3
11
from pprint import pprint
12
13
# bot config settings
14
15
BOT_USER = 'username'
16
BOT_PASS = 'password'
17
18
SUBREDDIT = 'u6ilDKVZralqnUaYyA'
19
20
# some constant stuff
21
22
AUR_URL = 'https://aur.archlinux.org/packages/'
23
AUR_SEARCH_WIKI = 'https://wiki.archlinux.org/index.php/Special:Search?search='
24
AUR_SEARCH_URL = 'https://aur.archlinux.org/packages/?O=0&K='
25
26
# message templates
27
28
MSG_AUR_INFO = (
29
	"AUR: [%s](%s) - %s\n\n"
30
	"Upstream: %s\n\n"
31
	"[Search Arch wiki](%s)\n\n"
32
)
33
34
MSG_AUR_SEARCH = (
35
	"Most popular packages in AUR containing %s:\n\n"
36
	"%s\n"
37
	"[Full search](%s)\n\n"
38
)
39
40
MSG_AUR_SEARCH_ENTRY = "* [%s](%s) - %s\n"
41
42
MSG_FOOT = (
43
	"---\n\n"
44
	"^(/r/%s AUR link bot by /u/redered - )"
45
	"[^(message me)](https://www.reddit.com/message/compose?to=redered)"
46
	"^( about bugs, suggestions, or feedback)"
47
)
48
49
# da code
50
51
db = sqlite3.connect('comments.db')
52
dbc = db.cursor()
53
54
r = praw.Reddit('Test comment bot 0.01 - /u/redered')
55
r.login(username=BOT_USER, password=BOT_PASS)
56
sr = r.get_subreddit(SUBREDDIT)
57
comments = praw.helpers.comment_stream(r, SUBREDDIT)
58
59
for comment in comments:
60
	m = re.search(r"^linkme: (\w+)", comment.body)
61
	if m:
62
		check = dbc.execute('SELECT * FROM comments WHERE id=?', (comment.id,))
63
		if (check.fetchone() is None):
64
			msg = ""
65
			pkgname = m.groups()[0]
66
67
			try:
68
				pkg = aur.info(pkgname)
69
				name = pkg.name
70
				url = AUR_URL + name
71
				desc = pkg.description
72
				upstr = pkg.url
73
				wiki = AUR_SEARCH_WIKI + name
74
75
				msg = MSG_AUR_INFO % (name, url, desc, upstr, wiki)
76
			except:
77
				pkgs = list(aur.search(pkgname))
78
				pkgs.sort(key=lambda p: -p.num_votes)
79
				plist = ""
80
				count = 0
81
				search_url = AUR_SEARCH_URL + pkgname
82
83
				for pkg in pkgs:
84
					name = pkg.name
85
					url = AUR_URL + name
86
					desc = pkg.description
87
88
					entry = MSG_AUR_SEARCH_ENTRY % (name, url, desc)
89
					plist += entry
90
					count += 1
91
					if count >= 10:
92
						break
93
94
				msg = MSG_AUR_SEARCH % (pkgname, plist, search_url)
95
96
			comment.reply(msg + MSG_FOOT % (SUBREDDIT,))
97
			dbc.execute('INSERT INTO comments VALUES (?)', (comment.id,))
98
99
db.commit()
100
db.close()