-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
134 lines (106 loc) · 5.52 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
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
using Curiosity.Library;
using System;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace codeproject_search
{
class Program
{
static async Task Main(string[] args)
{
ForceInvariantCultureAndUTF8Output();
var (server, token) = (args[0], args[1]);
using (var graph = Graph.Connect(server, token, "CodeProject"))
{
await graph.CreateNodeSchemaAsync<Article>();
await graph.CreateNodeSchemaAsync<Author>();
await graph.CreateNodeSchemaAsync<Tag>();
await graph.CreateNodeSchemaAsync<Category>();
await graph.CreateNodeSchemaAsync<Subcategory>();
await graph.CreateEdgeSchemaAsync(Edges.AuthorOf, Edges.HasAuthor,
Edges.CategoryOf, Edges.HasCategory,
Edges.SubcategoryOf, Edges.HasSubcategory,
Edges.TagOf, Edges.HasTag);
await IngestCodeProject(graph);
await graph.CommitPendingAsync();
}
Console.WriteLine("Done !");
}
private static async Task IngestCodeProject(Graph graph)
{
Console.WriteLine("Reading site map...");
var siteMap = await CodeProject.GetPage("https://www.codeproject.com/script/Content/SiteMap.aspx");
var siteMapLinks = CodeProject.GetLinks("https://www.codeproject.com/script/Content/SiteMap.aspx", siteMap);
Console.WriteLine("Reading categories...");
var (subcat2cat, catNames) = await CodeProject.GetCategories();
foreach (var subcategoryUrl in siteMapLinks.Where(l => l.StartsWith("https://www.codeproject.com/KB/")))
{
var subcategoryName = catNames[subcategoryUrl];
Console.WriteLine($"[{subcategoryName}] - Start processing");
var subcategoryNode = graph.AddOrUpdate(new Subcategory() { Name = subcategoryName, Url = subcategoryUrl });
var categoryUrl = subcat2cat[subcategoryUrl];
var categoryName = catNames[categoryUrl];
var categoryNode = graph.AddOrUpdate(new Category() { Name = categoryName, Url = categoryUrl });
graph.Link(categoryNode, subcategoryNode, Edges.HasSubcategory, Edges.SubcategoryOf);
var subcategoryPage = await CodeProject.GetPage(subcategoryUrl);
var articlesLinks = CodeProject.GetLinks(subcategoryUrl, subcategoryPage)
.Where(u => u.StartsWith("https://www.codeproject.com/Articles/"));
foreach (var articleLink in articlesLinks)
{
Console.WriteLine($"[{categoryName} > {subcategoryName}] now processing {articleLink}");
var article = await CodeProject.GetPage(articleLink);
var tags = CodeProject.GetTags(articleLink, article);
var stats = CodeProject.GetStats(article);
var authors = CodeProject.GetAuthor(articleLink, article);
var content = CodeProject.GetContent(articleLink, article);
var date = CodeProject.GetLastUpdated(article);
var articleNode = graph.AddOrUpdate(new Article()
{
Url = articleLink,
Bookmarks = stats.bookmarked,
Views = stats.views,
Downloads = stats.downloads,
Description = content.description,
Title = content.title,
Text = content.text,
Html = content.html,
Timestamp = date
});
foreach (var author in authors)
{
var authorNode = graph.AddOrUpdate(new Author() { Name = author.name, Url = author.url });
graph.Link(articleNode, authorNode, Edges.HasAuthor, Edges.AuthorOf);
}
foreach (var tag in tags)
{
var tagNode = graph.AddOrUpdate(new Tag() { Name = tag.tag, Url = tag.url });
graph.Link(articleNode, tagNode, Edges.HasTag, Edges.TagOf);
}
graph.Link(articleNode, subcategoryNode, Edges.HasSubcategory, Edges.SubcategoryOf);
graph.Link(articleNode, categoryNode, Edges.HasCategory, Edges.CategoryOf);
}
Console.WriteLine($"[{categoryName} > {subcategoryName}] - Done! \n\n");
}
}
static void ForceInvariantCultureAndUTF8Output()
{
if (Environment.UserInteractive)
{
try
{
Console.OutputEncoding = Encoding.UTF8;
Console.InputEncoding = Encoding.UTF8;
}
catch
{
//This might throw if not running on a console, ignore as we don't care in that case
}
}
Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
}
}
}