Exemplo n.º 1
0
        private async Task <string> Fetch(string id)
        {
            string s;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://api.gyazo.com/api/oembed/?url="
                                 + Uri.EscapeDataString("http://gyazo.com/" + id);
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    s = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, s, null);
            return(JSON.Deserialize <ApiResponse>(s).url);
        }
Exemplo n.º 2
0
        private async Task <CacheItem> Fetch(string id)
        {
            string json;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://api.dailymotion.com/video/" + id + "?fields=" + fields;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    switch (res.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                    case HttpStatusCode.NotFound:
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    json = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, json, null);
            return(JSON.Deserialize <CacheItem>(json));
        }
Exemplo n.º 3
0
        private async Task <string> Fetch(string id)
        {
            IHtmlDocument document;

            using (var hc = new HttpClient())
            {
                var requestUri = "http://www.meshimazu.net/posts/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    document = await res.Content.ReadAsHtmlDocument().ConfigureAwait(false);
                }
            }

            return(document.Body.Descendents <IHtmlImageElement>()
                   .First(img => img.ClassName == "testes")
                   .GetAttribute("src"));
        }
Exemplo n.º 4
0
        private async Task <string[]> GetAlbumThumbnails(string t, string id)
        {
            string content;

            using (var hc = new HttpClient())
            {
                var requestUri = "http://opa.cig2.imagegateway.net/s/" + t + "album/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);
                var res = await hc.GetAsync(requestUri).ConfigureAwait(false);

                content = await res.Content.ReadAsStringAsync().ConfigureAwait(false);

                CheckResponseError(res, content);
            }

            return(new HtmlParser().Parse(content)
                   .GetElementById("albumAdd1")
                   .ChildNodes.OfType <IHtmlListItemElement>()
                   .Select(li =>
            {
                var style = li.ChildNodes.OfType <IHtmlAnchorElement>().First()
                            .ChildNodes.OfType <IHtmlDivElement>().First()
                            .GetAttribute("style");
                // background-image: url(here);
                return style.Substring(22, style.Length - 24);
            })
                   .ToArray());
        }
Exemplo n.º 5
0
        private async Task <T> CallApi <T>(string method, string query)
            where T : FlickrResponseBase
        {
            string json;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://api.flickr.com/services/rest/?format=json&nojsoncallback=1&"
                                 + "&api_key=" + this._options.FlickrApiKey
                                 + "&method=" + method
                                 + "&" + query;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                json = await hc.GetStringAsync(requestUri).ConfigureAwait(false);
            }

            ResolverUtils.HttpResponseMessage(this._logger, json, null);
            var result = JSON.Deserialize <T>(json, Options.IncludeInherited);

            if (result.stat != "ok")
            {
                throw new FlickrException(result.code, result.message);
            }

            return(result);
        }
Exemplo n.º 6
0
        private async Task <string> Fetch(string id)
        {
            string s;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://api.500px.com/v1/photos/" + id
                                 + "?image_size=5&consumer_key=" + this._options._500pxConsumerKey;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();

                    s = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, s, null);
            return(JSON.Deserialize <ApiResponse>(s).photo.image_url);
        }
Exemplo n.º 7
0
        private async Task <string> GetImage(string t, string id)
        {
            // フルサイズ画像は毎回 URI が変わるのでキャッシュしたら死ぬ可能性
            // よってアルバムのサムネイル一覧から取得

            string content;

            using (var hc = new HttpClient())
            {
                var requestUri = "http://opa.cig2.imagegateway.net/s/" + t + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);
                var res = await hc.GetAsync(requestUri).ConfigureAwait(false);

                content = await res.Content.ReadAsStringAsync().ConfigureAwait(false);

                CheckResponseError(res, content);
            }

            var style = new HtmlParser().Parse(content)
                        .GetElementById("itemList")
                        .ChildNodes.OfType <IHtmlListItemElement>()
                        .First(x => x.ClassList.Contains("itemOver"))
                        .ChildNodes.OfType <IHtmlSpanElement>().First()
                        .ChildNodes.OfType <IHtmlSpanElement>().First()
                        .GetAttribute("style");

            // background-image:url(here);
            return(style.Substring(21, style.Length - 23));
        }
Exemplo n.º 8
0
        private async Task <IHtmlDocument> DownloadHtml(string id)
        {
            IHtmlDocument document;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://www.dropbox.com/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    document = await res.Content.ReadAsHtmlDocument().ConfigureAwait(false);
                }
            }

            // 200 で "検索している情報が見つかりません。" と返してくる
            if (document.GetElementsByClassName("err").Length > 0)
            {
                throw new ImageNotFoundException();
            }

            return(document);
        }
Exemplo n.º 9
0
        private async Task <CacheItem> Fetch(string id)
        {
            string json;

            using (var hc = new HttpClient())
            {
                var requestUri = "https://api.instagram.com/v1/media/shortcode/" + id
                                 + "?access_token=" + this._options.InstagramAccessToken;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    switch (res.StatusCode)
                    {
                    case HttpStatusCode.BadRequest:
                        throw new ImageNotFoundException();

                    case HttpStatusCode.NotFound:
                        return(null);    // アクセス不可能
                    }

                    res.EnsureSuccessStatusCode();
                    json = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, json, null);
            return(JSON.Deserialize <Response>(json).data);
        }
Exemplo n.º 10
0
        private async Task <CacheItem> Fetch(string uri)
        {
            string s;

            using (var hc = new HttpClient())
            {
                hc.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                ResolverUtils.RequestingMessage(this._logger, uri, null);

                using (var res = await hc.GetAsync(uri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    s = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, s, null);
            return(JSON.Deserialize <CacheItem>(s));
        }
Exemplo n.º 11
0
        private async Task <string> Fetch(string id)
        {
            using (var hc = new HttpClient(new HttpClientHandler {
                AllowAutoRedirect = false
            }))
            {
                var requestUri = "http://cameran.in/p/v1/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    switch (res.StatusCode)
                    {
                    case HttpStatusCode.NotFound:
                    case HttpStatusCode.Found:
                    case HttpStatusCode.SeeOther:
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();

                    return(ResolverUtils.GetOgImage(
                               await res.Content.ReadAsHtmlDocument().ConfigureAwait(false)));
                }
            }
        }
Exemplo n.º 12
0
        private async Task <CacheItem> FetchFile(string id)
        {
            var document = await this.DownloadHtml(id).ConfigureAwait(false);

            // body の class に preview-photo または preview-video があるならば対応
            return(new CacheItem
            {
                Type = document.Body.ClassList
                       .Where(x => x.StartsWith("preview-", StringComparison.Ordinal))
                       .Select(x => x.Substring(8)).SingleOrDefault(),
                OgImage = ResolverUtils.GetOgImage(document)
            });
        }
Exemplo n.º 13
0
        private async Task <CacheItem> Fetch(string username, string id)
        {
            IHtmlDocument document;

            using (var hc = new HttpClient(new HttpClientHandler {
                AllowAutoRedirect = false
            }))
            {
                var requestUri = "http://f.hatena.ne.jp/" + username + "/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    switch (res.StatusCode)
                    {
                    case HttpStatusCode.NotFound:
                    case HttpStatusCode.Found:
                    case HttpStatusCode.SeeOther:
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    document = await res.Content.ReadAsHtmlDocument().ConfigureAwait(false);
                }
            }

            var fotoBody = document.GetElementById("foto-body");
            var img      = fotoBody.GetElementById <IHtmlImageElement>("foto-for-html-tag-" + id);

            // <img id="foto-for-html-tag-" src="" style="display:none;" class="" alt="" title="" />
            if (img == null)
            {
                throw new ImageNotFoundException();
            }

            return(new CacheItem
            {
                Extension = img.GetAttribute("class"),
                IsOriginalAvailable = fotoBody.Descendents <IHtmlImageElement>()
                                      .Any(x => x.GetAttribute("src") == "/images/original.gif")
            });
        }
Exemplo n.º 14
0
        private async Task <CacheItem> Fetch(string uri)
        {
            string json;

            using (var hc = new HttpClient())
            {
                var requestUri = "http://backend.deviantart.com/oembed?url=" + Uri.EscapeDataString(uri);
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    json = await res.Content.ReadAsStringAsync().ConfigureAwait(false);
                }
            }

            ResolverUtils.HttpResponseMessage(this._logger, json, null);
            return(JSON.Deserialize <CacheItem>(json));
        }
Exemplo n.º 15
0
        /**
         * Resolves the given query, returning a QueryResult that contains
         * everything we found while doing the resolution.
         */
        public QueryResult Execute(Domain domain, ResourceRecordType rtype, AddressClass addr_class, bool recursive)
        {
            logger.Trace("Executing query on {0} for type {1} with recursion {2}",
                         domain, rtype, recursive);

            if (zone.IsAuthorityFor(domain))
            {
                var records = zone.Query(domain, rtype, addr_class).ToList();

                // It is possible that there is a CNAME that we should be aware of - in that case, check
                // it and see if we can find one.
                if (records.Count == 0)
                {
                    var cname_records = zone.Query(domain, ResourceRecordType.CANONICAL_NAME, addr_class).ToArray();

                    if (cname_records.Length != 0)
                    {
                        logger.Trace("Authoritative for CNAMEs, re-executing");

                        // In this case, try again with the alias
                        var alias         = ((CNAMEResource)cname_records[0].Resource).Alias;
                        var alias_results = Execute(alias, rtype, addr_class, recursive);

                        // The RFC directs us to return any intermediate CNAMEs, which we do
                        alias_results.Answers.InsertRange(0, cname_records);

                        return(alias_results);
                    }
                }

                var result = new QueryResult();
                result.IsAuthority = true;
                result.FoundAnswer = true;
                result.Answers     = new List <DNSRecord>();
                result.Authority   = new List <DNSRecord>();
                result.Additional  = new List <DNSRecord>();

                result.Answers.AddRange(records);
                result.Authority.Add(zone.StartOfAuthority);
                return(result);
            }
            else
            {
                var owning_subzone = zone.FindSubZone(domain);
                if (owning_subzone != null)
                {
                    logger.Trace("Subzone {0} is authoritative", owning_subzone);

                    // We can punt on the computation to our subzone delgation
                    var subzone_nameservers             = zone.Query(owning_subzone, ResourceRecordType.NAME_SERVER, addr_class);
                    var subzone_nameserver_addr_records = subzone_nameservers
                                                          .SelectMany(ns => zone.Query(ns.Name, ResourceRecordType.HOST_ADDRESS, addr_class));
                    var subzone_nameserver_addrs = subzone_nameserver_addr_records.Select(
                        record => new IPEndPoint(((AResource)record.Resource).Address, 53)
                        ).ToArray();

                    IEnumerable <DNSRecord> response = null;
                    try
                    {
                        var info = resolver.Resolve(domain, ResourceRecordType.HOST_ADDRESS, addr_class, subzone_nameserver_addrs);
                        response = info.aliases.Concat(info.answers).ToList();
                    }
                    catch (ResolverException err)
                    {
                        logger.Trace("Could not resolve from subzone: {0}", err);
                        response = new DNSRecord[] { };
                    }

                    var result = new QueryResult();
                    result.IsAuthority = false;
                    result.FoundAnswer = response.Count() > 0;
                    result.Answers     = new List <DNSRecord>(response);
                    result.Authority   = new List <DNSRecord>(subzone_nameservers);
                    result.Additional  = new List <DNSRecord>(subzone_nameserver_addr_records);
                    return(result);
                }
                else if (recursive)
                {
                    // We'll have to go outside our zone and use the general-purpose resolver
                    ResolverResult response;
                    logger.Trace("No authoritative server is local, executing recursive resolver");

                    try
                    {
                        response = resolver.Resolve(domain, rtype, addr_class, zone.Relays);
                    }
                    catch (ResolverException err)
                    {
                        logger.Trace("Could not resolve: {0}", err);

                        response                     = new ResolverResult();
                        response.answers             = new List <DNSRecord>();
                        response.aliases             = new List <DNSRecord>();
                        response.referrals           = new List <DNSRecord>();
                        response.referral_additional = new List <DNSRecord>();
                    }

                    var result = new QueryResult();
                    result.IsAuthority = false;
                    result.FoundAnswer = response.answers.Count() > 0;
                    result.Answers     = response.aliases.Concat(response.answers).ToList();
                    result.Authority   = response.referrals.ToList();
                    result.Additional  = response.referral_additional.ToList();
                    return(result);
                }
                else
                {
                    var cached_responses = cache.Query(domain, AddressClass.INTERNET, rtype);
                    if (cached_responses.Count > 0)
                    {
                        logger.Trace("Non-recursive search found {0} cached results", cached_responses.Count);

                        var cached_result = new QueryResult();
                        cached_result.IsAuthority = false;
                        cached_result.FoundAnswer = true;
                        cached_result.Answers     = cached_responses.ToList();
                        cached_result.Additional  = new List <DNSRecord>();
                        cached_result.Authority   = new List <DNSRecord>();
                        return(cached_result);
                    }

                    // If we can't recurse, and our cache knows nothing, then punt onto the forwarder
                    logger.Trace("Executing limited-case non-recursive resolver");

                    var question = new DNSQuestion(domain, rtype, AddressClass.INTERNET);

                    foreach (var forwarder in zone.Relays)
                    {
                        try
                        {
                            var forward_result = ResolverUtils.SendQuery(forwarder, question, false);

                            // If the server doesn't like our request, then pass it to something else
                            if (forward_result.ResponseType != ResponseType.NO_ERROR &&
                                forward_result.ResponseType != ResponseType.NAME_ERROR)
                            {
                                continue;
                            }

                            var forward_return = new QueryResult();
                            forward_return.FoundAnswer = forward_result.ResponseType == ResponseType.NO_ERROR;
                            forward_return.IsAuthority = false;
                            forward_return.Answers     = forward_result.Answers.ToList();
                            forward_return.Additional  = forward_result.AdditionalRecords.ToList();
                            forward_return.Authority   = forward_result.AuthoritativeAnswers.ToList();
                            return(forward_return);
                        }
                        catch (SocketException err)
                        {
                            // We can safely punt onto the next forwarder if one bails
                            logger.Trace("Could not request from {0}: {1}", forwarder, err);
                        }
                    }

                    // We can't do anything else here, so there is no such host, as far as we knot
                    var result = new QueryResult();
                    result.FoundAnswer = false;
                    result.IsAuthority = false;
                    result.Answers     = new List <DNSRecord>();
                    result.Authority   = new List <DNSRecord>();
                    result.Additional  = new List <DNSRecord>();
                    return(result);
                }
            }
        }
Exemplo n.º 16
0
        private async Task <CacheItem> Fetch(string id)
        {
            IHtmlDocument document;

            using (var hc = new HttpClient())
            {
                var requestUri = "http://imepic.jp/" + id;
                ResolverUtils.RequestingMessage(this._logger, requestUri, null);

                using (var res = await hc.GetAsync(requestUri).ConfigureAwait(false))
                {
                    if (res.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new ImageNotFoundException();
                    }

                    res.EnsureSuccessStatusCode();
                    document = await res.Content.ReadAsHtmlDocument().ConfigureAwait(false);
                }
            }

            string ogImage      = null;
            string twitterImage = null;

            foreach (var node in document.Head.ChildNodes)
            {
                var element = node as IHtmlMetaElement;
                if (element == null)
                {
                    continue;
                }

                if (ogImage == null && element.GetAttribute("property") == "og:image")
                {
                    ogImage = element.GetAttribute("content");
                    if (twitterImage != null)
                    {
                        break;
                    }
                }
                else if (twitterImage == null && element.GetAttribute("name") == "twitter:image")
                {
                    twitterImage = element.GetAttribute("content");
                    if (ogImage != null)
                    {
                        break;
                    }
                }
            }

            if (ogImage == null || twitterImage == null)
            {
                throw new Exception("イメピク仕様変更の可能性");
            }

            return(new CacheItem
            {
                OgImage = ogImage,
                TwitterImage = twitterImage
            });
        }