Files
VDownload/VDownload.Core/Sources/Twitch/Channel.cs

114 lines
3.6 KiB
C#
Raw Normal View History

using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
2022-02-26 14:32:34 +01:00
using System.Threading;
using System.Threading.Tasks;
2022-05-05 15:06:10 +02:00
using VDownload.Core.Enums;
using VDownload.Core.Exceptions;
using VDownload.Core.Interfaces;
2022-03-07 14:59:11 +01:00
using VDownload.Core.Services.Sources.Twitch.Helpers;
namespace VDownload.Core.Services.Sources.Twitch
{
2022-05-05 15:06:10 +02:00
[Serializable]
public class Channel : IPlaylist
{
#region CONSTRUCTORS
public Channel(string id)
{
2022-05-05 15:06:10 +02:00
Source = PlaylistSource.TwitchChannel;
ID = id;
Url = new Uri($"https://twitch.tv/{ID}");
}
#endregion
#region PROPERTIES
2022-05-05 15:06:10 +02:00
public PlaylistSource Source { get; private set; }
public string ID { get; private set; }
private string UniqueID { get; set; }
2022-05-05 15:06:10 +02:00
public Uri Url { get; private set; }
public string Name { get; private set; }
2022-05-05 15:06:10 +02:00
public IVideo[] Videos { get; private set; }
#endregion
#region PUBLIC METHODS
2022-02-26 14:32:34 +01:00
public async Task GetMetadataAsync(CancellationToken cancellationToken = default)
{
2022-03-02 22:13:28 +01:00
cancellationToken.ThrowIfCancellationRequested();
2022-03-07 14:59:11 +01:00
JToken response = null;
using (WebClient client = await Client.Helix())
{
client.QueryString.Add("login", ID);
response = JObject.Parse(await client.DownloadStringTaskAsync("https://api.twitch.tv/helix/users"))["data"];
if (((JArray)response).Count > 0) response = response[0];
else throw new MediaNotFoundException($"Twitch Channel (ID: {ID}) was not found");
2022-03-07 14:59:11 +01:00
}
UniqueID = (string)response["id"];
Name = (string)response["display_name"];
}
2022-05-05 15:06:10 +02:00
public async Task GetVideosAsync(CancellationToken cancellationToken = default) => await GetVideosAsync(0, cancellationToken);
2022-02-26 14:32:34 +01:00
public async Task GetVideosAsync(int numberOfVideos, CancellationToken cancellationToken = default)
{
2022-03-02 22:13:28 +01:00
cancellationToken.ThrowIfCancellationRequested();
string pagination = "";
List<Vod> videos = new List<Vod>();
2022-03-05 02:39:37 +01:00
bool getAll = numberOfVideos == 0;
int count;
JToken[] videosData;
List<Task> getStreamsTasks = new List<Task>();
do
{
2022-03-05 02:39:37 +01:00
count = numberOfVideos < 100 && !getAll ? numberOfVideos : 100;
2022-03-07 14:59:11 +01:00
JToken response = null;
using (WebClient client = await Client.Helix())
{
client.QueryString.Add("user_id", UniqueID);
2022-03-07 14:59:11 +01:00
client.QueryString.Add("first", count.ToString());
client.QueryString.Add("after", pagination);
response = JObject.Parse(await client.DownloadStringTaskAsync("https://api.twitch.tv/helix/videos"));
}
pagination = (string)response["pagination"]["cursor"];
2022-03-07 14:59:11 +01:00
videosData = response["data"].ToArray();
foreach (JToken videoData in videosData)
{
Vod video = new Vod((string)videoData["id"]);
video.GetMetadataAsync(videoData);
getStreamsTasks.Add(video.GetStreamsAsync());
videos.Add(video);
numberOfVideos--;
}
}
2022-03-05 02:39:37 +01:00
while ((getAll || numberOfVideos > 0) && count == videosData.Length);
await Task.WhenAll(getStreamsTasks);
Videos = videos.ToArray();
}
#endregion
}
}