2022-02-16 03:15:41 +01:00
|
|
|
|
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;
|
2022-02-16 03:15:41 +01:00
|
|
|
|
using System.Threading.Tasks;
|
2022-05-05 15:06:10 +02:00
|
|
|
|
using VDownload.Core.Enums;
|
2022-02-16 03:15:41 +01:00
|
|
|
|
using VDownload.Core.Exceptions;
|
|
|
|
|
|
using VDownload.Core.Interfaces;
|
2022-03-07 14:59:11 +01:00
|
|
|
|
using VDownload.Core.Services.Sources.Twitch.Helpers;
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
namespace VDownload.Core.Services.Sources.Twitch
|
|
|
|
|
|
{
|
2022-05-05 15:06:10 +02:00
|
|
|
|
[Serializable]
|
|
|
|
|
|
public class Channel : IPlaylist
|
2022-02-16 03:15:41 +01:00
|
|
|
|
{
|
|
|
|
|
|
#region CONSTRUCTORS
|
|
|
|
|
|
|
|
|
|
|
|
public Channel(string id)
|
|
|
|
|
|
{
|
2022-05-05 15:06:10 +02:00
|
|
|
|
Source = PlaylistSource.TwitchChannel;
|
2022-05-11 20:50:50 +02:00
|
|
|
|
ID = id;
|
|
|
|
|
|
Url = new Uri($"https://twitch.tv/{ID}");
|
2022-02-16 03:15:41 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-02-19 01:38:37 +01:00
|
|
|
|
#region PROPERTIES
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
2022-05-05 15:06:10 +02:00
|
|
|
|
public PlaylistSource Source { get; private set; }
|
2022-05-11 20:50:50 +02:00
|
|
|
|
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; }
|
2022-02-16 03:15:41 +01:00
|
|
|
|
public string Name { get; private set; }
|
2022-05-05 15:06:10 +02:00
|
|
|
|
public IVideo[] Videos { get; private set; }
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-11 20:50:50 +02:00
|
|
|
|
#region PUBLIC METHODS
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
2022-02-26 14:32:34 +01:00
|
|
|
|
public async Task GetMetadataAsync(CancellationToken cancellationToken = default)
|
2022-02-16 03:15:41 +01:00
|
|
|
|
{
|
2022-03-02 22:13:28 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
JToken response = null;
|
|
|
|
|
|
using (WebClient client = await Client.Helix())
|
|
|
|
|
|
{
|
|
|
|
|
|
client.QueryString.Add("login", ID);
|
2022-03-09 23:43:51 +01:00
|
|
|
|
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
|
|
|
|
}
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
2022-05-11 20:50:50 +02:00
|
|
|
|
UniqueID = (string)response["id"];
|
2022-02-16 03:15:41 +01:00
|
|
|
|
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-02-16 03:15:41 +01:00
|
|
|
|
{
|
2022-03-02 22:13:28 +01:00
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
string pagination = "";
|
|
|
|
|
|
|
|
|
|
|
|
List<Vod> videos = new List<Vod>();
|
|
|
|
|
|
|
2022-03-05 02:39:37 +01:00
|
|
|
|
bool getAll = numberOfVideos == 0;
|
2022-02-16 03:15:41 +01:00
|
|
|
|
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-02-16 03:15:41 +01:00
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
JToken response = null;
|
|
|
|
|
|
using (WebClient client = await Client.Helix())
|
|
|
|
|
|
{
|
2022-05-11 20:50:50 +02:00
|
|
|
|
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"));
|
|
|
|
|
|
}
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
pagination = (string)response["pagination"]["cursor"];
|
|
|
|
|
|
|
2022-03-07 14:59:11 +01:00
|
|
|
|
videosData = response["data"].ToArray();
|
2022-02-16 03:15:41 +01:00
|
|
|
|
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);
|
2022-02-16 03:15:41 +01:00
|
|
|
|
|
|
|
|
|
|
await Task.WhenAll(getStreamsTasks);
|
|
|
|
|
|
|
|
|
|
|
|
Videos = videos.ToArray();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|