public static string GetQueryString(DanbooruProvider provider, DanbooruSearchParam query)
        {
            var queryStr = "";

            // Clean up txtTags
            var tags = query.Tag;
            while (tags.Contains("  "))
            {
                tags = tags.Replace("  ", " ");
            }
            tags = tags.Trim();
            tags = System.Web.HttpUtility.UrlEncode(tags);

            //StartPage
            var page = 1;
            if (query.Page.HasValue && query.Page > 0)
            {
                page = query.Page.Value;
            }

            queryStr = tags;
            if (!String.IsNullOrWhiteSpace(tags)) queryStr += "/";
            queryStr += page;

            return queryStr;
        }
        public List<DanbooruProvider> Read(string filename = @"DanbooruProviderList.xml")
        {
            List<DanbooruProvider> list = new List<DanbooruProvider>();

            using (XmlTextReader reader = new XmlTextReader(filename))
            {
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            if (reader.Name.Equals("DanbooruProviderList")) break;
                            else if (reader.Name.Equals("DanbooruProvider"))
                            {
                                Console.WriteLine(reader.Value);

                                DanbooruProvider newProvider = new DanbooruProvider();
                                do
                                {
                                    reader.Read();
                                    if (reader.NodeType.Equals(XmlNodeType.Element))
                                    {
                                        switch (reader.Name)
                                        {
                                            case "Name": newProvider.Name = reader.ReadElementContentAsString(); break;
                                            case "Url": newProvider.Url = reader.ReadElementContentAsString(); break;
                                            case "QueryStringJson": newProvider.QueryStringJson = reader.ReadElementContentAsString(); break;
                                            case "QueryStringHtml": newProvider.QueryStringHtml = reader.ReadElementContentAsString(); break;
                                            case "QueryStringXml": newProvider.QueryStringXml = reader.ReadElementContentAsString(); break;
                                            case "Preferred":
                                                string preferred = reader.ReadElementContentAsString().ToLowerInvariant();
                                                switch (preferred)
                                                {
                                                    case "json":
                                                        newProvider.Preferred = PreferredMethod.Json;
                                                        break;
                                                    case "xml":
                                                        newProvider.Preferred = PreferredMethod.Xml;
                                                        break;
                                                    case "html":
                                                        newProvider.Preferred = PreferredMethod.Html;
                                                        break;
                                                    default:
                                                        throw new Exception("Invalid Provider Type in DanbooruProviderList.xml: " + preferred);
                                                }
                                                break;
                                            case "DefaultLimit": newProvider.DefaultLimit = reader.ReadElementContentAsInt(); break;
                                            case "HardLimit": newProvider.HardLimit = reader.ReadElementContentAsInt(); break;
                                            case "UserName": newProvider.UserName = reader.ReadElementContentAsString(); break;
                                            case "Password": newProvider.Password = reader.ReadElementContentAsString(); break;
                                            case "UseAuth": newProvider.UseAuth = reader.ReadElementContentAsBoolean(); break;
                                            case "PasswordSalt": newProvider.PasswordSalt = reader.ReadElementContentAsString(); break;
                                            case "PasswordHash": newProvider.PasswordHash = reader.ReadElementContentAsString(); break;
                                            case "BoardType":
                                                string type = reader.ReadElementContentAsString();
                                                newProvider.BoardType = (BoardType)Enum.Parse(typeof(BoardType), type); //Type.Equals("Danbooru") ? BoardType.Danbooru:BoardType.Gelbooru ;
                                                break;
                                            case "TagDownloadUseLoop": newProvider.TagDownloadUseLoop = reader.ReadElementContentAsBoolean(); break;
                                            default: break;
                                        }
                                    }
                                } while (!reader.Name.Equals("DanbooruProvider"));
                                list.Add(newProvider);
                            }
                            break;
                        default: break;
                    }
                }
            }
            return list;
        }
 private static bool isHttps(DanbooruProvider provider)
 {
     return provider.Url.ToLowerInvariant().StartsWith("https");
 }
Exemple #4
0
 public static List<DanbooruTag> ParseTags(string p, DanbooruProvider provider)
 {
     if (provider != null && provider.HasPrivateTags && !DanbooruDownloader3.Properties.Settings.Default.UseGlobalProviderTags)
     {
         return DanbooruTagsDao.Instance.ParseTagsString(p, provider.ProviderTagCollection);
     }
     else
         return DanbooruTagsDao.Instance.ParseTagsString(p);
 }
Exemple #5
0
 public static bool DumpRawData(string data, DanbooruProvider provider, string query)
 {
     string filename = "Dump for List " + provider.Name + " Query " + query + ".txt";
     filename = Helper.SanitizeFilename(filename);
     try
     {
         using (StreamWriter output = File.CreateText(filename))
         {
             output.Write(data);
             output.Flush();
         }
         return true;
     }
     catch (Exception ex)
     {
         Program.Logger.Error("Failed to create dump file: " + filename, ex);
         return false;
     }
 }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            DanbooruProvider newP = new DanbooruProvider();
            newP.Url = "http://";
            Providers.Add(newP);
            LoadProviders();
            cbxProviders.SelectedIndex = cbxProviders.Items.Count - 1;

            var controls = this.Controls.Find("BoardType", true);
            if (controls.Length > 0)
            {
                ComboBox c = controls[0] as ComboBox;
                c.SelectedIndex = -1;
                c.SelectedValue = null;
                c.SelectedText = "";
            }
        }
        private void GetValues(int index)
        {
            if (index == -1 || index >= Providers.Count) return;
            DanbooruProvider temp = new DanbooruProvider();
            PropertyInfo[] propertyInfos;
            propertyInfos = typeof(DanbooruProvider).GetProperties();

            foreach (var info in propertyInfos)
            {
                try
                {
                    var controls = tableLayoutPanel1.Controls.Find(info.Name, true);
                    if (controls.Length > 0)
                    {
                        if (!controls[0].Enabled) continue;
                        if (controls[0].GetType().Name == "TextBox")
                        {
                            var value = controls[0].Text;
                            info.SetValue(temp, Convert.ChangeType(value, info.PropertyType), null);
                        }
                        else
                        {
                            ComboBox c = (ComboBox)controls[0];
                            var value = c.SelectedItem;
                            info.SetValue(temp, value, null);
                        }
                    }
                }
                catch (Exception ex) { Program.Logger.Error(ex.Message, ex); }
            }

            Providers[index] = temp;
        }
        public static string GetQueryString(DanbooruProvider provider, DanbooruSearchParam query)
        {
            var queryStr = "";

            // Clean up txtTags
            var tags = query.Tag;
            while (tags.Contains("  "))
            {
                tags = tags.Replace("  ", " ");
            }
            tags = tags.Trim();
            tags = System.Web.HttpUtility.UrlEncode(tags);

            List<string> queryList = new List<string>();
            List<string> tagsList = new List<string>();

            //Tags
            if (tags.Length > 0) tagsList.Add(tags.Replace(' ', '+'));

            //Rating
            if (!String.IsNullOrWhiteSpace(query.Rating)) tagsList.Add(query.IsNotRating ? "-" + query.Rating : "" + query.Rating);

            //Source
            if (!String.IsNullOrWhiteSpace(query.Source)) tagsList.Add("source:" + query.Source);

            //Order
            if (!String.IsNullOrWhiteSpace(query.OrderBy)) tagsList.Add(query.OrderBy);

            if (tagsList.Count > 0) queryList.Add("tags=" + String.Join("+", tagsList));

            //Limit
            if (query.Limit > 0) queryList.Add("limit=" + query.Limit);

            //StartPage
            if (query.Page > 0)
            {
                if (provider.BoardType == BoardType.Danbooru) queryList.Add("page=" + query.Page);
                else if (provider.BoardType == BoardType.Gelbooru) queryList.Add("pid=" + query.Page);
            }

            if (queryList.Count > 0) queryStr = String.Join("&", queryList);

            return queryStr;
        }
 private string AppendHttp(string url, DanbooruProvider provider)
 {
     if (String.IsNullOrWhiteSpace(url)) return url;
     if (!url.StartsWith("http"))
     {
         return provider.Url + url;
     }
     return url;
 }
 private void btnEdit_Click(object sender, EventArgs e)
 {
     using (FormProvider form = new FormProvider())
     {
         form.Providers = _listProvider.ToList<DanbooruProvider>();
         form.SelectedIndex = cbxProvider.SelectedIndex;
         DialogResult result = form.ShowDialog();
         if (result == System.Windows.Forms.DialogResult.OK)
         {
             DanbooruProviderDao.GetInstance().Save(form.Providers);
             LoadProviderList();
             cbxProvider.SelectedIndex = form.SelectedIndex;
             _currProvider = _listProvider[cbxProvider.SelectedIndex];
         }
     }
 }
        private void cbxProvider_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbxProvider.SelectedIndex == -1) return;

            txtPage.Text = "";
            _currProvider = _listProvider[cbxProvider.SelectedIndex];
            UpdateStatus();

            if (_currProvider.BoardType == BoardType.Danbooru) pbIcon.Image = Properties.Resources.Danbooru;
            else if (_currProvider.BoardType == BoardType.Gelbooru) pbIcon.Image = Properties.Resources.Gelbooru;
            else if (_currProvider.BoardType == BoardType.Shimmie2) pbIcon.Image = Properties.Resources.Shimmie2;
            else pbIcon.Image = null;
        }
 private static string AppendHttp(string url, DanbooruProvider provider)
 {
     if (!url.StartsWith("http")) url = provider.Url + url;
     return url;
 }
        private void UpdateQueryString()
        {
            var index = cbxProvider.SelectedIndex;
            if (index > -1)
            {
                SelectedProvider = list[index];
                string limit = "0";
                chkUseLoop.Checked = SelectedProvider.TagDownloadUseLoop;
                txtStartingPage.Enabled = chkUseLoop.Checked;
                if (chkUseLoop.Checked)
                {
                    limit = "1000";
                }

                Page = Int32.Parse(txtStartingPage.Text);
                if (SelectedProvider.BoardType == BoardType.Danbooru)
                {
                    txtUrl.Text = cbxProvider.SelectedValue + @"/tag/index.xml?limit=" + limit;

                    // sankaku
                    if (cbxProvider.SelectedValue.ToString().ToLowerInvariant().Contains("sankaku"))
                    {
                        txtUrl.Text = cbxProvider.SelectedValue + @"/tag/index?";
                        isSankaku = true;
                    }
                    else
                    {
                        isSankaku = false;
                    }

                    pbIcon.Image = Properties.Resources.Danbooru;
                }
                else if (SelectedProvider.BoardType == BoardType.Gelbooru)
                {
                    txtUrl.Text = cbxProvider.SelectedValue + @"/index.php?page=dapi&s=tag&q=index&limit=" + limit;
                    pbIcon.Image = Properties.Resources.Gelbooru;
                }
            }
            else
            {
                chkUseLoop.Checked = false;
                txtUrl.Text = "";
                pbIcon.Image = null;
            }
        }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            retry = 0;
            if (!String.IsNullOrWhiteSpace(txtUrl.Text))
            {
                SelectedProvider = list[cbxProvider.SelectedIndex];
                btnDownload.Enabled = false;
                lblStatus.Text = "Status: Download starting...";
                lblStatus.Invalidate();
                lblStatus.Update();
                lblStatus.Refresh();
                Application.DoEvents();

                CreateBackup();

                // Merge preparation
                if (chkMerge.Checked)
                {
                    Program.Logger.Info("[Download Tags] Merge checked.");
                    var mergeTarget = TAGS_FILENAME + ".merge";
                    if (File.Exists(TAGS_FILENAME))
                    {
                        if (File.Exists(mergeTarget))
                            File.Delete(mergeTarget);
                        File.Copy(TAGS_FILENAME, mergeTarget);
                    }
                    else
                    {
                        chkMerge.Checked = false;
                    }
                }

                if (chkUseLoop.Checked)
                {
                    chkUseLoop.Enabled = false;
                    ProcessLoop(Page);
                }
                else
                {
                    ProcessSingle();
                }
            }
        }
 public static DateTime ParseDateTime(string dtStr, DanbooruProvider provider)
 {
     DateTime dt = DateTime.MinValue;
     if (!String.IsNullOrWhiteSpace(dtStr) &&
         provider.DateTimeFormat != DanbooruProviderDao.DATETIME_FORMAT_NA)
     {
         if (provider.DateTimeFormat == DanbooruProviderDao.DATETIME_FORMAT_UNIX)
         {
             double timestamp = 0;
             double.TryParse(dtStr, out timestamp);
             dt = new DateTime(1970, 1, 1).AddSeconds(timestamp);
         }
         else
         {
             var result = DateTime.TryParseExact(dtStr,
                                    provider.DateTimeFormat,
                                    CultureInfo.InvariantCulture,
                                    DateTimeStyles.None,
                                    out dt);
             if (!result)
             {
                 Program.Logger.WarnFormat("Invalid format: {1} for {2} ==> {0}.", dt, provider.DateTimeFormat, provider.Name);
             }
         }
     }
     return dt;
 }
        private void UpdateQueryString()
        {
            var index = cbxProvider.SelectedIndex;
            if (index > -1)
            {
                SelectedProvider = list[index];
                string limit = "0";
                chkUseLoop.Checked = SelectedProvider.TagDownloadUseLoop;
                txtStartingPage.Enabled = chkUseLoop.Checked;
                if (chkUseLoop.Checked)
                {
                    limit = "1000";
                }

                Page = Int32.Parse(txtStartingPage.Text);
                if (SelectedProvider.BoardType == BoardType.Danbooru)
                {
                    txtUrl.Text = cbxProvider.SelectedValue + @"/tag/index.xml?limit=" + limit;

                    // sankaku
                    if (cbxProvider.SelectedValue.ToString().ToLowerInvariant().Contains("sankaku"))
                    {
                        txtUrl.Text = cbxProvider.SelectedValue + @"/tag/index?";
                        isSankaku = true;
                    }
                    else
                    {
                        isSankaku = false;
                    }

                    pbIcon.Image = Properties.Resources.Danbooru;
                }
                else if (SelectedProvider.BoardType == BoardType.Gelbooru)
                {
                    txtUrl.Text = cbxProvider.SelectedValue + @"/index.php?page=dapi&s=tag&q=index&limit=" + limit;
                    pbIcon.Image = Properties.Resources.Gelbooru;
                }

                if (SelectedProvider.LoginType == LoginType.Cookie)
                {
                    // need to inject csv cookie  to the webclient
                    var cookies = Helper.ParseCookie(SelectedProvider.UserName, SelectedProvider.Url);
                    foreach (var cookie in cookies)
                    {
                        ExtendedWebClient.CookieJar.Add(cookie);
                    }
                }
            }
            else
            {
                chkUseLoop.Checked = false;
                txtUrl.Text = "";
                pbIcon.Image = null;
            }
        }