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