From 03706b85faa60b68a00376fb5423e980325bfc4a Mon Sep 17 00:00:00 2001 From: Aaditya Thakur Date: Mon, 6 Feb 2023 21:38:51 +0530 Subject: [PATCH 1/2] Basic config system --- Aaditya/config | 3 +++ Aaditya/config.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Aaditya/config create mode 100644 Aaditya/config.py diff --git a/Aaditya/config b/Aaditya/config new file mode 100644 index 0000000..136da49 --- /dev/null +++ b/Aaditya/config @@ -0,0 +1,3 @@ +likes=6000 +true=false +hotel=trivago diff --git a/Aaditya/config.py b/Aaditya/config.py new file mode 100644 index 0000000..fd5a148 --- /dev/null +++ b/Aaditya/config.py @@ -0,0 +1,17 @@ +class Config: + def __init__(self, filename): + self.configpath = filename + self.config = {} + with open(self.configpath, "r") as file: + for i in file.readlines(): + i = i.split("=") + self.config[i[0]] = i[1].strip() + + def update(self, key, value): + self.config[key] = value + with open(self.configpath, 'w') as file: + for i, j in self.config.items(): + file.write(f'{i}={j}\n') + + def show(self): + print(self.config) \ No newline at end of file From aa05b1d27f1a4d4b0569343d4e3206d3fdaf5011 Mon Sep 17 00:00:00 2001 From: Aaditya Thakur Date: Fri, 10 Feb 2023 22:35:00 +0530 Subject: [PATCH 2/2] Config system works now --- Aaditya/config | 5 ++--- Aaditya/config.py | 4 ++-- Aaditya/main.py | 21 ++++++++++++++++++++- Aaditya/scrapper.py | 4 ++-- 4 files changed, 26 insertions(+), 8 deletions(-) diff --git a/Aaditya/config b/Aaditya/config index 136da49..6d9cb06 100644 --- a/Aaditya/config +++ b/Aaditya/config @@ -1,3 +1,2 @@ -likes=6000 -true=false -hotel=trivago +likes=50000 +num=1 diff --git a/Aaditya/config.py b/Aaditya/config.py index fd5a148..4047358 100644 --- a/Aaditya/config.py +++ b/Aaditya/config.py @@ -13,5 +13,5 @@ def update(self, key, value): for i, j in self.config.items(): file.write(f'{i}={j}\n') - def show(self): - print(self.config) \ No newline at end of file + def val(self, key): + return self.config[key] \ No newline at end of file diff --git a/Aaditya/main.py b/Aaditya/main.py index 23458ee..204df2c 100644 --- a/Aaditya/main.py +++ b/Aaditya/main.py @@ -1,11 +1,13 @@ import discord from scrapper import Scraper +from config import Config class Client(discord.Client): async def on_ready(self): print("Bot is online!") self.scraper = Scraper() + self.config = Config('config') async def on_message(self, message): if message.author == self.user: @@ -19,7 +21,7 @@ async def on_message(self, message): await message.channel.send(f"Looking up twitter for {x}") try: - data = self.scraper.scrape(x, 5) + data = self.scraper.scrape(x, int(self.config.val('num')), self.config.val('likes')) except: await message.channel.send(f"Couldn't find anything about {x} on twitter") return @@ -32,6 +34,23 @@ async def on_message(self, message): await message.channel.send(embed=discord.Embed(color=discord.Colour.blurple(), title=x, description=desc)) + elif message.content.startswith('!setlikes'): + try: + x = int(message.content.split()[1]) + self.config.update('likes', x) + await message.channel.send(f"Done! Minimum likes set to {x}") + except: + await message.channel.send("Please enter an integer value") + + elif message.content.startswith('!setnum'): + try: + + x = int(message.content.split()[1]) + self.config.update('num', x) + await message.channel.send(f"Done! Number of entries set to {x}") + except: + await message.channel.send("Please enter an integer value") + def main(): #only works if command is invoked from the working directory itself with open(".env", "r") as file: diff --git a/Aaditya/scrapper.py b/Aaditya/scrapper.py index ff86401..1946bfc 100644 --- a/Aaditya/scrapper.py +++ b/Aaditya/scrapper.py @@ -6,8 +6,8 @@ class Scraper: def __init__(self): pass - def scrape(self, text, num): - fQuery=text+' min_faves:4000' + def scrape(self, text, num, likes): + fQuery=text+f' min_faves:{likes}' data=sntwitter.TwitterSearchScraper(fQuery) tweets=[]