Skip to content
Snippets Groups Projects
Commit a6d0b145 authored by dnns01's avatar dnns01
Browse files

Add schedule functionality

parent 08cb60da
No related branches found
No related tags found
No related merge requests found
......@@ -121,3 +121,4 @@ GitHub.sublime-settings
/text_commands.json
/news.json
/highscores.json
/schedule.json
......@@ -3,5 +3,5 @@
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (strolly)" project-jdk-type="Python SDK" />
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (strolly)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
......@@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (strolly)" jdkType="Python SDK" />
<orderEntry type="jdk" jdkName="Python 3.9 (strolly)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
import json
import os
from datetime import datetime
import discord
from aiohttp import ClientSession
from discord.ext import commands, tasks
async def get_schedule():
async with ClientSession() as session:
auth = "kimne78kx3ncx6brgo4mv6wki5h1ko"
headers = {"client-id": f"{auth}", "Content-Type": "application/json"}
async with session.post("https://gql.twitch.tv/gql",
headers=headers,
json={
"query": "query {\r\n channel(name: \"indiestrolche\") {\r\n schedule {\r\n segments(includeFutureSegments: true) {\r\n id\r\n startAt\r\n title\r\n categories {\r\n displayName\r\n boxArtURL\r\n }\r\n }\r\n }\r\n }\r\n}"}) as r:
if r.status == 200:
return {segment["id"]: segment for segment in
(await r.json())["data"]["channel"]["schedule"]["segments"]}
return None
class Schedule(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.update_schedule.start()
self.schedule_file = "schedule.json"
self.schedule = self.load()
def load(self):
schedule_file = open(self.schedule_file, mode="r")
return json.load(schedule_file)
def save(self):
schedule_file = open(self.schedule_file, mode="w")
json.dump(self.schedule, schedule_file)
@tasks.loop(hours=1)
async def update_schedule(self):
new_schedule = await get_schedule()
for id, segment in new_schedule.items():
if id in self.schedule.keys():
old_segment = self.schedule.get(id)
if old_segment["startAt"] != segment["startAt"]:
self.schedule[id] = segment
await self.announce_segment(segment, new=False)
else:
self.schedule[id] = segment
await self.announce_segment(segment)
self.save()
async def announce_segment(self, segment, new=True):
channel = await self.bot.fetch_channel(int(os.getenv("DURCHSAGEN_CHANNEL")))
url = segment['categories'][0]['boxArtURL'].replace('-{width}x{height}', '').replace("/./", "/")
startAt = datetime.strptime(segment["startAt"], "%Y-%m-%dT%H:%M:%SZ").strftime("%d.%m.%Y %H:%M")
title = "<:ja:836282702248411217> <:aa:836282738709233675> <:aa:836282738709233675> <:aa:836282738709233675> <:aa:836282738709233675>" if new else "Achtung Leute aufgepasst!!!"
description = "Wie geil ist es? Ein neuer Stream ist in den Kalender geglitten\n" if new else "Es gibt eine kleine Änderung im Programmablauf!\n"
embed = discord.Embed(title=title, description=description)
embed.set_thumbnail(url=url)
embed.add_field(name=segment["title"], value=segment['categories'][0]['displayName'])
embed.add_field(name="Wann?", value=startAt)
await channel.send(embed=embed)
......@@ -8,6 +8,7 @@ from armin import Armin
from leaderboard import Leaderboard
from poll_cog import PollCog
from roll_cog import RollCog
from schedule import Schedule
# .env file is necessary in the same directory, that contains several strings.
load_dotenv()
......@@ -21,6 +22,7 @@ bot.add_cog(PollCog(bot))
bot.add_cog(RollCog(bot))
bot.add_cog(Leaderboard(bot))
bot.add_cog(Armin(bot))
bot.add_cog(Schedule(bot))
@bot.event
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment