-
Notifications
You must be signed in to change notification settings - Fork 3
/
ContentQueueTrigger.cs
63 lines (57 loc) · 2.37 KB
/
ContentQueueTrigger.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
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Net.Http;
using Newtonsoft.Json;
using HtmlAgilityPack;
using System.Web;
using Microsoft.Azure.Storage.Blob;
namespace HvidevoldDevelopmentENK.GetPixelArt
{
public static class ContentQueueTrigger
{
static readonly HttpClient client = new HttpClient();
[FunctionName("ContentQueueTrigger")]
public static async Task Run(
[QueueTrigger("contentqueue", Connection = "AzureWebJobsStorage")] string page,
[Blob("opengameart/{queueTrigger}.html")] CloudBlockBlob blob,
[Queue("filequeue"), StorageAccount("AzureWebJobsStorage")] ICollector<string> msg,
ILogger log)
{
log.LogInformation($"C# ContentQueueTrigger function processed page {page}");
string responseBody = null;
try
{
responseBody = await Common.ReadURIOrCache(blob, Common.BaseURI + page, client);
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(responseBody);
var htmlBody = htmlDoc.DocumentNode.SelectSingleNode("//body");
foreach (var nNode in htmlBody.Descendants("a"))
{
if (nNode.NodeType == HtmlNodeType.Element &&
nNode.Attributes["href"] != null &&
nNode.Attributes["href"].Value.Contains("/default/files/"))
{
msg.Add(HttpUtility.HtmlDecode(nNode.Attributes["href"].Value.Replace(Common.FileURI, "")));
}
}
}
catch(HttpRequestException e)
{
log.LogError("\nException Caught!");
log.LogError("Message :{0} ",e.Message);
log.LogError("Stack :{0}", e.StackTrace.ToString());
}
catch (NullReferenceException e) {
log.LogError("\nException Caught!");
log.LogError("Message :{0} ", e.Message);
log.LogError("Stack :{0}", e.StackTrace.ToString());
}
}
}
}