PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/worm/Utilities.cs

https://gitlab.com/SirCmpwn/worm
C# | 125 lines | 120 code | 5 blank | 0 comment | 17 complexity | e94b2b77be5c3c61d8d9caab952b540c MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using Newtonsoft.Json.Linq;
  6. using HtmlAgilityPack;
  7. using System.Xml;
  8. using System.Xml.Linq;
  9. using System.Linq;
  10. namespace worm
  11. {
  12. partial class MainClass
  13. {
  14. static string FetchPageTitle(string url)
  15. {
  16. try
  17. {
  18. WebClient wc = new WebClient(); // I'm sorry, okay?
  19. StreamReader sr = new StreamReader(wc.OpenRead(url));
  20. string data = sr.ReadToEnd();
  21. sr.Close();
  22. HtmlDocument hDocument = new HtmlDocument();
  23. hDocument.LoadHtml(data);
  24. var title = hDocument.DocumentNode.Descendants("title");
  25. if (title != null)
  26. {
  27. if (title.Count() > 0)
  28. {
  29. string text = title.First().InnerText;
  30. text = text.Replace("\n", "").Replace("\r", "").Trim();
  31. if (text.Length < 100)
  32. return WebUtility.HtmlDecode(HtmlRemoval.StripTagsRegexCompiled(text));
  33. }
  34. }
  35. }
  36. catch { return null; }
  37. return null;
  38. }
  39. class Video
  40. {
  41. public string Title, Author;
  42. public int Views, Likes, Dislikes;
  43. public TimeSpan Duration;
  44. public bool RegionLocked, HD, CommentsEnabled, RatingsEnabled;
  45. public string Stars;
  46. public Uri VideoUri;
  47. }
  48. private static Video GetYoutubeVideo(string vid)
  49. {
  50. try
  51. {
  52. WebClient client = new WebClient();
  53. var sr = new StreamReader(client.OpenRead(string.Format("http://gdata.youtube.com/feeds/api/videos/{0}?v=2", Uri.EscapeUriString(vid))));
  54. string xml = sr.ReadToEnd();
  55. XDocument document = XDocument.Parse(xml);
  56. XNamespace media = XNamespace.Get("http://search.yahoo.com/mrss/");
  57. XNamespace youtube = XNamespace.Get("http://gdata.youtube.com/schemas/2007");
  58. XNamespace root = XNamespace.Get("http://www.w3.org/2005/Atom");
  59. XNamespace googleData = XNamespace.Get("http://schemas.google.com/g/2005");
  60. Video video = new Video();
  61. video.Title = document.Root.Element(root + "title").Value;
  62. video.Author = document.Root.Element(root + "author").Element(root + "name").Value;
  63. video.CommentsEnabled = document.Root.Elements(youtube + "accessControl").Where(e =>
  64. e.Attribute("action").Value == "comment").First().Attribute("permission").Value == "allowed";
  65. video.RatingsEnabled = document.Root.Elements(youtube + "accessControl").Where(e =>
  66. e.Attribute("action").Value == "rate").First().Attribute("permission").Value == "allowed";
  67. if (video.RatingsEnabled)
  68. {
  69. video.Likes = int.Parse(document.Root.Element(youtube + "rating").Attribute("numLikes").Value);
  70. video.Dislikes = int.Parse(document.Root.Element(youtube + "rating").Attribute("numDislikes").Value);
  71. }
  72. video.Views = int.Parse(document.Root.Element(youtube + "statistics").Attribute("viewCount").Value);
  73. video.Duration = TimeSpan.FromSeconds(
  74. double.Parse(document.Root.Element(media + "group").Element(youtube + "duration").Attribute("seconds").Value));
  75. video.RegionLocked = document.Root.Element(media + "group").Element(media + "restriction") != null;
  76. video.VideoUri = new Uri("http://youtu.be/" + vid);
  77. video.HD = document.Root.Element(youtube + "hd") != null;
  78. if (video.RatingsEnabled)
  79. {
  80. video.Stars = "\u000303";
  81. int starCount = (int)Math.Round(double.Parse(document.Root.Element(googleData + "rating").Attribute("average").Value));
  82. for (int i = 0; i < 5; i++)
  83. {
  84. if (i < starCount)
  85. video.Stars += "★";
  86. else if (i == starCount)
  87. video.Stars += "\u000315☆";
  88. else
  89. video.Stars += "☆";
  90. }
  91. video.Stars += "\u000f";
  92. }
  93. return video;
  94. }
  95. catch
  96. {
  97. return null;
  98. }
  99. }
  100. static List<string> DoGoogleSearch(string terms)
  101. {
  102. List<string> results = new List<string>();
  103. try
  104. {
  105. WebClient client = new WebClient();
  106. StreamReader sr = new StreamReader(client.OpenRead("http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" + Uri.EscapeUriString(terms)));
  107. string json = sr.ReadToEnd();
  108. sr.Close();
  109. JObject jobject = JObject.Parse(json);
  110. foreach (var result in jobject["responseData"]["results"])
  111. results.Add(WebUtility.HtmlDecode(HtmlRemoval.StripTagsRegexCompiled(Uri.UnescapeDataString(result["title"].Value<string>())) +
  112. " " + Uri.UnescapeDataString(result["url"].Value<string>())));
  113. }
  114. catch (Exception)
  115. {
  116. }
  117. return results;
  118. }
  119. }
  120. }