-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
57 lines (48 loc) · 1.59 KB
/
Program.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
using PuppeteerSharp;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddOutputCache();
builder.Services.AddCors();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(x =>
{
x.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
x.RoutePrefix = "";
});
}
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseOutputCache();
app.MapGet("/photos/{hashtag}", async (string hashtag) =>
{
var urls = await GetImageUrls(hashtag).ToListAsync();
return urls;
})
.WithName("GetImageUrls")
.CacheOutput(x => x.Expire(TimeSpan.FromMinutes(1)))
.WithOpenApi();
app.Run();
return;
async IAsyncEnumerable<string> GetImageUrls(string hashtag)
{
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true });
await using var page = await browser.NewPageAsync();
await page.GoToAsync($"https://www.instagram.com/explore/tags/{hashtag}/", WaitUntilNavigation.Networkidle0);
await page.WaitForXPathAsync("//img");
var images = await page.XPathAsync("//img");
foreach (var image in images)
{
var src = await image.EvaluateFunctionAsync<string>("e => e.src");
if (!src.Contains("http"))
{
continue;
}
yield return src;
}
}