Skip to content

Instantly share code, notes, and snippets.

@kirkegaard kirkegaard/app.py
Last active Jul 31, 2018

Embed
What would you like to do?
Subscribe to multiple channels using Pusher and post to Discord
import requests
import pysher
import time
import json
appkey = '50ed18dd967b455393ed'
endpoint = '...'
auth = '...'
subscriptions = []
channels = {
123456789: [
'blunderyears',
'holdmybeer',
'holdmycosmo'
],
987654321: [
'getthatguyabeer',
'whatcouldgowrong',
'nononono'
]
}
class Subscription():
listener = None
channel = None
subreddit = None
def __init__(self, channel, subreddit):
self.channel = channel
self.subreddit = subreddit
self.listener = pusher.subscribe(self.subreddit)
self.listener.bind('new-listing', self.handler)
print('Subscriping: {} : {}'.format(channel, subreddit))
def handler(self, data):
j = json.loads(data)
title = j['title']
subreddit = j['subreddit']
url = j['url']
msg = '[{}] **{}:** {}'.format(subreddit, title, url)
res = requests.post(endpoint, json={'channel': self.channel, 'msg': msg}, headers={'Authorization': auth})
if res.status_code:
print('Posted: {}'.format(msg))
else:
print('Failed to post')
pusher = pysher.Pusher(appkey)
def connect_handler(data):
for c in channels:
for s in channels[c]:
subscriptions.append(Subscription(c, s))
pusher.connection.bind('pusher:connection_established', connect_handler)
pusher.connect()
while True:
time.sleep(1)
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.