From b984715472f61310443dcf6b00d175b55ff73344 Mon Sep 17 00:00:00 2001
From: tholimet <tholimet@localhost>
Date: Mon, 4 Mar 2024 16:58:27 +0100
Subject: [PATCH] =?UTF-8?q?Transf=C3=A9rer=20les=20fichiers=20vers=20'ScdL?=
 =?UTF-8?q?iv'?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ScdLiv/main.py | 49 ++++++++++++++++++++++++++++++-------------------
 1 file changed, 30 insertions(+), 19 deletions(-)

diff --git a/ScdLiv/main.py b/ScdLiv/main.py
index dbf3380..d43152b 100644
--- a/ScdLiv/main.py
+++ b/ScdLiv/main.py
@@ -1,16 +1,17 @@
 from tkinter import *
 from tkinter import messagebox
 import mysql.connector
+from obswsrc import OBSWS
 from obswebsocket import obsws, requests
 from twitch.helix.api import TwitchHelix
 from twitchAPI.helper import first
 import asyncio
-import twitchAPI
 import webbrowser
+import requests
     
 class NouvelleFenetre:
 
-    oauth_authorization_url = f"https://id.twitch.tv/oauth2/authorize?client_id={'6h883my3p6ndqozotnl6nsbllyyr3h'}&redirect_uri={'http://localhost'}&response_type=code&scope={'user_read'}"
+    oauth_authorization_url = f"https://id.twitch.tv/oauth2/authorize?client_id={'6h883my3p6ndqozotnl6nsbllyyr3h'}&redirect_uri={'http://localhost'}&response_type=token&scope={'user_read'}"
     
     # Fonction permettant de fermer la deuxième fenetre créee et la première fenetre de connexion de se réafficher tout en se déconnecctant d'OBS
     def fermer_et_reafficher(self):
@@ -139,16 +140,22 @@ class NouvelleFenetre:
             self.is_twitch_streaming = True
 
     client_id = '6h883my3p6ndqozotnl6nsbllyyr3h'
-    oauth_token ='r4l2v9bgvlb0uh6cqqgw9j85tlojvd'
+    oauth_token ='z11zvdhgxvemn2l8kjl1dvczgiyelt'
     login = 'iSeralestis'
 
     async def get_twitch_user(self, client_id, oauth_token, login):
-        twitch = TwitchHelix(client_id, oauth_token)
-    
-        user = await first (twitch.get_users(login_names= login))
-    
-        return user
-        
+        try:
+            twitch = TwitchHelix(client_id, oauth_token)
+            users = await twitch.get_users(login)
+            if users:
+                return users[0]
+            else:
+                print("Utilisateur Twitch non trouvé.")
+                return None
+        except Exception as e:
+            print(f"Une erreur s'est produite lors de la récupération de l'utilisateur Twitch : {e}")
+            return None
+            
     async def initialize_twitch(self):
         twitch_user = await self.get_twitch_user(self.client_id, self.oauth_token, self.login)
         if twitch_user:
@@ -157,6 +164,7 @@ class NouvelleFenetre:
             print("Username:", twitch_user.display_name)
         else:
             print("Twitch user not found.")
+
       
 
     # Création de la nouvelle fenetre une fois l'authentification faite
@@ -195,11 +203,6 @@ class NouvelleFenetre:
         bouton_auth_twitch = Button(self.root, text="Autoriser l'accès à Twitch", command=self.lancer_auth_twitch)
         bouton_auth_twitch.grid(row=15, column=0)
 
-        # C'est mon id de ma chaine twitch identifiant Alexis
-        self.client_id = '6h883my3p6ndqozotnl6nsbllyyr3h'
-        self.oauth_token ='r4l2v9bgvlb0uh6cqqgw9j85tlojvd'
-        self.login = 'iSeralestis'
-
         self.twitch_client = TwitchHelix(client_id = self.client_id, oauth_token = self.oauth_token)
 
         # On récupère la clé Twitch depuis la base de données
@@ -255,10 +258,18 @@ class NouvelleFenetre:
         bouton_fermer.grid(row = 13, column = 0)
 
     def lancer_auth_twitch(self):
-        # Ouvrir le lien dans le navigateur Web par défaut
-        webbrowser.open(self.oauth_authorization_url)
-
-        # Afficher un message indiquant que l'utilisateur doit autoriser l'accès à Twitch dans le navigateur
-        messagebox.showinfo("Authentification Twitch", "Veuillez autoriser l'accès à Twitch dans le navigateur Web qui s'est ouvert.")
+        # Vérifier si la colonne OAuth est vide dans la base de données
+        oauth_token = self.get_oauth_token_from_database()
+        if oauth_token is None:
+            # Si la colonne OAuth est vide, ouvrir la page d'authentification Twitch
+            webbrowser.open(self.oauth_authorization_url)
+            messagebox.showinfo("Authentification Twitch", "Veuillez autoriser l'accès à Twitch dans le navigateur Web qui s'est ouvert.")
+        else:
+            # Si la colonne OAuth n'est pas vide, afficher un message indiquant que l'authentification n'est pas nécessaire
+            messagebox.showinfo("Authentification Twitch", "L'authentification Twitch n'est pas nécessaire car le token OAuth est déjà présent dans la base de données.")
 
+    def get_oauth_token_from_database(self):
+        # Code pour récupérer la valeur de la colonne OAuth dans la base de données
+        # Retourne la valeur de la colonne OAuth
+        return self.oauth_token
         
\ No newline at end of file