-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (116 loc) · 4.96 KB
/
main.py
File metadata and controls
136 lines (116 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from telegram.ext import Updater, CommandHandler
import praw
import prawcore
import logging
import os
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG)
reddit = praw.Reddit('Bot') # change the ini var
TOKEN='token' # telegram api key
PORT = os.environ.get('PORT')
NAME = "bot"
def help(bot, update):
message = '/all : access frontpage and fetches the hottest posts\n'\
'/subreddit <subreddit> : access the subreddit and fetches the hottest posts\n'\
'/fetch <subreddit> : fetches a fresh unseen post from the subreddit <subreddit>\n'\
'/reset <subreddit> : resets already seen posts'
update.message.reply_text(message)
def help(bot, update):
message = 'Hello! I am RedditBot! Whatcha planning to do today?\n\n'\
'/(r, get, fetch) <subreddit> : fetches a post from <subreddit>\n'\
'/reset <subreddit> : resets already seen posts in <subreddit>\n'
update.message.reply_text(message)
def fetchfreshpost(bot, update):
_, *query = update.message.text.split()
if not query or query[0] == 'all':
sendfreshpost(bot, update, reddit.front)
else:
check = checksubreddit(query[0])
if check > 0:
errorhandler(bot, update, check)
else:
sendfreshpost(bot,update, reddit.subreddit(query[0]))
def sendfreshpost(bot, update, sr):
srlist = sr.hot(limit=15)
sent = False
for submission in srlist:
if submission.stickied or submission.over_18 or submission.hidden:
continue
else:
submission.hide()
if submission.url.endswith(('.jpg', '.png')):
# reddit-hosted images / imgur-hosted images
update.message.reply_photo(photo=submission.url,
caption='Title: {}'.format(submission.title),
quote=True)
sent = True
break
elif 'gyfcat' in submission.url:
# gyfcat-hosted gifs
update.message.reply_video(video=submission.url + '.mp4',
caption='Title: {}'.format(submission.title),
quote=True)
sent = True
break
elif submission.url.endswith(('.gif', '.gifv')):
# imgur-hosted gifs
update.message.reply_video(video=submission.url[0:submission.url.find('.gif')] + '.mp4',
caption='Title: {}'.format(submission.title),
quote=True)
sent = True
break
else:
# any other media
update.message.reply_text(text='Title: {}\nURL: {}\n'
.format(submission.title, submission.url), quote=True)
sent = True
break
if not sent:
update.message.reply_text(text='Sorry, we seem to have ran out of fresh content')
def errorhandler(bot, update, error=0):
if error == 1:
update.message.reply_text("Sorry, the subreddit is NSFW!", quote=True)
elif error == 2:
update.message.reply_text("Sorry, the subreddit does not exist!", quote=True)
else:
update.message.reply_text("Unknown error", quote=True)
def checksubreddit(sub):
try:
sr = reddit.subreddit(sub)
if sr.over18:
return 1
elif sr.subscribers < 1000:
return 2
else:
return 0
except prawcore.exceptions.Redirect or prawcore.exceptions.NotFound:
return 2
def reset(bot, update):
_, *query = update.message.text.split()
if not query or query[0] == 'all':
srlist = reddit.front.hot(limit=15)
for submission in srlist:
if submission.hidden:
submission.unhide()
bot.send_message(update.message.chat_id, text='All done!')
else:
try:
srlist = reddit.subreddit(query[0]).hot(limit=10)
for submission in srlist:
if submission.hidden:
submission.unhide()
bot.send_message(update.message.chat_id, text='All done!')
except prawcore.exceptions.Redirect or prawcore.exceptions.NotFound:
bot.send_message(update.message.chat_id, text="The subreddit /r/{} does not exist!".format(query[0]))
def main():
updater = Updater(TOKEN)
updater.start_webhook(listen="0.0.0.0",
port=int(PORT),
url_path=TOKEN)
updater.dispatcher.add_handler(CommandHandler('help', help))
updater.dispatcher.add_handler(CommandHandler(['r', 'fetch', 'get'], fetchfreshpost))
updater.dispatcher.add_handler(CommandHandler('reset', reset))
updater.bot.set_webhook("https://" + NAME + ".herokuapp.com/" + TOKEN)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()