Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
import requests
import praw
import time
import sqlite3
from urllib.request import pathname2url
class Subscriptions():
subscriptions = []
endpoint = None
auth = None
client_id = None
client_secret = None
db = None
conn = None
reddit = None
def __init__(self, endpoint, auth, client_id, client_secret, db='pusher.db'):
self.endpoint = endpoint
self.auth = auth
self.client_id = client_id
self.client_secret = client_secret
self.db = db
self.conn = sqlite3.connect(self.db)
self.conn.row_factory = sqlite3.Row
self.reddit = praw.Reddit(client_id=self.client_id,
user_agent='python:com.lowpoly.nibbler.feed:v0.1 (by /u/ranzadk)')
def update_subscriptions(self):
self.subscriptions = []
c = self.conn.cursor()
c.execute('SELECT * FROM subscriptions')
for s in c.fetchall():
def check_subscriptions(self):
for subscription in self.subscriptions:
print('Checking: {}'.format(subscription.subreddit))
for submission in self.reddit.subreddit(subscription.subreddit).hot():
sid = int(, 36)
if sid > subscription.sid and not self.__link_exists(, submission.url):
self.__save_link(, submission.url)
self.__update_sid(sid, subscription.subreddit)
self.__post(subscription, submission)
def __link_exists(self, channel, link):
c = self.conn.cursor()
c.execute('SELECT * FROM links WHERE channel=? AND link=?', [channel, link])
if c.fetchone() == None:
return False
return True
def __save_link(self, channel, link):
c = self.conn.cursor()
c.execute('INSERT INTO links VALUES (?,?)', [channel, link])
def __update_sid(self, sid, subreddit):
c = self.conn.cursor()
c.execute('UPDATE subscriptions SET sid=? WHERE subreddit=?', [sid, subreddit])
def __post(self, subscription, submission):
msg = '[{}] **{}:** {}'.format(subscription.subreddit, submission.title, submission.url)
res =, json={'channel':,
'msg': msg}, headers={'Authorization': self.auth})
print('Posting: %s' % msg)
class Subscription():
sid = None
channel = None
subreddit = None
def __init__(self, sid, channel, subreddit):
self.sid = sid = channel
self.subreddit = subreddit
sub = Subscriptions(endpoint='...', auth='...',
client_id='...', client_secret='...')
while True:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.