Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Disclaimer: I don't write much Python, and this code is pretty messy
- # setup: make sure you have a SQLite3 database named comments.db, and run
- # `CREATE TABLE comments(id varchar(10) PRIMARY KEY NOT NULL);` to create the
- # table.
- import aur
- import praw
- import re
- import sqlite3
- from pprint import pprint
- # bot config settings
- BOT_USER = 'username'
- BOT_PASS = 'password'
- SUBREDDIT = 'u6ilDKVZralqnUaYyA'
- # some constant stuff
- AUR_URL = 'https://aur.archlinux.org/packages/'
- AUR_SEARCH_WIKI = 'https://wiki.archlinux.org/index.php/Special:Search?search='
- AUR_SEARCH_URL = 'https://aur.archlinux.org/packages/?O=0&K='
- # message templates
- MSG_AUR_INFO = (
- "AUR: [%s](%s) - %s\n\n"
- "Upstream: %s\n\n"
- "[Search Arch wiki](%s)\n\n"
- )
- MSG_AUR_SEARCH = (
- "Most popular packages in AUR containing %s:\n\n"
- "%s\n"
- "[Full search](%s)\n\n"
- )
- MSG_AUR_SEARCH_ENTRY = "* [%s](%s) - %s\n"
- MSG_FOOT = (
- "---\n\n"
- "^(/r/%s AUR link bot by /u/redered - )"
- "[^(message me)](https://www.reddit.com/message/compose?to=redered)"
- "^( about bugs, suggestions, or feedback)"
- )
- # da code
- db = sqlite3.connect('comments.db')
- dbc = db.cursor()
- r = praw.Reddit('Test comment bot 0.01 - /u/redered')
- r.login(username=BOT_USER, password=BOT_PASS)
- sr = r.get_subreddit(SUBREDDIT)
- comments = praw.helpers.comment_stream(r, SUBREDDIT)
- for comment in comments:
- m = re.search(r"^linkme: (\w+)", comment.body)
- if m:
- check = dbc.execute('SELECT * FROM comments WHERE id=?', (comment.id,))
- if (check.fetchone() is None):
- msg = ""
- pkgname = m.groups()[0]
- try:
- pkg = aur.info(pkgname)
- name = pkg.name
- url = AUR_URL + name
- desc = pkg.description
- upstr = pkg.url
- wiki = AUR_SEARCH_WIKI + name
- msg = MSG_AUR_INFO % (name, url, desc, upstr, wiki)
- except:
- pkgs = list(aur.search(pkgname))
- pkgs.sort(key=lambda p: -p.num_votes)
- plist = ""
- count = 0
- search_url = AUR_SEARCH_URL + pkgname
- for pkg in pkgs:
- name = pkg.name
- url = AUR_URL + name
- desc = pkg.description
- entry = MSG_AUR_SEARCH_ENTRY % (name, url, desc)
- plist += entry
- count += 1
- if count >= 10:
- break
- msg = MSG_AUR_SEARCH % (pkgname, plist, search_url)
- comment.reply(msg + MSG_FOOT % (SUBREDDIT,))
- dbc.execute('INSERT INTO comments VALUES (?)', (comment.id,))
- db.commit()
- db.close()
Add Comment
Please, Sign In to add comment