Esempio n. 1
0
        public void LoadYoutubePlaylist(string ytLink, bool loadLength)
        {
            Match matchYtId = ytListMatch.Match(ytLink);

            if (!matchYtId.Success)
            {
                // error here
                return;
            }
            string id = matchYtId.Groups[2].Value;

            List <YoutubePlaylistItem> videoList = new List <YoutubePlaylistItem>();

            bool   hasNext   = false;
            object nextToken = null;

            do
            {
                var queryString = new Uri($"https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId={id}{(hasNext ? ("&pageToken=" + nextToken) : string.Empty)}&key={data.youtubeApiKey}");

                string response;
                if (!WebWrapper.DownloadString(out response, queryString))
                {
                    throw new Exception();                     // TODO correct error handling
                }
                var parsed     = (Dictionary <string, object>)json.DeserializeObject(response);
                var videoDicts = ((object[])parsed["items"]).Cast <Dictionary <string, object> >().ToArray();
                YoutubePlaylistItem[] itemBuffer = new YoutubePlaylistItem[videoDicts.Length];
                for (int i = 0; i < videoDicts.Length; i++)
                {
                    itemBuffer[i] = new YoutubePlaylistItem
                    {
                        AudioType = AudioType.Youtube,
                        Id        = (string)(((Dictionary <string, object>)videoDicts[i]["contentDetails"])["videoId"]),
                    }
                }
                ;
                hasNext = parsed.TryGetValue("nextPageToken", out nextToken);

                if (loadLength)
                {
                    queryString = new Uri($"https://www.googleapis.com/youtube/v3/videos?id={string.Join(",", itemBuffer.Select(item => item.Id))}&part=contentDetails&key={data.youtubeApiKey}");
                    if (!WebWrapper.DownloadString(out response, queryString))
                    {
                        throw new Exception();                         // TODO correct error handling
                    }
                    parsed     = (Dictionary <string, object>)json.DeserializeObject(response);
                    videoDicts = ((object[])parsed["items"]).Cast <Dictionary <string, object> >().ToArray();
                    for (int i = 0; i < videoDicts.Length; i++)
                    {
                        itemBuffer[i].Length = XmlConvert.ToTimeSpan((string)(((Dictionary <string, object>)videoDicts[i]["contentDetails"])["duration"]));
                    }
                }

                videoList.AddRange(itemBuffer);
            } while (hasNext);
        }
Esempio n. 2
0
		public void LoadYoutubePlaylist(string ytLink, bool loadLength)
		{
			Match matchYtId = ytListMatch.Match(ytLink);
			if (!matchYtId.Success)
			{
				// error here
				return;
			}
			string id = matchYtId.Groups[2].Value;

			List<YoutubePlaylistItem> videoList = new List<YoutubePlaylistItem>();

			bool hasNext = false;
			object nextToken = null;
			do
			{
				var queryString = new Uri($"https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId={id}{(hasNext ? ("&pageToken=" + nextToken) : string.Empty)}&key={data.youtubeApiKey}");

				string response;
				if (!WebWrapper.DownloadString(out response, queryString))
					throw new Exception(); // TODO correct error handling
				var parsed = (Dictionary<string, object>)json.DeserializeObject(response);
				var videoDicts = ((object[])parsed["items"]).Cast<Dictionary<string, object>>().ToArray();
				YoutubePlaylistItem[] itemBuffer = new YoutubePlaylistItem[videoDicts.Length];
				for (int i = 0; i < videoDicts.Length; i++)
					itemBuffer[i] = new YoutubePlaylistItem
					{
						AudioType = AudioType.Youtube,
						Id = (string)(((Dictionary<string, object>)videoDicts[i]["contentDetails"])["videoId"]),
					};
				hasNext = parsed.TryGetValue("nextPageToken", out nextToken);

				if (loadLength)
				{
					queryString = new Uri($"https://www.googleapis.com/youtube/v3/videos?id={string.Join(",", itemBuffer.Select(item => item.Id))}&part=contentDetails&key={data.youtubeApiKey}");
					if (!WebWrapper.DownloadString(out response, queryString))
						throw new Exception(); // TODO correct error handling
					parsed = (Dictionary<string, object>)json.DeserializeObject(response);
					videoDicts = ((object[])parsed["items"]).Cast<Dictionary<string, object>>().ToArray();
					for (int i = 0; i < videoDicts.Length; i++)
						itemBuffer[i].Length = XmlConvert.ToTimeSpan((string)(((Dictionary<string, object>)videoDicts[i]["contentDetails"])["duration"]));
				}

				videoList.AddRange(itemBuffer);
			} while (hasNext);
		}