Skip to content

Commit fce0500

Browse files
committed
fix video search result
1 parent 49723c9 commit fce0500

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/DuckDuckGoDotNet/DuckDuckGoSearch.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private async Task<string> GetVqd(string keywords)
160160
stream.Read(content, 0, content.Length);
161161
return Utils.ExtractVqd(content, keywords);
162162
}
163-
163+
164164
/// <summary>
165165
/// DuckDuckGo text search. Query params: https://duckduckgo.com/params.
166166
/// </summary>
@@ -416,7 +416,7 @@ public async Task<IEnumerable<ImageSearchItem>> ImagesAsync(
416416
/// <param name="licenseVideos">creativeCommon, youtube. Defaults to null.</param>
417417
/// <param name="maxResults">Max number of results. If null, returns results only from the first response. Defaults to null.</param>
418418
/// <returns>List of video search results.</returns>
419-
public async Task<List<Dictionary<string, string>>> VideosAsync(
419+
public async Task<List<VideoSearchItem>> VideosAsync(
420420
string keywords,
421421
string region = "wt-wt",
422422
string safesearch = "moderate",
@@ -443,25 +443,15 @@ public async Task<List<Dictionary<string, string>>> VideosAsync(
443443
};
444444

445445
var cache = new HashSet<string>();
446-
var results = new List<Dictionary<string, string>>();
446+
var results = new List<VideoSearchItem>();
447447

448448
for (int i = 0; i < 8; i++)
449449
{
450450
using var resp = await GetUrl("GET", "https://duckduckgo.com/v.js", paramsDict: payload);
451451
var jsonStr = await resp.Content.ReadAsStringAsync();
452452
var respJson = JsonSerializer.Deserialize<Dictionary<string, object>>(jsonStr);
453-
var pageData = JsonSerializer.Deserialize<List<Dictionary<string, string>>>(respJson["results"].ToString());
454-
455-
foreach (var row in pageData)
456-
{
457-
if (!cache.Contains(row["content"]))
458-
{
459-
cache.Add(row["content"]);
460-
results.Add(row);
461-
if (maxResults.HasValue && results.Count >= maxResults.Value) return results;
462-
}
463-
}
464-
453+
var pageData = JsonSerializer.Deserialize<List<VideoSearchItem>>(respJson["results"].ToString());
454+
results.AddRange(pageData);
465455
string next = respJson.ContainsKey("next") ? respJson["next"].ToString() : null;
466456
if (string.IsNullOrEmpty(next) || !maxResults.HasValue) return results;
467457
payload["s"] = next.Split("s=")[1].Split("&")[0];

src/DuckDuckGoDotNet/Search/SearchItem.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,49 @@ public class NewsSearchItem : BaseMediaSearchItem
3636
public DateTimeOffset Date { get; init; }
3737
[JsonPropertyName("body")]
3838
public string Body { get; init; }
39+
}
40+
public class VideoSearchItem : BaseSearchItem
41+
{
42+
[JsonPropertyName("content")]
43+
public string Content { get; set; }
44+
[JsonPropertyName("description")]
45+
public string Description { get; set; }
46+
[JsonPropertyName("duration")]
47+
public string Duration { get; set; }
48+
[JsonPropertyName("embed_html")]
49+
public string EmbedHtml { get; set; }
50+
[JsonPropertyName("embed_url")]
51+
public string EmbedUrl { get; set; }
52+
[JsonPropertyName("image_token")]
53+
public string ImageToken { get; set; }
54+
[JsonPropertyName("provider")]
55+
public string Provider { get; set; }
56+
[JsonPropertyName("published")]
57+
public DateTime Published { get; set; }
58+
[JsonPropertyName("publisher")]
59+
public string Publisher { get; set; }
60+
[JsonPropertyName("statistics")]
61+
public Statistics Statistics { get; set; }
62+
[JsonPropertyName("uploader")]
63+
public string Uploader { get; set; }
64+
[JsonPropertyName("images")]
65+
public Images Images { get; set; }
66+
}
67+
68+
public class Images
69+
{
70+
[JsonPropertyName("large")]
71+
public Uri Large { get; set; }
72+
[JsonPropertyName("medium")]
73+
public Uri Medium { get; set; }
74+
[JsonPropertyName("motion")]
75+
public Uri Motion { get; set; }
76+
[JsonPropertyName("small")]
77+
public Uri Small { get; set; }
78+
}
79+
80+
public class Statistics
81+
{
82+
[JsonPropertyName("viewCount")]
83+
public int? ViewCount { get; set; }
3984
}

0 commit comments

Comments
 (0)