11package org .codeoverflow .chatoverflow .requirement .service .twitch .api
22
3- class TwitchAPIConnector {
3+ import akka .actor .{ActorRef , ActorSystem , Props }
4+ import akka .pattern .ask
5+ import akka .util .Timeout
6+ import org .apache .http .HttpEntity
7+ import org .apache .http .client .methods .HttpGet
8+ import org .apache .http .client .utils .URIBuilder
9+ import org .apache .http .util .EntityUtils
10+ import org .codeoverflow .chatoverflow .WithLogger
11+ import org .codeoverflow .chatoverflow .connector .Connector
12+ import org .codeoverflow .chatoverflow .framework .actors .HttpClientActor
413
14+ import scala .concurrent .Await
15+ import scala .concurrent .duration ._
16+
17+ // FIXME: Chery picked from Class Library Rework, should be reworked, lol
18+
19+ /**
20+ * The twitch api connector
21+ *
22+ * @param sourceIdentifier the name to the twitch account
23+ */
24+ class TwitchAPIConnector (override val sourceIdentifier : String ) extends Connector (sourceIdentifier) with WithLogger {
25+ private val API_FORMAT : String = " application/vnd.twitchtv.v5+json"
26+ private val BASE_URL : String = " https://api.twitch.tv/helix/"
27+ private val BASE_URL_v5 : String = " https://api.twitch.tv/kraken/"
28+ private val actorSystem = ActorSystem (" TwitchAPIActorSystem" )
29+ private val actor : ActorRef = actorSystem.actorOf(Props [HttpClientActor ])
30+ private var clientID = " "
31+ private var oauth = " "
32+ requiredCredentialKeys = List (TwitchAPIConnector .credentialsClientID, TwitchAPIConnector .credentialsOauthKey)
33+
34+ override def getUniqueTypeString : String = this .getClass.getName
35+
36+ /**
37+ * Returns true, if the connector has been already instantiated and is running.
38+ */
39+ override def isRunning : Boolean = true
40+
41+ /**
42+ * Initializes the connector, e.g. creates a connection with its platform.
43+ */
44+ override def init (): Boolean = {
45+ val oauth = credentials.get.getValue(TwitchAPIConnector .credentialsOauthKey)
46+ val clientID = credentials.get.getValue(TwitchAPIConnector .credentialsClientID)
47+
48+ if (clientID.isEmpty) {
49+ logger warn s " key ' ${TwitchAPIConnector .credentialsClientID}' not found in credentials for ' $sourceIdentifier'. "
50+ false
51+ } else {
52+ this .clientID = clientID.get
53+ if (oauth.isEmpty) {
54+ logger warn s " key ' ${TwitchAPIConnector .credentialsOauthKey}' not found in credentials for ' $sourceIdentifier'. "
55+ false
56+ } else {
57+ this .oauth = oauth.get
58+ true
59+ }
60+ }
61+ }
62+
63+ def getSubscriptions (channelID : String , offset : Int = 0 , newestFirst : Boolean = true ): String = {
64+ get(" channels/" + channelID + " /subscriptions" , auth = true , oldAPI = true , Seq ((" limit" , " 100" ), (" offset" , String .valueOf(offset)), (" direction" , if (newestFirst) " desc" else " asc" )))
65+ }
66+
67+ def getUser (userLogin : String ): String = {
68+ get(" users" , auth = false , oldAPI = false , Seq ((" login" , userLogin)))
69+ }
70+
71+ def get (uri : String , auth : Boolean , oldAPI : Boolean , queryParams : Seq [(String , String )]): String = {
72+ val httpGet = if (auth) getUrlAuth(uri, oldAPI) else getURL(uri, oldAPI)
73+ val urlBuilder = new URIBuilder (httpGet.getURI)
74+ queryParams.foreach(param => urlBuilder.addParameter(param._1, param._2))
75+ httpGet.setURI(urlBuilder.build())
76+ implicit val timeout : Timeout = Timeout (5 seconds)
77+ val entity = Await .result(actor ? httpGet, timeout.duration).asInstanceOf [HttpEntity ]
78+ if (entity != null ) {
79+ EntityUtils .toString(entity, " UTF-8" )
80+ }
81+ else " "
82+ }
83+
84+ def getUrlAuth (uri : String , oldAPI : Boolean ): HttpGet = {
85+ val get = getURL(uri, oldAPI)
86+ get.setHeader(" Authorization" , oauth)
87+ get
88+ }
89+
90+ def getURL (uri : String , oldAPI : Boolean ): HttpGet = {
91+ val baseUrl = if (oldAPI) BASE_URL_v5 else BASE_URL
92+ new HttpGet (baseUrl + uri) {
93+ setHeader(" Accept" , API_FORMAT )
94+ setHeader(" Client-ID" , clientID)
95+ }
96+ }
97+
98+ def getFollowers (userID : String ): String = {
99+ get(" users/follows" , auth = false , oldAPI = false , Seq ((" to_id" , userID)))
100+ }
101+
102+ /**
103+ * Shuts down the connector, closes its platform connection.
104+ */
105+ override def shutdown (): Unit = ???
5106}
107+
108+ object TwitchAPIConnector {
109+ val credentialsOauthKey = " oauth"
110+ val credentialsClientID = " clientid"
111+ }
0 commit comments