okardec

Twitch.kt

Oct 15th, 2020 (edited)
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.36 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(channelJson: SimpleJSON){
  29.  
  30.         //channelJson.getInt("id"), channelJson.getString("channel")
  31.  
  32.         var url = "https://api.twitch.tv/helix/search/channels?query=${channelJson.getString("channel")}"  ///live_only=true&
  33.  
  34.         /*println("aqui...")
  35.         println(url)
  36.         println(channelJson)*/
  37.  
  38.         val headers = mapOf(
  39.                 "client-id" to clientID,
  40.                 "Authorization" to "Bearer $accessToken")
  41.  
  42.         val json = WebClient(url)
  43.                 .withHeader(headers)
  44.                 //.withHeader("client-id",clientID)
  45.                 //.withHeader("Authorization", "Bearer $accessToken")
  46.                 .download.asJSON()  ?: return
  47.  
  48.  
  49.         json.getJSONList("data").forEach {
  50.             //println(it)
  51.  
  52.             ////so atualiza se estiver em live, e o channel for igual ao display_name
  53.             if (it.getString("is_live") == "false" || it.getString("display_name") != channelJson.getString("channel")) return@forEach
  54.  
  55.             ///se tiver online atualiza
  56.             println(it.getString("display_name"))
  57.             println(it.getString("is_live"))
  58.  
  59.             val posts = mapOf(
  60.                     "id" to channelJson.getString("id"),
  61.                     "content" to it.getString("title")
  62.             )
  63.  
  64.             WebClient("https://painel3.gamevicio.com/control.php?area=live.update")
  65.                .withHeader("Cookie","userid=1;username=GameVicio")
  66.               .withPost(posts)
  67.              .download.asBytes()
  68.  
  69.  
  70.         }
  71.  
  72.  
  73.     }
  74.  
  75.     /**
  76.      * baixa a lista a ser verificada
  77.      */
  78.     fun getList(){
  79.         var url = "https://painel3.gamevicio.com/control.php?area=live.list"   ///live_only=true&
  80.  
  81.         val json = WebClient(url).withHeader("Cookie","userid=1;username=GameVicio").download.asJSON() ?: return
  82.  
  83.         json.getJSONList("items").forEach { it ->
  84.             //println(it)
  85.             //busca atualizações, pode retornar 0 caso estiver ainda em live
  86.             /*if (it.getInt("id") > 0 ) {
  87.                 checkLive(it)
  88.             }*/
  89.  
  90.             it.getInt("id").takeIf { it > 0 }?.apply { checkLive(it) }
  91.  
  92.         }
  93.  
  94.         ///atualiza o cache somente se retornar algo
  95.         //removi pois já está no cron
  96.         /*if (json.count("items") > 0) {
  97.             WebClient("https://gvapi.gamevicio.com/api/gv/?method=sys.live.cache").withHeader(
  98.                 "Cookie",
  99.                 "userid=1;username=GameVicio"
  100.             ).download.asBytes()
  101.         }*/
  102.  
  103.         //println(json)
  104.         //println(total)
  105.  
  106.     }
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. }
Add Comment
Please, Sign In to add comment