1- using Discord_Stream_Notify_Bot . HttpClients . TwitCasting ;
1+ using Discord_Stream_Notify_Bot . HttpClients . Twitcasting . Model ;
22using System . Text ;
33
44#nullable enable
@@ -11,6 +11,7 @@ public class TwitcastingClient
1111 private readonly HttpClient ? _apiHttpClient ;
1212
1313 private readonly string ? _twitcastingAccessToken ;
14+ private static readonly string [ ] _events = [ "livestart" ] ;
1415
1516 public TwitcastingClient ( HttpClient httpClient , BotConfig botConfig )
1617 {
@@ -30,10 +31,10 @@ public TwitcastingClient(HttpClient httpClient, BotConfig botConfig)
3031 }
3132
3233 /// <summary>
33- /// 取得頻道正在直播的資料
34+ /// 取得直播分類資料
3435 /// </summary>
3536 /// <param name="channelId"></param>
36- /// <returns>如果正在直播,則回傳 (<see langword="true"/>, <see cref="int">影片 Id</see>),否則為 (<see langword="false"/>, <see cref="int">0</see>) </returns>
37+ /// <returns></returns>
3738 public async Task < List < Category > ? > GetCategoriesAsync ( )
3839 {
3940 if ( _apiHttpClient == null )
@@ -47,7 +48,7 @@ public TwitcastingClient(HttpClient httpClient, BotConfig botConfig)
4748 }
4849 catch ( Exception ex )
4950 {
50- Log . Error ( $ "TwitCastingClient.GetCategoriesAsync: { ex } ") ;
51+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.GetCategoriesAsync" ) ;
5152 return null ;
5253 }
5354 }
@@ -70,7 +71,7 @@ public TwitcastingClient(HttpClient httpClient, BotConfig botConfig)
7071 }
7172 catch ( Exception ex )
7273 {
73- Log . Error ( $ "TwitCastingClient.GetNewStreamDataAsync: { ex } ") ;
74+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.GetNewStreamDataAsync" ) ;
7475 return null ;
7576 }
7677 }
@@ -97,9 +98,146 @@ public TwitcastingClient(HttpClient httpClient, BotConfig botConfig)
9798 }
9899 catch ( Exception ex )
99100 {
100- Log . Error ( $ "TwitCastingClient.GetMovieInfoAsync: { ex } ") ;
101+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.GetMovieInfoAsync" ) ;
102+ return null ;
103+ }
104+ }
105+
106+ /// <summary>
107+ /// 取得使用者資訊
108+ /// </summary>
109+ /// <param name="userIdOrScreenId">使用者 id 或 screen_id</param>
110+ /// <returns><see cref="GetUserInfoResponse"/></returns>
111+ public async Task < GetUserInfoResponse ? > GetUserInfoAsync ( string userIdOrScreenId )
112+ {
113+ if ( _apiHttpClient == null )
114+ throw new NullReferenceException ( nameof ( _apiHttpClient ) ) ;
115+
116+ if ( string . IsNullOrEmpty ( userIdOrScreenId ) )
117+ throw new ArgumentNullException ( nameof ( userIdOrScreenId ) ) ;
118+
119+ try
120+ {
121+ var json = await _apiHttpClient . GetStringAsync ( $ "users/{ userIdOrScreenId } ") ;
122+ var data = JsonConvert . DeserializeObject < GetUserInfoResponse > ( json ) ;
123+ return data ;
124+ }
125+ catch ( HttpRequestException ex ) when ( ex . StatusCode == System . Net . HttpStatusCode . NotFound )
126+ {
127+ // Not Found
128+ return null ;
129+ }
130+ catch ( Exception ex )
131+ {
132+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.GetUserInfoAsync" ) ;
133+ return null ;
134+ }
135+ }
136+
137+ #region WebHook
138+ /// <summary>
139+ /// 取得所有已註冊的 WebHook
140+ /// </summary>
141+ /// <returns>返回 WebHook 列表</returns>
142+ /// <exception cref="NullReferenceException"></exception>
143+ public async Task < List < Webhook > ? > GetAllRegistedWebHookAsync ( )
144+ {
145+ if ( _apiHttpClient == null )
146+ throw new NullReferenceException ( nameof ( _apiHttpClient ) ) ;
147+
148+ const int pageSize = 50 ;
149+ int offset = 0 ;
150+ int allCount = int . MaxValue ;
151+ var result = new List < Webhook > ( ) ;
152+
153+ try
154+ {
155+ while ( result . Count < allCount )
156+ {
157+ var url = $ "webhooks?limit={ pageSize } &offset={ offset } ";
158+ var jsonResponse = await _apiHttpClient . GetStringAsync ( url ) ;
159+ var data = JsonConvert . DeserializeObject < GetAllRegistedWebHookJson > ( jsonResponse ) ;
160+
161+ if ( data ? . Webhooks == null || data . Webhooks . Count == 0 )
162+ break ;
163+
164+ if ( allCount == int . MaxValue )
165+ allCount = data . AllCount ;
166+
167+ result . AddRange ( data . Webhooks ) ;
168+ offset += pageSize ;
169+ }
170+
171+ return result ;
172+ }
173+ catch ( Exception ex )
174+ {
175+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.GetAllRegistedWebHookAsync" ) ;
176+ return null ;
177+ }
178+ }
179+
180+ /// <summary>
181+ /// 註冊 WebHook
182+ /// </summary>
183+ /// <param name="channelId"></param>
184+ /// <returns></returns>
185+ /// <exception cref="NullReferenceException"></exception>
186+ public async Task < bool ? > RegisterWebHookAsync ( string channelId )
187+ {
188+ if ( _apiHttpClient == null )
189+ throw new NullReferenceException ( nameof ( _apiHttpClient ) ) ;
190+
191+ if ( string . IsNullOrEmpty ( channelId ) )
192+ throw new NullReferenceException ( nameof ( channelId ) ) ;
193+
194+ try
195+ {
196+ var responseMessage = await _apiHttpClient . PostAsync ( "webhooks" , new StringContent ( JsonConvert . SerializeObject ( new
197+ {
198+ user_id = channelId ,
199+ events = _events
200+ } ) ) ) ;
201+
202+ responseMessage . EnsureSuccessStatusCode ( ) ;
203+
204+ return true ;
205+ }
206+ catch ( Exception ex )
207+ {
208+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.RegisterWebHookAsync" ) ;
209+ return null ;
210+ }
211+ }
212+
213+ /// <summary>
214+ /// 取消註冊 WebHook
215+ /// </summary>
216+ /// <param name="channelId"></param>
217+ /// <returns></returns>
218+ /// <exception cref="NullReferenceException"></exception>
219+ public async Task < bool ? > RemoveWebHookAsync ( string channelId )
220+ {
221+ if ( _apiHttpClient == null )
222+ throw new NullReferenceException ( nameof ( _apiHttpClient ) ) ;
223+
224+ if ( string . IsNullOrEmpty ( channelId ) )
225+ throw new NullReferenceException ( nameof ( channelId ) ) ;
226+
227+ try
228+ {
229+ var responseMessage = await _apiHttpClient . DeleteAsync ( $ "webhooks?user_id={ channelId } &events[]=livestart") ;
230+
231+ responseMessage . EnsureSuccessStatusCode ( ) ;
232+
233+ return true ;
234+ }
235+ catch ( Exception ex )
236+ {
237+ Log . Error ( ex . Demystify ( ) , "TwitCastingClient.RemoveWebHookAsync" ) ;
101238 return null ;
102239 }
103240 }
241+ #endregion
104242 }
105243}
0 commit comments