예제 #1
0
        /// <summary>
        /// Asynchronicznie zapisuje treść wybranej ustawy z API do pliku <c>HTML</c> do dalszej obróbki.
        /// <seealso cref="HttpClient"/>
        /// <seealso cref="StringBuilder"/>
        /// <seealso cref="HttpResponseMessage"/>
        /// <seealso cref="FileStream"/>
        /// </summary>
        /// <param name="ustawa"><see cref="Ustawa"/>, której treść pobieramy</param>
        /// <returns>Treść ustawy zapisana w pliku <c>HTML</c></returns>
        public static async Task GetContentAsync(Ustawa ustawa)
        {
            byte counter = 1;
            HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
            var s     = Resources.Base_API2 + $"{ustawa.Dokument_Id}/{ustawa.Dokument_Id}_{counter}.html";
            Uri adres = new Uri(s);
            var tresc = "";

            try
            {
                //responseMessage = await Client.GetAsync(adres);
            }
            catch (HttpRequestException e)
            {
                MessageBox.Show(e.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            #region DownloadRegion

            while (responseMessage.IsSuccessStatusCode)
            {
                using (WebClient client2 = new WebClient())
                {
                    try
                    {
                        client2.DownloadFile(adres, $"../../tresci http/tresc_{ustawa.Dokument_Id}.html");
                        tresc += client2.DownloadString(adres);
                        counter++;
                    }
                    catch (HttpRequestException hEx)
                    {
                        MessageBox.Show(hEx.Message);
                    }
                    catch (WebException we)
                    {
                        MessageBox.Show("Sie popsuło\n" + we.Message);
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    finally
                    {
                        responseMessage = await Client.GetAsync(adres);
                    }
                }
            }

            File.WriteAllText($"../../tresci http/tresc_{ustawa.Dokument_Id}.html", tresc);
            ustawa.Tresc_path = $"../../tresci http/tresc_{ustawa.Dokument_Id}.html";
            #endregion
        }
예제 #2
0
 public SzczegolyUstawy(Ustawa u) : this()
 {
     _taUstawa            = u;
     textBox_tytul.Text   = _taUstawa.Tytul;
     textBox_nr.Text      = _taUstawa.Nr.ToString();
     textBox_autor.Text   = _taUstawa.Autor_Nazwa;
     textBox_label.Text   = _taUstawa.Label;
     textBox_dataPub.Text = _taUstawa.Data_Publikacji;
     textBox_dataWyd.Text = _taUstawa.Data_Wydania;
     textBox_DataWej.Text = _taUstawa.Data_Wejscia_W_Zycie;
     textBox_Syg.Text     = _taUstawa.Sygnatura;
 }
예제 #3
0
 private void button_szczegoly_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         Ustawa          ustawa = _listaUst.ElementAt(listBox_listaUstaw.SelectedIndex);
         SzczegolyUstawy szcz   = new SzczegolyUstawy(ustawa);
         if (szcz.ShowDialog() != null)
         {
         }
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
예제 #4
0
        /// <summary>
        /// Wyszukuje ustawy w API korzystając z metody ?conditions[].
        /// Pobiera słownik (nazwa pola w GUI, treść pola w GUI),
        /// a zwraca listę <see cref="Ustawa" /> do wyświetlenia w interfejsie.
        /// </summary>
        /// <param name="conds">Lista <see cref="Dictionary{TKey,TValue}" />:
        /// Key = Nazwa parametru, np.<see cref="Ustawa.Rok" />;
        /// Value = Zawartość <see cref="TextBox" /></param>
        /// <returns>
        /// Lista <see cref="Ustawa" /> do wyświetlenia w GUI
        /// </returns>
        /// <exception cref="System.ArgumentNullException">Jeśli lista jest null</exception>
        /// <exception cref="System.ArgumentException">Jeśli lista argumentów jest pusta</exception>
        /// <seealso cref="GetSavejson" />
        /// <seealso cref="Ustawa.RemoveDatasetName" />
        public static async Task <List <Ustawa> > SzukajAsync(Dictionary <string, string> conds)
        {
            //check if null
            if (conds == null)
            {
                throw new ArgumentNullException(nameof(conds));
            }
            //check if empty
            if (conds.Count == 0)
            {
                throw new ArgumentException(Resources.cannot_be_an_empty_collection, nameof(conds));
            }

            List <Ustawa> wynikiList = new List <Ustawa>();

            string        apiStr = Resources.DzU_Search;
            StringBuilder sb     = new StringBuilder(apiStr);

            foreach (KeyValuePair <string, string> warunek in conds)
            {
                if (warunek.Value != "" && warunek.Value != " ")
                {
                    sb.Append($"&conditions[dziennik_ustaw.{warunek.Key}]={warunek.Value}");
                }
            }

            try
            {
                HttpResponseMessage responseMessage = await Client.GetAsync(sb.ToString());

                if (responseMessage.IsSuccessStatusCode)
                {
                    string ustawa = await responseMessage.Content.ReadAsStringAsync();

                    Regex rgx = new Regex(@"\{.*\[");
                    ustawa = rgx.Replace(ustawa, "");
                    Regex rgx2 = new Regex(@"\]\,.\w{5}.*\}\}");
                    ustawa = rgx2.Replace(ustawa, "");
                    string[] podustawy = Regex.Split(ustawa, @"\}\}\,");

                    for (int i = 0; i < podustawy.Length; i++)
                    {
                        string temp = podustawy[i];
                        if (i < (podustawy.Length - 1))
                        {
                            temp = temp + "}}";
                        }
                        podustawy[i] = temp;
                    }
                    Parallel.For(0, podustawy.Length, i =>
                    {
                        using (TextWriter sw = new StreamWriter($"../../Json/Ustawa_{i + 1}.json"))
                        {
                            try
                            {
                                sw.Write(podustawy[i]);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                                throw;
                            }
                            finally
                            {
                                Ustawa u = Ustawa.ParseUstawa(i + 1);
                                wynikiList.Add(u);
                            }
                        }
                    });
                }
                else
                {
                    wynikiList = new List <Ustawa>();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            return(wynikiList);
        }