Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import requests.auth
- import sys
- import time
- import os
- import random
- from pprint import pprint
- import pyrebase
- import datetime
- # variables #
- print('setting variables')
- id = 'YdSbzhqDbT6kRw'
- secret = 'ZCCKeZEYE2a5CnbEqTisPk0VZOo'
- userAgent = 'motobebot 1.0'
- user = 'motoBeBot'
- password = 'z0mg7812'
- waiting = 60
- url = 'https://www.reddit.com/r/YourBotTestingGround/comments/.json'
- headers = {'User-agent': userAgent}
- print('variables set')
- config = {
- 'apiKey':
- 'authDomain':
- 'databaseURL':
- 'storageBucket':
- }
- fb = pyrebase.initialize_app(config)
- db = fb.database()
- # functions #
- class Token:
- def __init__(self):
- self.files = []
- def __enter__(self):
- print('getting token')
- client_auth = requests.auth.HTTPBasicAuth(id, secret) # id and secret
- post_data = {'grant_type': 'password', 'username': user, 'password': password, 'scope': 'submit identity'} # post data to request access token
- headers = {'User-agent': userAgent} # user agent header
- r = requests.post('https://www.reddit.com/api/v1/access_token', auth=client_auth, data=post_data, headers=headers)
- msg = r.json()
- print('token received')
- self.accessToken = msg['access_token']
- return self
- def __exit__(self, exc_type, exc_value, traceback):
- print('revoking token')
- headers = {'User-agent': userAgent}
- post_data = {'token': self.accessToken, 'token_type_hint': 'access_token'}
- client_auth = requests.auth.HTTPBasicAuth(id, secret)
- r = requests.post('https://www.reddit.com/api/v1/revoke_token', auth=client_auth, data=post_data, headers=headers)
- print('token revoked')
- def reply(commentId, text):
- with Token() as token:
- headers = {'Authorization': 'bearer ' + token.accessToken, 'User-Agent': user}
- post_data = {'thing_id': 't1_' + commentId, 'api_type': 'json', 'text': text}
- r = requests.post('https://oauth.reddit.com/api/comment', headers=headers, data=post_data)
- class Events:
- def __init__(self, database):
- self.db = database
- def getEvents(self):
- events = self.db.child('events').order_by_key().get()
- data = events.val()
- print(data)
- text = '''event|date\n:--|:--'''
- for key in data:
- eventD, eventM, eventY = map(int, data[key]['date'].split('/'))
- eventDate = datetime.datetime(eventY, eventM, eventD)
- if eventDate < datetime.datetime.now():
- text += '\n~~' + data[key]['name'] + '~~|~~' + data[key]['date']+'~~'
- else:
- text += '\n'+data[key]['name']+'|'+data[key]['date']
- return text
- def saveEvent(self, eventId, eventName, date):
- data = {'name': eventName, 'date': date}
- self.db.child('events').child(eventId).set(data)
- class Comment:
- def __init__(self, url):
- self.url = url
- def getComment(self):
- print('getting latest comment')
- headers = {'User-agent': userAgent}
- self.url += '?limit=1'
- r = requests.get(self.url, headers=headers)
- rJson = r.json()
- data = rJson['data']['children']
- msg = data[0]
- comment = msg['data']['body']
- self.parseComment(comment)
- self.commentId = msg['data']['id']
- self.linkUrl = msg['data']['link_url']
- def parseComment(self, command):
- self.command = command
- if len(command) > 4:
- self.commandSplit = command.split(' ')
- if len(self.commandSplit) >= 1:
- self.botCommand = self.commandSplit[0]
- if len(self.commandSplit) >= 2:
- self.botAction = self.commandSplit[1]
- if len(self.commandSplit) >= 3:
- self.eventName = self.commandSplit[2]
- if len(self.commandSplit) >= 4:
- self.eventDate = self.commandSplit[3]
- if len(self.commandSplit) >= 5:
- self.eventTime = self.commandSplit[4]
- class bot:
- def __init__(self, url, database):
- self.url = url
- self.events = Events(database)
- def loop(self):
- comment = Comment(self.url)
- while True:
- comment.getComment()
- if comment.botCommand == '!bot':
- print(comment.command)
- if comment.botAction == 'roll20':
- rng = random.randint(1, 20)
- reply(comment.commentId, 'rolling d20, the result is %s' % str(rng))
- elif comment.botAction == 'create':
- if len(comment.commandSplit) >= 4 and '/' in comment.eventDate:
- dateD, dateM, dateY = map(int, comment.eventDate.split('/'))
- eventId = dateD+(dateM*100)+(dateY*10000)
- date = date.strftim('%d/%m/%Y')
- Events.saveEvent(self.events, eventId, comment.eventName, comment.eventDate)
- else:
- print('wrong comment format')
- elif comment.botAction == 'list':
- text = self.events.getEvents()
- reply(comment.commentId, text)
- else:
- print('no comment the bot can use')
- print('trying again in %i seconds' % waiting)
- time.sleep(waiting)
- bot(url, db).loop()
Add Comment
Please, Sign In to add comment