Exemplo n.º 1
0
        public StorageEntry GetText(string url)
        {
            string file_path = Path.Combine(storage_dir, Common.MD5(url));

            if (File.Exists(file_path))
            {
                try
                {
                    JsonObject obj = JsonConvert.Import<JsonObject>(File.ReadAllText(file_path));

                    StorageEntry se = new StorageEntry();

                    se.LastModified = parse_datetime(obj["LastModified"].ToString());

                    se.Text = obj["Text"].ToString();

                    return se;
                }
                catch (JsonException)
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }
Exemplo n.º 2
0
        public StorageEntry GetText(string url)
        {
            string file_path = Path.Combine(storage_dir, Common.MD5(url));

            if (File.Exists(file_path))
            {
                try
                {
                    JsonObject obj = JsonConvert.Import <JsonObject>(File.ReadAllText(file_path));

                    StorageEntry se = new StorageEntry();

                    se.LastModified = parse_datetime(obj["LastModified"].ToString());

                    se.Text = obj["Text"].ToString();

                    return(se);
                }
                catch (JsonException)
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
        public Dictionary <string, BoardInfo> GetAvailableBoards()
        {
            string data = ChanArchiver.Properties.Resources.cached_boards;

            StorageEntry cached_catalog_data = cache.GetText("CatalogData");

            if (cached_catalog_data == null || cached_catalog_data != null && (DateTime.Now - cached_catalog_data.LastModified).Days > 6)
            {
                APIResponse api_r = LoadAPI(string.Format("{0}://a.4cdn.org/boards.json", Common.HttpPrefix));

                if (api_r.Error == APIResponse.ErrorType.NoError)
                {
                    data = api_r.Data;

                    cache.StoreText("CatalogData", api_r.Data, DateTime.Now);
                }
            }
            else
            {
                data = cached_catalog_data.Text;
            }

            JsonObject json = JsonConvert.Import <JsonObject>(data);

            JsonArray boards = (JsonArray)json["boards"];

            var dic = new Dictionary <string, BoardInfo>();

            for (int i = 0; i < boards.Count; i++)
            {
                JsonObject board = (JsonObject)boards[i];

                string letter = Convert.ToString(board["board"]);
                string desc = Convert.ToString(board["title"]);
                int    bl; int iml;
                if (letter == "f")
                {
                    bl = 300; iml = 150;
                }
                else
                {
                    bl  = Convert.ToInt32(board["bump_limit"]);
                    iml = Convert.ToInt32(board["image_limit"]);
                }
                dic.Add(letter, new BoardInfo()
                {
                    Title      = desc,
                    BumpLimit  = bl,
                    ImageLimit = iml
                });
            }

            return(dic);
        }
Exemplo n.º 4
0
 public void StoreText(string url, string text, DateTime lastModified)
 {
     if (data.ContainsKey(url))
     {
         data[url] = new StorageEntry()
         {
             LastModified = lastModified, Text = text
         };
     }
     else
     {
         data.Add(url, new StorageEntry()
         {
             LastModified = lastModified, Text = text
         });
     }
 }
Exemplo n.º 5
0
 public void StoreText(string url, string text, DateTime lastModified)
 {
     if (data.ContainsKey(url))
     {
         data[url] = new StorageEntry() { LastModified = lastModified, Text = text };
     }
     else
     {
         data.Add(url, new StorageEntry() { LastModified = lastModified, Text = text });
     }
 }
Exemplo n.º 6
0
        private APIResponse LoadAPI(string url)
        {
            DateTime lastModified = old_date;

            APIResponse result = null;

            StorageEntry se = cache.GetText(url);

            if (se != null)
            {
                lastModified = se.LastModified;
            }

            HttpWebRequest wr = (HttpWebRequest)HttpWebRequest.Create(url);

            wr.IfModifiedSince = lastModified;

            wr.UserAgent = ChanArchiver.Program.get_random_user_agent();

            WebResponse wbr = null;

            try
            {
                byte[] data;

                wbr = wr.GetResponse();

                using (Stream s = wbr.GetResponseStream())
                {
                    int iByteSize = 0;

                    byte[] byteBuffer = new byte[2048];

                    using (MemoryStream MemIo = new MemoryStream())
                    {
                        while ((iByteSize = s.Read(byteBuffer, 0, 2048)) > 0)
                        {
                            MemIo.Write(byteBuffer, 0, iByteSize);
                            ChanArchiver.NetworkUsageCounter.Add_ApiConsumed(iByteSize);
                        }
                        data = MemIo.ToArray();
                    }
                }

                string text = System.Text.Encoding.UTF8.GetString(data.ToArray());

                string lm = wbr.Headers["Last-Modified"];

                DateTime lmm = DateTime.Parse(lm);

                cache.StoreText(url, text, lmm);

                result = new APIResponse(text, APIResponse.ErrorType.NoError);
            }
            catch (WebException wex)
            {
                HttpWebResponse httpResponse = wex.Response as HttpWebResponse;
                if (httpResponse != null)
                {
                    if (httpResponse.StatusCode == HttpStatusCode.NotModified)
                    {
                        if (se != null)
                        {
                            result = new APIResponse(se.Text, APIResponse.ErrorType.NoError);
                        }
                    }
                    else if (httpResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        result = new APIResponse(null, APIResponse.ErrorType.NotFound);
                        //delete cache entry since resource has 404'ed
                        cache.ClearText(url);
                    }
                    else
                    {
                        result = new APIResponse(wex.Message, APIResponse.ErrorType.Other);
                        //throw wex;
                    }
                }
                else
                {
                    result = new APIResponse(wex.Message, APIResponse.ErrorType.Other);
                    //throw wex;
                }
            }

            if (wbr != null)
            {
                wbr.Close();
            }

            return(result);
        }