Example #1
0
        public static async Task <KeyValuePair <string, string> > GetSummary(string url, bool debug = false)
        {
            try
            {
                string id = url;

                if (Cache.Get(id) != null)
                {
                    return(new KeyValuePair <string, string>(url, Cache.Get(id).Content + "(cache hit)"));
                }

                IResolver resolver       = null;
                bool      valid_to_cache = true;

                for (int i = 0; i < Resolvers.Count; i++)
                {
                    resolver = Resolvers[i];

                    try
                    {
                        if (resolver == null || !resolver.Matches(url))
                        {
                            continue;
                        }

                        if (!resolver.Ready(url))
                        {
                            Console.WriteLine("Resolver {0} isn't ready yet, not caching", resolver.Name);
                            valid_to_cache = false;
                            continue;
                        }

                        id = resolver.GetCacheID(url);

                        if (Cache.Get(id) != null)
                        {
                            return(new KeyValuePair <string, string>(url, Cache.Get(id).Content + "(cache hit)"));
                        }

                        string summary = resolver.GetSummary(url);

                        if (summary != "-")
                        {
                            if (valid_to_cache)
                            {
                                Cache.Add(id, summary, TimedCache.DefaultExpiry);
                            }
                            return(new KeyValuePair <string, string>(id, summary));
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }
                }

                HttpClient httpClient = new HttpClient();

                HttpRequestMessage request =
                    new HttpRequestMessage(HttpMethod.Head,
                                           new Uri(url));

                request.Headers.Add("User-Agent", Config.GetString("title.useragent"));

                HttpResponseMessage response =
                    await httpClient.SendAsync(request);

                int length = 0;

                if (response.Content.Headers.Contains("Content-Length"))
                {
                    length = int.Parse(response.Content.Headers.GetValues("Content-Length").First());

                    if (length > 10000000)
                    {
                        return(new KeyValuePair <string, string>("", "File too big!"));
                    }
                }

                string ext = "";

                if (url.Contains("."))
                {
                    ext = "." + url.Split('.').Last();
                }

                if (ext.Length > 5)
                {
                    ext = "";
                }

                string mime     = GetMimeType(response);
                var    type     = GetType(mime);
                var    ext_type = GetTypeByExt(ext);

                if ((type == LinkType.Generic || type != ext_type) && ext_type != LinkType.Generic)
                {
                    type = ext_type;
                }

                switch (type)
                {
                case LinkType.Html:
                    string title = "";

                    title = GetTitle(url);

                    if (valid_to_cache)
                    {
                        Cache.Add(url, title, TimedCache.DefaultExpiry);
                    }

                    return(new KeyValuePair <string, string>(url, title));

                case LinkType.Image:
                {
                    string msg = "";

                    var resp = await httpClient.GetAsync(url);

                    byte[] data = await resp.Content.ReadAsByteArrayAsync();

                    MemoryStream ms = new MemoryStream(data);

                    Bitmap bmp = new Bitmap(ms);

                    string hash    = GetHash(data);
                    string imgtype = mime.Split('/')[1].ToUpper();

                    if (length != 0)
                    {
                        msg = string.Format("11{0} image({1}, {2}x{3})", imgtype, GetBoldLength(length), bmp.Width, bmp.Height);
                    }
                    else
                    {
                        msg = string.Format("11{0} image({1}x{2})", imgtype, bmp.Width, bmp.Height);
                    }

                    if (valid_to_cache)
                    {
                        Cache.Add(url, msg, TimedCache.DefaultExpiry);
                    }
                    return(new KeyValuePair <string, string>(url, msg));
                }

                case LinkType.Video:
                case LinkType.Audio:
                //{
                //    var resp = await httpClient.GetAsync(url);
                //    string temp = Path.GetTempFileName();
                //    byte[] data = await resp.Content.ReadAsByteArrayAsync();
                //    File.WriteAllBytes(temp, data);
                //    string ret = string.Format(MediaInfo.GetMediaInfo(temp), GetBoldLength(length));
                //    File.Delete(temp);

                //    Cache.Add(url, ret, TimedCache.DefaultExpiry);
                //    return new KeyValuePair<string, string>(url, ret);
                //}
                case LinkType.Generic:
                    if (length != 0)
                    {
                        string ret = "";

                        ret = string.Format("11{0}, {1}", mime, GetBoldLength(length));

                        if (valid_to_cache)
                        {
                            Cache.Add(url, ret, TimedCache.DefaultExpiry);
                        }

                        return(new KeyValuePair <string, string>(url, ret));
                    }
                    break;

                default:
                    break;
                }

                return(new KeyValuePair <string, string>("", "-"));
            }
            catch
            {
                throw;
            }
        }