-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulateLinks.csx
More file actions
159 lines (136 loc) · 4.72 KB
/
Copy pathPopulateLinks.csx
File metadata and controls
159 lines (136 loc) · 4.72 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#r "System.Web"
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;
using System.Linq;
public static void Run(TimerInfo ShouldBe615, TraceWriter log)
{
log.Info($"PopulateLinks function executed at: {DateTime.Now}");
// query DB for topics
DocumentClient client = new DocumentClient(new Uri(Environment.GetEnvironmentVariable("DOCDB_URL")),
Environment.GetEnvironmentVariable("DOCDB_KEY"));
// create some topics
//PopulateTopics(client);
// Set some common query options
FeedOptions queryOptions = new FeedOptions { MaxItemCount = -1 };
// Here we find the active topics
IQueryable<Topic> topics = client.CreateDocumentQuery<Topic>(
UriFactory.CreateDocumentCollectionUri("NewsMessages", "Topics"), queryOptions)
.Where(f => f.Active == true);
// loop through the topics
foreach (Topic tp in topics)
{
log.Info($"creating links for this topic: {tp.Title}");
createAndStoreLinks(client, tp);
// call the cog services API for each topic
// store results for each topic
}
log.Info($"PopulateLinks function finished at: {DateTime.Now}");
}
public static void createAndStoreLinks(DocumentClient client, Topic topic)
{
DateTime now = DateTime.Now;
var linksUri = UriFactory.CreateDocumentCollectionUri("NewsMessages", "Links");
// call the rest API
var httpClient = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", Environment.GetEnvironmentVariable("SEARCH_KEY"));
// Request parameters
queryString["q"] = topic.Terms;
queryString["count"] = "3";
queryString["offset"] = "0";
queryString["freshness"] = "Day";
queryString["mkt"] = "en-us";
queryString["safeSearch"] = "Strict";
var uri = "https://api.cognitive.microsoft.com/bing/v5.0/news/search?" + queryString;
var response = httpClient.GetAsync(uri).Result;
var contents = response.Content.ReadAsStringAsync().Result;
dynamic json = JsonConvert.DeserializeObject(contents);
dynamic values = json.value;
foreach (var v in values)
{
string url = v.url;
string lk = url.Substring(url.LastIndexOf("http"), (url.LastIndexOf("&p=DevEx") - url.LastIndexOf("http")));
Link link = new Link{Created=now, Active=true};
link.URL = lk;
link.Published = v.datePublished;
link.Name = v.name;
link.Description = v.description;
link.TopicTitle = topic.Title;
link.TopicTerms = topic.Terms;
client.CreateDocumentAsync(linksUri, link);
}
}
public class Topic
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
public string Title { get; set; }
public string Terms { get; set; }
public bool Active { get; set; }
public DateTime Created { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public class Link
{
public string URL { get; set; }
public DateTime Published { get; set; }
public string TopicTitle { get; set; }
public string TopicTerms { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public bool Active { get; set; }
public DateTime Created { get; set; }
public override string ToString()
{
return JsonConvert.SerializeObject(this);
}
}
public static void PopulateTopics(DocumentClient client)
{
var topicsUri = UriFactory.CreateDocumentCollectionUri("NewsMessages", "Topics");
Topic datasci = new Topic{
Id = "1",
Title = "Data Science",
Terms = "'data science' or 'machine learning'",
Active = true,
Created = DateTime.Now
};
client.CreateDocumentAsync(topicsUri, datasci);
Topic innov = new Topic{
Id = "2",
Title = "Business Innovation",
Terms = "'business innovation'",
Active = true,
Created = DateTime.Now
};
client.CreateDocumentAsync(topicsUri, innov);
Topic ns = new Topic{
Id = "3",
Title = "Network Security",
Terms = "'network security'",
Active = true,
Created = DateTime.Now
};
client.CreateDocumentAsync(topicsUri, ns);
Topic ai = new Topic{
Id = "4",
Title = "AI",
Terms = "AI or 'deep learning'",
Active = true,
Created = DateTime.Now
};
client.CreateDocumentAsync(topicsUri, ai);
}