diff --git a/authentication.py b/authentication.py new file mode 100644 index 0000000..d7b39ac --- /dev/null +++ b/authentication.py @@ -0,0 +1,22 @@ +import praw + +##### REDDIT + +reddit = praw.Reddit(client_id='YourId', + client_secret = 'YourSecret' , + username ='YourUsername' , + password = 'YourPassword', + user_agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36') + +#Check the praw documentation if you have questions (You get these (exept user agent) when you create an app whithin a Reddit) +# reddit.com/prefs/apps/ +##### + + + + +#####DISCORD + +TOKEN = 'YOUR TOKEN'#You can find it when the bot application created here: discord.com/developers/applications + +##### diff --git a/functions.py b/functions.py new file mode 100644 index 0000000..d44aaf7 --- /dev/null +++ b/functions.py @@ -0,0 +1,48 @@ +import praw +import urllib +from urllib import request,parse + +url_opn_list = [] + +headers = {} +headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36' + +def reddit_parse_hot(reddit, sub , many): + loop = 0 + what_to_parse = reddit.subreddit(sub) + hot_posts = what_to_parse.hot(limit = many) + for post in hot_posts: + post_url = post.url + print(post_url) + req = request.Request(post_url, headers = headers) + url_opn = request.urlopen(req) + url_opn_list.append(post_url) + loop += 1 + return url_opn_list + +def reddit_parse_time(reddit, sub, many): + loop = 0 + what_to_parse = reddit.subreddit(sub) + time_posts = what_to_parse.new(limit = many) + for post in time_posts: + post_url = post.url + print(post_url) + req = request.Request(post_url, headers = headers) + url_opn = request.urlopen(req) + url_opn_list.append(post_url) + loop += 1 + return url_opn_list + + +def random_subreddit(reddit,many): + loop = 0 + what_to_parse = reddit.subreddit('random') + hot_posts = what_to_parse.hot(limit = many) + for post in hot_posts: + post_url = post.url + print(post_url) + req = request.Request(post_url, headers = headers) + url_opn = request.urlopen(req) + url_opn_list.append(post_url) + loop += 1 + return url_opn_list, what_to_parse diff --git a/main.py b/main.py new file mode 100644 index 0000000..5656ccc --- /dev/null +++ b/main.py @@ -0,0 +1,57 @@ +from authentication import TOKEN, reddit +import discord +from discord.ext import commands +from functions import * + +client = commands.Bot(command_prefix = '#') + +@client.event +async def on_ready(): + print('=======================================================') + print(' READY ') + print('=======================================================') + await client.change_presence(activity = discord.Game('game')) + #You can delete this ↑↑↑ line if you don`t want your bot to have an activity showing + + +@client.command() +async def parse_hot(ctx, subreddit , how_many = 5): + await ctx.send('Okay, the subreddit is : r/{}; I`ll show you {} posts in hot'.format(str(subreddit), str(how_many))) + url_opn_list.clear() + try: + reddit_parse_hot(reddit,str(subreddit),int(how_many)) + for url in url_opn_list: + await ctx.send(url) + except Exception as error: + await ctx.send('Error : '+ str(error)+ '\n') + +@client.command() +async def parse_time(ctx, subreddit, how_many = 5): + await ctx.send('Okay, the subreddit is : r/{}; I`ll show you {} posts based on time'.format(str(subreddit), str(how_many))) + url_opn_list.clear() + try: + reddit_parse_time(reddit,str(subreddit),int(how_many)) + for url in url_opn_list: + await ctx.send(url) + except Exception as error: + await ctx.send('Error : '+ str(error)+ '\n') + + +@client.command() +async def check(ctx): + await ctx.send(' ```css' +'\n'+ '[Online]' + '\n'+ '```') + + +@client.command() +async def random(ctx,how_many = 5): + url_opn_list.clear() + try: + what_to_parse = random_subreddit(reddit,int(how_many))[1] + await ctx.send('Aaaand... The subreddit is: r/{}. I`ll show you {} posts in hot'.format(str(what_to_parse),str(how_many))) + for url in url_opn_list: + await ctx.send(url) + except Exception as error: + await ctx.send('Error : '+ str(error)+ '\n') + + +client.run(TOKEN)