KichangKim / DanbooruDownloader

Danbooru image downloader.
MIT License
149 stars 25 forks source link

403Error,but i can access the link through website #11

Closed AXiX-official closed 1 year ago

AXiX-official commented 1 year ago

2023-01-03 13:18:41.9115|INFO|DanbooruDownloader.Commands.DumpCommand|Downloading metadata ... (5947490 ~ ) 2023-01-03 13:18:42.7698|ERROR|DanbooruDownloader.Commands.DumpCommand|System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden). at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode() at System.Net.Http.HttpClient.GetStringAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken) at DanbooruDownloader.Utilities.DanbooruUtility.GetPosts(Int64 startId, String username, String apikey) in D:\Projects\Machine Learning Projects\DanbooruDownloader\DanbooruDownloader\Utilities\DanbooruUtility.cs:line 26 at DanbooruDownloader.Commands.DumpCommand.<>c__DisplayClass1_1.<<Run>b__0>d.MoveNext() in D:\Projects\Machine Learning Projects\DanbooruDownloader\DanbooruDownloader\Commands\DumpCommand.cs:line 49 --- End of stack trace from previous location --- at DanbooruDownloader.Utilities.TaskUtility.RunWithRetry(Func1 job, Func2 retryCondition, Int32 retryCount, Int32 retryDelay) in D:\Projects\Machine Learning Projects\DanbooruDownloader\DanbooruDownloader\Utilities\TaskUtility.cs:line 16

AXiX-official commented 1 year ago

It's because the cloudflare. I solved this,just need this https://github.com/RyuzakiH/CloudflareSolverRe

UiharuKazari2008 commented 1 year ago

I have the same issue, does the require a update the the code or is this some external tool. More details please

AXiX-official commented 1 year ago

I have the same issue, does the require a update the the code or is this some external tool. More details please

yes. You can simply replace the following code in DanbooruDownloader/Utilities/DanbooruUtility.cs

public static async Task<JObject[]> GetPosts(long startId, string username, string apikey)
        {
            using (HttpClient client = new HttpClient())
            {
                string url = GetPostsUrl(startId, username, apikey);
                string jsonString = await client.GetStringAsync(url);

                JArray jsonArray = JArray.Parse(jsonString);

                return jsonArray.Cast<JObject>().ToArray();
            }
        }

to

public static async Task<JObject[]> GetPosts(long startId, string username, string apikey)
        {
            string url = GetPostsUrl(startId, username, apikey);
            var target = new Uri(url);
            var handler = new ClearanceHandler
            {
                MaxTries = 3,
                ClearanceDelay = 3000
            };

            using (HttpClient client = new HttpClient(handler))
            {
                string jsonString = await client.GetStringAsync(target);

                JArray jsonArray = JArray.Parse(jsonString);

                return jsonArray.Cast<JObject>().ToArray();
            }
        }

and don't forget

using CloudflareSolverRe;

and if you are training deepdanbooru with https://github.com/KichangKim/DeepDanbooru you will also encounter this issue. for example,

> deepdanbooru download-tags [your_project_folder]

will also raise 403 error. for python,you need https://github.com/VeNoMouS/cloudscraper

UiharuKazari2008 commented 1 year ago

Thank you my knowledge of C is nonexistent but I was able to get it to work only issue now is its just non-stop

|ERROR|DanbooruDownloader.Commands.DumpCommand|Can't retryable exception was occured : Id=880

I don't know what when i should know if there a issue sense I have never used this application to know

AXiX-official commented 1 year ago

sorry,it's my fault.I missed another changing in DanbooruDownloader/Commands/DumpCommands.cs replace:

static async Task Download(string uri, string path)
        {
            using (HttpClient client = new HttpClient())
            {
                HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

                switch (response.StatusCode)
                {
                    case HttpStatusCode.Forbidden:
                    case HttpStatusCode.NotFound:
                        throw new NotRetryableException();
                }

                response.EnsureSuccessStatusCode();

                using (FileStream fileStream = File.Create(path))
                {
                    using (Stream httpStream = await response.Content.ReadAsStreamAsync())
                    {
                        httpStream.CopyTo(fileStream);
                        fileStream.Flush();
                    }
                }
            }
        }

with

static async Task Download(string uri, string path)
        {
            var handler = new ClearanceHandler
            {
                MaxTries = 3,
                ClearanceDelay = 3000
            };

            using (HttpClient client = new HttpClient(handler))
            {
                HttpResponseMessage response = await client.GetAsync(uri, HttpCompletionOption.ResponseHeadersRead);

                switch (response.StatusCode)
                {
                    case HttpStatusCode.Forbidden:
                    case HttpStatusCode.NotFound:
                        throw new NotRetryableException();
                }

                response.EnsureSuccessStatusCode();

                using (FileStream fileStream = File.Create(path))
                {
                    using (Stream httpStream = await response.Content.ReadAsStreamAsync())
                    {
                        httpStream.CopyTo(fileStream);
                        fileStream.Flush();
                    }
                }
            }
        }

also remember adding

using CloudflareSolverRe;

at beginning

UiharuKazari2008 commented 1 year ago

Thank you very much, I actually about to start getting the ball rolling on this project! I would suggest making a pull request so other people wont just give up immediately like i was about to do lol

PlutoNameless commented 1 year ago

Thank you very much, I actually about to start getting the ball rolling on this project! I would suggest making a pull request so other people wont just give up immediately like i was about to do lol

I greatly appreciate your response. However, my knowledge of C# is quite limited. I've tried to compile the existing code using my Visual Studio, but without any luck. I'm also stuck at the 403 step, just like you. Seeing your modifications has sparked my hope again, yet I've been unable to locate a compiled version in your fork, which leaves me unable to execute the code. Would you be able to provide an executable exe file that includes the fix for the 403 issue? I would be immensely grateful.