Exemplo n.º 1
0
        public static T ExecuteGetSync <T>(string url)
        {
            using (var wc = new TimedWebClient()) {
                var str = wc.DownloadString(new Uri(url));

                return(JsonConvert.DeserializeObject <T>(str));
            }
        }
Exemplo n.º 2
0
 public static string ExecuteGetAsStringSync(string url, int?timeOut = null)
 {
     using (var wc = new TimedWebClient()) {
         if (timeOut.HasValue)
         {
             wc.Timeout = timeOut.Value;
         }
         return(wc.DownloadString(new Uri(url)));
     }
 }
Exemplo n.º 3
0
        void QueueNewAnnouncement(HtmlNodeCollection repoNodes)
        {
            string title            = repoNodes["title"].InnerText;
            var    separator        = title.IndexOf('/');
            var    repoName         = title.Substring(separator + 1);
            var    author           = repoNodes["author"].ChildNodes["name"].InnerText;
            var    publishTime      = repoNodes["published"].InnerText;
            var    rawLink          = repoNodes["link"].Attributes["href"].Value;
            var    compareSeparator = rawLink.LastIndexOf('/');
            var    link             = rawLink.Substring(0, compareSeparator + 1);
            var    beginCommit      = rawLink.Substring(compareSeparator + 1, 10);
            var    endCommit        = rawLink.Substring(compareSeparator + 1 + 10 + 3, 10);
            var    timestamp        = new DateTime
                                      (
                int.Parse(publishTime.Substring(0, 4)),
                int.Parse(publishTime.Substring(5, 2)),
                int.Parse(publishTime.Substring(8, 2)),
                int.Parse(publishTime.Substring(11, 2)),
                int.Parse(publishTime.Substring(14, 2)),
                int.Parse(publishTime.Substring(17, 2))
                                      );

            var client     = new TimedWebClient();
            var commitPage = client.DownloadString(rawLink);
            var doc        = new HtmlDocument();

            doc.LoadHtml(commitPage);
            client.Dispose();

            var entry         = doc.DocumentNode.SelectNodes("//span[@class=\"num\"]");
            var rawNumCommits = entry[0].InnerText;

            rawNumCommits = new string(rawNumCommits.Where(char.IsDigit).ToArray());
            int numCommits = int.Parse(rawNumCommits);


            if (_subscribedProjects.Contains(repoName))
            {
                lock (_queuedAnnouncements){
                    _queuedAnnouncements.Add
                        (new QueuedAnnouncement
                        (
                            author,
                            link,
                            repoName,
                            timestamp,
                            beginCommit,
                            endCommit,
                            numCommits
                        )
                        );
                }
            }
        }
Exemplo n.º 4
0
        public static int GetUniqueID()
        {
            string commentForCodeLurkers = "This is to find the public IPv4 address of the client to use it as unique ID for Analytics";

            try
            {
                var client    = new TimedWebClient();
                var ipAddress = client.DownloadString(ipService);
                ipAddress = Regex.Match(ipAddress, @"([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})").Groups[1].Value;

                return(BitConverter.ToInt32(IPAddress.Parse(ipAddress).GetAddressBytes(), 0));
            }
            catch
            {
                Random rnd = new Random();
                return(rnd.Next(int.MinValue, int.MaxValue));
            }
        }
        private async Task <string> GetResponse(string url)
        {
            using (var client = new TimedWebClient(10000))
            {
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

                try
                {
                    return(client.DownloadString(url));
                }
                catch (WebException ex)
                {
                    await _log.WriteInfoAsync(nameof(GetResponse),
                                              $"erl: {url}",
                                              $"Error. Status Code: {ex.Status}. Stack: {ex.ToString()}");

                    return(null);
                }
            }
        }
Exemplo n.º 6
0
        void RefreshRepos()
        {
            var client = new TimedWebClient();
            var rssStr = client.DownloadString(_rssUrl);

            client.Dispose();

            var rssDocument = new HtmlDocument();

            rssDocument.LoadHtml(rssStr);

            var entries = rssDocument.DocumentNode.SelectNodes("//entry");

            foreach (var entry in entries)
            {
                var childNodes = entry.ChildNodes;
                if (childNodes["id"].InnerText.Contains("PushEvent"))
                {
                    QueueNewAnnouncement(childNodes);
                }
            }
        }
Exemplo n.º 7
0
        internal static Tuple <int, string> CheckAvailability(string url, string prefix)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(url) ||
                    !Uri.TryCreate(url, UriKind.Absolute, out var uri) ||
                    !Url.IsValid(url))
                {
                    return(new Tuple <int, string>(02_0005, url)); // Invalid url
                }

                if (!PingHost(uri.Host, out var time))
                {
                    return(new Tuple <int, string>(02_0006, url)); // cannot ping
                }
                using (var client = new TimedWebClient())
                {
                    try
                    {
                        var s = client.DownloadString(Url.Combine(url, prefix, ""));
                        if (!s.StartsWith("Milvaneth Api Service"))
                        {
                            return(new Tuple <int, string>(02_0004, url)); // not valid service
                        }
                    }
                    catch
                    {
                        return(new Tuple <int, string>(02_0004, url));
                    }
                }

                return(new Tuple <int, string>(time, url));
            }
            catch
            {
                return(new Tuple <int, string>(02_0001, url)); // other exception
            }
        }
Exemplo n.º 8
0
        void RefreshRepos()
        {
            var client = new TimedWebClient();
            var rssStr = client.DownloadString(_rssUrl);
            client.Dispose();

            var rssDocument = new HtmlDocument();
            rssDocument.LoadHtml(rssStr);

            var entries = rssDocument.DocumentNode.SelectNodes("//entry");

            foreach (var entry in entries){
                var childNodes = entry.ChildNodes;
                if (childNodes["id"].InnerText.Contains("PushEvent")){
                    QueueNewAnnouncement(childNodes);
                }
            }
        }
Exemplo n.º 9
0
        void QueueNewAnnouncement(HtmlNodeCollection repoNodes)
        {
            string title = repoNodes["title"].InnerText;
            var separator = title.IndexOf('/');
            var repoName = title.Substring(separator + 1);
            var author = repoNodes["author"].ChildNodes["name"].InnerText;
            var publishTime = repoNodes["published"].InnerText;
            var rawLink = repoNodes["link"].Attributes["href"].Value;
            var compareSeparator = rawLink.LastIndexOf('/');
            var link = rawLink.Substring(0, compareSeparator + 1);
            var beginCommit = rawLink.Substring(compareSeparator + 1, 10);
            var endCommit = rawLink.Substring(compareSeparator + 1 + 10 + 3, 10);
            var timestamp = new DateTime
                (
                int.Parse(publishTime.Substring(0, 4)),
                int.Parse(publishTime.Substring(5, 2)),
                int.Parse(publishTime.Substring(8, 2)),
                int.Parse(publishTime.Substring(11, 2)),
                int.Parse(publishTime.Substring(14, 2)),
                int.Parse(publishTime.Substring(17, 2))
                );

            var client = new TimedWebClient();
            var commitPage = client.DownloadString(rawLink);
            var doc = new HtmlDocument();
            doc.LoadHtml(commitPage);
            client.Dispose();

            var entry = doc.DocumentNode.SelectNodes("//span[@class=\"num\"]");
            var rawNumCommits = entry[0].InnerText;
            rawNumCommits = new string(rawNumCommits.Where(char.IsDigit).ToArray());
            int numCommits = int.Parse(rawNumCommits);

            if (_subscribedProjects.Contains(repoName)){
                lock (_queuedAnnouncements){
                    _queuedAnnouncements.Add
                        (new QueuedAnnouncement
                            (
                            author,
                            link,
                            repoName,
                            timestamp,
                            beginCommit,
                            endCommit,
                            numCommits
                            )
                        );
                }
            }
        }
Exemplo n.º 10
0
        protected static string DownloadString(string url, int timeout = TimedWebClient.DEFAULT_TIMEOUT)
        {
            var client = new TimedWebClient(timeout);

            return(client.DownloadString(url));
        }