Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit

Permalink
feat(twitch): add proper twitch oauth
Browse files Browse the repository at this point in the history
  • Loading branch information
AtomToast committed Apr 25, 2020
1 parent 7294aa7 commit 52e432a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
4 changes: 3 additions & 1 deletion auth_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
server_url = "SERVER URL"
discord = "DISCORD BOT TOKEN"
google = "GOOGLE APPLICATION TOKEN"
twitch_id = "TWITCH APPLICATION TOKEN"
twitch_id = "TWITCH CLIENT ID"
twitch_secret = "TWITCH CLIENT SECRET"
twitch_token = "TWITCH APPLICATION ACCESS TOKEN"
google_callback_verification = "GOOGLE DOMAIN VERIFICATION FILE"
12 changes: 8 additions & 4 deletions ext/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ async def subscribe(self, ctx, *, channel=None):

# trying to get channel data
parsingChannelUrl = "https://api.twitch.tv/helix/users"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"login": channel}
async with self.bot.session.get(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
channel_obj = await resp.json()
Expand Down Expand Up @@ -100,7 +101,8 @@ async def subscribe(self, ctx, *, channel=None):

# send twitch subscription request
parsingChannelUrl = "https://api.twitch.tv/helix/webhooks/hub"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"hub.mode": "subscribe", "hub.callback": auth_token.server_url + "/twitch",
"hub.topic": "https://api.twitch.tv/helix/streams?user_id=" + channel_id, "hub.lease_seconds": 864000}
async with self.bot.session.post(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
Expand All @@ -126,7 +128,8 @@ async def unsubscribe(self, ctx, *, channel=None):

# try to get channel data
parsingChannelUrl = "https://api.twitch.tv/helix/users"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"login": channel}
async with self.bot.session.get(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
channel_obj = await resp.json()
Expand Down Expand Up @@ -156,7 +159,8 @@ async def unsubscribe(self, ctx, *, channel=None):

# send unsubscribe request
parsingChannelUrl = "https://api.twitch.tv/helix/webhooks/hub"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"hub.mode": "unsubscribe", "hub.callback": auth_token.server_url + "/twitch",
"hub.topic": "https://api.twitch.tv/helix/streams?user_id=" + channel_id}
async with self.bot.session.post(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
Expand Down
4 changes: 2 additions & 2 deletions ext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Utils(commands.Cog):
def __init__(self, bot):
self.bot = bot

self.svan_sleep_reminder = self.bot.loop.create_task(
self.sleep_reminder())
# self.svan_sleep_reminder = self.bot.loop.create_task(
# self.sleep_reminder())

# who and where the commands are permitted to use
@commands.has_permissions(manage_messages=True)
Expand Down
26 changes: 22 additions & 4 deletions ext/webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, bot):
# push notification run out after a specified time so I need to refresh them regularly
self.scheduler = AsyncIOScheduler(event_loop=self.bot.loop)
self.scheduler.add_job(self.refresh_subscriptions, "interval", days=3, id="refresher",
replace_existing=True, next_run_time=datetime.datetime.utcnow())
replace_existing=True, next_run_time=datetime.datetime.utcnow() + datetime.timedelta(seconds=+10, hours=+2))
self.scheduler.add_job(
self.ping_feedburner, "interval", minutes=3, id="pinger", replace_existing=True)
self.scheduler.start()
Expand Down Expand Up @@ -70,7 +70,8 @@ async def refresh_subscriptions(self):
for row in cursor:
ID = row[0]
parsingChannelUrl = "https://api.twitch.tv/helix/webhooks/hub"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"hub.mode": "subscribe", "hub.callback": auth_token.server_url + "/twitch",
"hub.topic": "https://api.twitch.tv/helix/streams?user_id=" + ID, "hub.lease_seconds": 864000}
async with self.bot.session.post(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
Expand All @@ -89,7 +90,22 @@ async def refresh_subscriptions(self):
print(resp.text)
await asyncio.sleep(2)

async with self.bot.session.get("https://id.twitch.tv/oauth2/validate", headers={'Authorization': "OAuth " + auth_token.twitch_token}) as resp:
if resp.status != 200:
print(resp.text)

queryString = {"client_id": auth_token.twitch_id,
"client_secret": auth_token.twitch_secret,
"grant_type": "client_credentials"}
async with self.bot.session.post("https://id.twitch.tv/oauth2/token", params=queryString) as resp2:
if resp2.status != 200:
print(resp.text)
else:
auth_token.twitch_secret = await resp2.json()[
"client_id"]

# pings feedburner to update feed

async def ping_feedburner(self):
parsingChannelUrl = "http://feedburner.google.com/fb/a/pingSubmit?bloglink=https%3A%2F%2Ffeeds.feedburner.com%2Fsurrenderat20%2FCqWw"
async with self.bot.session.get(parsingChannelUrl) as resp:
Expand Down Expand Up @@ -337,15 +353,17 @@ async def twitch_notifs(self, obj):

# getting channel data
parsingChannelUrl = "https://api.twitch.tv/helix/users"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"id": data["user_id"]}
async with self.bot.session.get(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
channel_obj = await resp.json()
ch = channel_obj["data"][0]

# getting game data
parsingChannelUrl = "https://api.twitch.tv/helix/games"
parsingChannelHeader = {'Client-ID': auth_token.twitch_id}
parsingChannelHeader = {'Client-ID': auth_token.twitch_id,
"Authorization": "Bearer " + auth_token.twitch_token}
parsingChannelQueryString = {"id": data["game_id"]}
async with self.bot.session.get(parsingChannelUrl, headers=parsingChannelHeader, params=parsingChannelQueryString) as resp:
game_obj = await resp.json()
Expand Down

0 comments on commit 52e432a

Please sign in to comment.