add lowercase matching of terms of interest, add listing of terms of interest
This commit is contained in:
parent
d7d371a999
commit
a04fe9e7a7
39
bot.py
39
bot.py
|
@ -7,16 +7,19 @@ from bs4 import BeautifulSoup
|
||||||
import nextcord
|
import nextcord
|
||||||
from nextcord.ext import tasks, commands
|
from nextcord.ext import tasks, commands
|
||||||
|
|
||||||
|
|
||||||
def load_config(config_name):
|
def load_config(config_name):
|
||||||
if not os.path.exists(config_name):
|
if not os.path.exists(config_name):
|
||||||
return None
|
return None
|
||||||
with open(config_name, "rt", encoding="utf-8") as f:
|
with open(config_name, "rt", encoding="utf-8") as f:
|
||||||
return json.load(f)
|
return json.load(f)
|
||||||
|
|
||||||
|
|
||||||
def save_config(config_name, config):
|
def save_config(config_name, config):
|
||||||
with open(config_name, 'wt', encoding="utf-8") as f:
|
with open(config_name, 'wt', encoding="utf-8") as f:
|
||||||
json.dump(config, f, indent=1, ensure_ascii=False)
|
json.dump(config, f, indent=1, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
# dictionary of urls and time they were added
|
# dictionary of urls and time they were added
|
||||||
processed_urls = {}
|
processed_urls = {}
|
||||||
|
|
||||||
|
@ -30,11 +33,13 @@ if not config.get("deepl_key") or not config.get("discord_bot_token"):
|
||||||
|
|
||||||
intents = nextcord.Intents.default()
|
intents = nextcord.Intents.default()
|
||||||
bot = commands.Bot(command_prefix=".", intents=intents)
|
bot = commands.Bot(command_prefix=".", intents=intents)
|
||||||
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
|
headers = {
|
||||||
|
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
|
||||||
deepl_headers = {'Authorization': f'DeepL-Auth-Key {config["deepl_key"]}', 'User-Agent': 'YahooScraper/1.0.0'}
|
deepl_headers = {'Authorization': f'DeepL-Auth-Key {config["deepl_key"]}', 'User-Agent': 'YahooScraper/1.0.0'}
|
||||||
search_term = 'けものフレンズ'
|
search_term = 'けものフレンズ'
|
||||||
ping_terms: dict = config["ping_terms"]
|
ping_terms: dict = config["ping_terms"]
|
||||||
|
|
||||||
|
|
||||||
def add_ping(term, user_id):
|
def add_ping(term, user_id):
|
||||||
if term not in ping_terms:
|
if term not in ping_terms:
|
||||||
ping_terms[term] = [user_id]
|
ping_terms[term] = [user_id]
|
||||||
|
@ -48,6 +53,7 @@ def add_ping(term, user_id):
|
||||||
save_config("config.json", config)
|
save_config("config.json", config)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def remove_ping(term, user_id):
|
def remove_ping(term, user_id):
|
||||||
if term not in ping_terms:
|
if term not in ping_terms:
|
||||||
return False
|
return False
|
||||||
|
@ -59,29 +65,35 @@ def remove_ping(term, user_id):
|
||||||
save_config("config.json", config)
|
save_config("config.json", config)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def get_user_pings(user_id):
|
def get_user_pings(user_id):
|
||||||
return [term for term in ping_terms.keys() if user_id in ping_terms[term]]
|
return [term for term in ping_terms.keys() if user_id in ping_terms[term]]
|
||||||
|
|
||||||
|
|
||||||
async def ping_users(auction_name_jp, auction_name_en):
|
async def ping_users(auction_name_jp, auction_name_en):
|
||||||
"""If the name of the listing is particularly interesting, ping some people
|
"""If the name of the listing is particularly interesting, ping some people
|
||||||
- HAV0X"""
|
- HAV0X"""
|
||||||
users_to_ping = []
|
users_to_ping = []
|
||||||
|
interested_terms = []
|
||||||
for term in ping_terms.keys():
|
for term in ping_terms.keys():
|
||||||
if term in auction_name_jp or term in auction_name_en:
|
if term.lower() in auction_name_jp.lower() or term.lower() in auction_name_en.lower():
|
||||||
|
interested_terms.append(term)
|
||||||
for user in ping_terms[term]:
|
for user in ping_terms[term]:
|
||||||
if user not in users_to_ping:
|
if user not in users_to_ping:
|
||||||
users_to_ping.append(user)
|
users_to_ping.append(user)
|
||||||
|
|
||||||
if len(users_to_ping) > 0:
|
if len(users_to_ping) > 0:
|
||||||
ping_message = "Interesting listing, pinging: " + ", ".join([f"<@{user}>" for user in users_to_ping])
|
ping_message = "Interesting listing, pinging: " + ", ".join([f"<@{user}>" for user in users_to_ping]) + "\nTerms of interest: " + ', '.join([f'"{s}"' for s in interested_terms])
|
||||||
await send_message_text("Japari Modding", "yahoo-auctions", ping_message)
|
await send_message_text("Japari Modding", "yahoo-auctions", ping_message)
|
||||||
|
|
||||||
|
|
||||||
def translate(text: str):
|
def translate(text: str):
|
||||||
text = requests.post("https://api-free.deepl.com/v2/translate",
|
text = requests.post("https://api-free.deepl.com/v2/translate",
|
||||||
json={"text": [text], "target_lang": "EN", 'source_lang': 'JA'}, headers=deepl_headers).json()[
|
json={"text": [text], "target_lang": "EN", 'source_lang': 'JA'}, headers=deepl_headers).json()[
|
||||||
'translations'][0]['text']
|
'translations'][0]['text']
|
||||||
return text.replace("Beast Friends", "Kemono Friends")
|
return text.replace("Beast Friends", "Kemono Friends")
|
||||||
|
|
||||||
|
|
||||||
def embed_auction(service: str, url: str, name: str, name_en: str, thumbnail: str, price=None):
|
def embed_auction(service: str, url: str, name: str, name_en: str, thumbnail: str, price=None):
|
||||||
desc = name
|
desc = name
|
||||||
if price is not None:
|
if price is not None:
|
||||||
|
@ -91,6 +103,7 @@ def embed_auction(service: str, url: str, name: str, name_en: str, thumbnail: st
|
||||||
embed.set_image(url=thumbnail)
|
embed.set_image(url=thumbnail)
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
async def send_message(server_name: str, channel_name: str, embed: nextcord.Embed):
|
async def send_message(server_name: str, channel_name: str, embed: nextcord.Embed):
|
||||||
for guild in bot.guilds:
|
for guild in bot.guilds:
|
||||||
if guild.name == server_name:
|
if guild.name == server_name:
|
||||||
|
@ -99,6 +112,7 @@ async def send_message(server_name: str, channel_name: str, embed: nextcord.Embe
|
||||||
await channel.send(embed=embed)
|
await channel.send(embed=embed)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
async def send_message_text(server_name: str, channel_name: str, text: str):
|
async def send_message_text(server_name: str, channel_name: str, text: str):
|
||||||
for guild in bot.guilds:
|
for guild in bot.guilds:
|
||||||
if guild.name == server_name:
|
if guild.name == server_name:
|
||||||
|
@ -107,6 +121,7 @@ async def send_message_text(server_name: str, channel_name: str, text: str):
|
||||||
await channel.send(content=text)
|
await channel.send(content=text)
|
||||||
break
|
break
|
||||||
|
|
||||||
|
|
||||||
async def check_yahoo_fleamarket(search_term: str, page: int, notify: bool):
|
async def check_yahoo_fleamarket(search_term: str, page: int, notify: bool):
|
||||||
url = f'https://buyee.jp/paypayfleamarket/search?keyword={search_term}&order-sort=created_time&page={page}'
|
url = f'https://buyee.jp/paypayfleamarket/search?keyword={search_term}&order-sort=created_time&page={page}'
|
||||||
|
|
||||||
|
@ -142,6 +157,7 @@ async def check_yahoo_fleamarket(search_term: str, page: int, notify: bool):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(url, e)
|
print(url, e)
|
||||||
|
|
||||||
|
|
||||||
async def check_yahoo_auction(search_term: str, page: int, notify: bool):
|
async def check_yahoo_auction(search_term: str, page: int, notify: bool):
|
||||||
url = f'https://buyee.jp/item/search/query/{search_term}?sort=end&order=d&page={page}'
|
url = f'https://buyee.jp/item/search/query/{search_term}?sort=end&order=d&page={page}'
|
||||||
|
|
||||||
|
@ -174,6 +190,7 @@ async def check_yahoo_auction(search_term: str, page: int, notify: bool):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(url, e)
|
print(url, e)
|
||||||
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
for i in range(1, 4):
|
for i in range(1, 4):
|
||||||
|
@ -182,6 +199,7 @@ async def on_ready():
|
||||||
await asyncio.sleep(10)
|
await asyncio.sleep(10)
|
||||||
myLoop.start()
|
myLoop.start()
|
||||||
|
|
||||||
|
|
||||||
@tasks.loop(minutes=30)
|
@tasks.loop(minutes=30)
|
||||||
async def myLoop():
|
async def myLoop():
|
||||||
fulldate = datetime.now()
|
fulldate = datetime.now()
|
||||||
|
@ -203,6 +221,7 @@ async def myLoop():
|
||||||
if onlydate - processed_urls[key] > timedelta(weeks=3):
|
if onlydate - processed_urls[key] > timedelta(weeks=3):
|
||||||
del processed_urls[key]
|
del processed_urls[key]
|
||||||
|
|
||||||
|
|
||||||
@bot.slash_command(description="Pings you when auction name contains the given term")
|
@bot.slash_command(description="Pings you when auction name contains the given term")
|
||||||
async def ping_me(interaction: nextcord.Interaction, term: str):
|
async def ping_me(interaction: nextcord.Interaction, term: str):
|
||||||
if add_ping(term, interaction.user.id):
|
if add_ping(term, interaction.user.id):
|
||||||
|
@ -210,9 +229,12 @@ async def ping_me(interaction: nextcord.Interaction, term : str):
|
||||||
else:
|
else:
|
||||||
pings = get_user_pings(interaction.user.id)
|
pings = get_user_pings(interaction.user.id)
|
||||||
if len(pings) > 0:
|
if len(pings) > 0:
|
||||||
await interaction.response.send_message(f"Failed to add {term}. You are pinged for {', '.join(pings)}", ephemeral=True)
|
await interaction.response.send_message(f"Failed to add {term}. You are pinged for {', '.join(pings)}",
|
||||||
|
ephemeral=True)
|
||||||
else:
|
else:
|
||||||
await interaction.response.send_message(f"Failed to add {term}. You are not pinged for anything", ephemeral=True)
|
await interaction.response.send_message(f"Failed to add {term}. You are not pinged for anything",
|
||||||
|
ephemeral=True)
|
||||||
|
|
||||||
|
|
||||||
@bot.slash_command(description="No longer pings you when auction name contains the given term")
|
@bot.slash_command(description="No longer pings you when auction name contains the given term")
|
||||||
async def unping_me(interaction: nextcord.Interaction, term: str):
|
async def unping_me(interaction: nextcord.Interaction, term: str):
|
||||||
|
@ -221,8 +243,11 @@ async def unping_me(interaction: nextcord.Interaction, term : str):
|
||||||
else:
|
else:
|
||||||
pings = get_user_pings(interaction.user.id)
|
pings = get_user_pings(interaction.user.id)
|
||||||
if len(pings) > 0:
|
if len(pings) > 0:
|
||||||
await interaction.response.send_message(f"Failed to remove {term}. You are pinged for {', '.join(pings)}", ephemeral=True)
|
await interaction.response.send_message(f"Failed to remove {term}. You are pinged for {', '.join(pings)}",
|
||||||
|
ephemeral=True)
|
||||||
else:
|
else:
|
||||||
await interaction.response.send_message(f"Failed to remove {term}. You are not pinged for anything", ephemeral=True)
|
await interaction.response.send_message(f"Failed to remove {term}. You are not pinged for anything",
|
||||||
|
ephemeral=True)
|
||||||
|
|
||||||
|
|
||||||
bot.run(config["discord_bot_token"], reconnect=True)
|
bot.run(config["discord_bot_token"], reconnect=True)
|
Loading…
Reference in New Issue