-
Notifications
You must be signed in to change notification settings - Fork 3
/
Utils.cs
73 lines (56 loc) · 2.29 KB
/
Utils.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
using HtmlAgilityPack;
using System.Linq;
using UrlNotes.Models;
using System;
namespace UrlNotes
{
public class Utils
{
public static void Unfurl<T>(ref T item) where T:Item
{
try
{
string src = string.Empty;
var doc = new HtmlDocument();
try
{
var h = new HtmlWeb();
doc = h.Load(item.Url);
}
catch
{
}
var metaTags = doc.DocumentNode.SelectNodes("//meta");
var title = (from x in metaTags
where (x.Attributes["property"] != null && x.Attributes["property"].Value == "og:title")
select x).FirstOrDefault().Attributes["content"].Value;
item.Name = title;
var keywords = (from x in metaTags
where (x.Attributes["name"] != null && x.Attributes["name"].Value == "keywords")
select x);
if (keywords.Count() > 0)
{
var words = keywords.FirstOrDefault().Attributes["content"].Value.Split(',');
item.Keywords = words.Select(x => new Keyword() { name = x }).ToList();
}
if (item.GetType() == typeof(Video))
{
var i = item as Video;
var comments = (from x in metaTags
where (x.Attributes["name"] != null && x.Attributes["name"].Value == "CommentCount")
select x).FirstOrDefault().Attributes["content"].Value;
int count;
i.CommentCount = int.TryParse(comments, out count) ? count : 0;
var img = (from x in metaTags
where (x.Attributes["property"] != null && x.Attributes["property"].Value == "og:image")
select x).FirstOrDefault().Attributes["content"].Value;
i.Screencap = new Uri(img);
}
}
catch(Exception ex)
{
throw ex;
}
}
}
}