-
Notifications
You must be signed in to change notification settings - Fork 0
/
YoutubeSearch.cs
69 lines (55 loc) · 1.66 KB
/
YoutubeSearch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Upload;
using Google.Apis.Util.Store;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
internal class YouTubeSearcher
{
private const string API_key = "AIzaSyC98V5xKK5ba027fvhue02zrc7ctPTyFPc";
private readonly YouTubeService service;
private SearchListResponse response;
private int index;
public YouTubeSearcher()
{
this.service = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = API_key,
ApplicationName = "Sharpvin"
});
this.index = 0;
response = new SearchListResponse();
}
public async Task<String> Search(string query)
{
var request = this.service.Search.List("snippet");
request.Q = query;
request.MaxResults = 5;
request.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.None;
request.Type = "video";
request.RelevanceLanguage = "en";
this.response = await request.ExecuteAsync();
this.index = 0;
if (response.Items.Count == 0)
{
throw new Exception("No results from youtube");
}
return $"https://www.youtube.com/watch?v={response.Items[0].Id.VideoId}";
}
public String NextVideo()
{
index++;
if (index < response.Items.Count)
{
return $"https://www.youtube.com/watch?v={response.Items[this.index].Id.VideoId}";
}
else
return "";
}
}