okardec

Twitch.kt

Oct 14th, 2020
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.12 KB | None | 0 0
  1. import core.io.SimpleJSON
  2. import core.net.WebClient
  3.  
  4.  
  5. class Twitch {
  6.  
  7.     val clientID = "3t6ct5oofvv82si9jkhixvsbyh204b"
  8.     val clientSecret = "2ukqmk576idjqbbtm510v4iozcch1p"
  9.     val redirectURL = "https://painel3.gamevicio.com/twitch.php"
  10.     val accessToken = "j8nd2n7hlhhcf6p2zsrsyt75rquvp2"   ///nao sei quanto tempos erá valido
  11.  
  12.  
  13.     /**
  14.      * retorna o tokken
  15.      */
  16.     fun getTokken(){
  17.  
  18.           println(WebClient("https://id.twitch.tv/oauth2/authorize?client_id=$clientID&redirect_uri=$redirectURL&response_type=token&scope=viewing_activity_read").
  19.             download.asString())
  20.  
  21.         ////irá retornar uma URL de redirecionamento para enviar um SMS
  22.         /// abra e logue-se, confirme o codigo do SMS e peque o tokken
  23.     }
  24.  
  25.     /**
  26.      * verifica se esta online
  27.      */
  28.     fun checkLive(id:Int,Channel:String){
  29.  
  30.         var url = "https://api.twitch.tv/helix/search/channels?query=$Channel"   ///live_only=true&
  31.  
  32.         val json = WebClient(url)
  33.             .withHeader("client-id",clientID)
  34.             .withHeader("Authorization","Bearer $accessToken")
  35.             .download.asJSON()
  36.  
  37.         if (json != null) {
  38.             json.getJSONList("data").forEach {
  39.                 //println(it)
  40.  
  41.                 ////so atualiza se estiver em live, e o channel for igual ao display_name
  42.                 if (it.getString("is_live") == "true" && it.getString("display_name") == Channel){
  43.  
  44.                     ///se tiver online atualiza
  45.                     println(it.getString("display_name"))
  46.                     println(it.getString("is_live"))
  47.  
  48.                     val posts = mapOf(
  49.                         "id" to id,
  50.                         "content" to it.getString("title")
  51.                     )
  52.  
  53.                     WebClient("https://painel3.gamevicio.com/control.php?area=live.update")
  54.                         .withHeader("Cookie","userid=1;username=GameVicio")
  55.                         .withPost(posts)
  56.                         .download.asJSON()
  57.  
  58.                 }
  59.             }
  60.         }
  61.  
  62.     }
  63.  
  64.     /**
  65.      * baixa a lista a ser verificada
  66.      */
  67.     fun getList(){
  68.         var url = "https://painel3.gamevicio.com/control.php?area=live.list"   ///live_only=true&
  69.  
  70.         val json = WebClient(url).withHeader("Cookie","userid=1;username=GameVicio").download.asJSON()
  71.  
  72.         if (json != null) {
  73.             val total = json?.count("items")
  74.             json.getJSONList("items").forEach {
  75.                 //println(it)
  76.                 //busca atualizações, pode retornar 0 caso estiver ainda em live
  77.                 if (it.getInt("id") > 0 ) {
  78.                     checkLive(it.getInt("id"), it.getString("channel"))
  79.                 }
  80.             }
  81.  
  82.             ///atualiza o cache somente se retornar algo
  83.             if (json?.count("items") > 0) {
  84.                 WebClient("https://gvapi.gamevicio.com/api/gv/?method=sys.live.cache").withHeader(
  85.                     "Cookie",
  86.                     "userid=1;username=GameVicio"
  87.                 ).download.asJSON()
  88.             }
  89.         }
  90.  
  91.         //println(json)
  92.         //println(total)
  93.  
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. }
Add Comment
Please, Sign In to add comment