Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine(StringConstants.ProductName);

            // load publication dates "db"
            const string PubDatesDbFilename = "./fantiaimgs.posts.json";
            var          serializer         = new System.Web.Script.Serialization.JavaScriptSerializer();

            if (File.Exists(PubDatesDbFilename))
            {
                CurrentAppConfig.PublicationDates = serializer.Deserialize <Dictionary <string, DateTime> >(
                    File.ReadAllText(PubDatesDbFilename)
                    );
            }

            // check subarguments count and handle help requests
            for (int i = 0; i < args.Count(); i++)
            {
                switch (args[i])
                {
                case "-club":
                case "-name":
                case "-cookiefile":
                case "-since":
                    if (i + 1 >= args.Count() || (args[i + 1] != "" && args[i + 1][0] == '-'))
                    {
                        Console.WriteLine(string.Format("Error: \"{0}\" must be followed by its argument.", args[i]));
                        return;
                    }
                    break;

                case "/?":
                case "-help":
                case "--help":
                case "-h":
                    try
                    {
                        using (var stream = System.Reflection.Assembly.
                                            GetExecutingAssembly().GetManifestResourceStream("fantiaimgs.helpfile.txt"))
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                string text = reader.ReadToEnd();
                                Console.WriteLine(text);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // no-op
                    }
                    finally
                    {
                        Console.WriteLine("For the most up to date information please visit:");
                        Console.WriteLine("https://yupdates.neocities.org/tools/fantiaimgs/");
                    }
                    return;
                }
            }
            // apply all the useful arguments
            for (int i = 0; i < args.Count(); i++)
            {
                if (args[i] == "" || args[i][0] != '-')
                {
                    Console.WriteLine(string.Format("Error: \"{0}\" is not a recognized argument.", args[i]));
                    Console.WriteLine("Use \"-h\" to get a list of all supported parameters.");
                    return;
                }
                switch (args[i])
                {
                case "-club":
                    CurrentAppConfig.FanclubId = args[++i];
                    break;

                case "-name":
                    CurrentAppConfig.FanclubName = args[++i];
                    break;

                case "-cookiefile":
                    CurrentAppConfig.CookieFile = args[++i];
                    break;

                case "-since":
                    CurrentAppConfig.EarliestValidDate = DateTime.Parse(args[++i]);
                    break;

                case "-upd":
                    CurrentAppConfig.CheckModifiedPosts = true;
                    break;

                case "-nometa":
                    CurrentAppConfig.DownloadPolicyGetMetaPics = false;
                    break;

                case "-imgnum":
                    CurrentAppConfig.RenamePolicyPrependImageIdx = true;
                    break;

                case "-subp":
                    CurrentAppConfig.RenamePolicyPrependGroupIdx = true;
                    break;

                case "-nh":
                    CurrentAppConfig.RenamePolicyRemoveHash = true;
                    break;

                case "-keepthumbnames":
                    CurrentAppConfig.RenamePolicyRenameThumbs = false;
                    break;

                default:
                    Console.WriteLine(string.Format("What did you mean by {0}?", args[i]));
                    Console.WriteLine("Use \"-h\" to get a list of all supported parameters.");
                    return;
                }
            }

            if (!File.Exists(CurrentAppConfig.CookieFile))
            {
                Console.WriteLine("_session_id value?");
                string cookie = Console.ReadLine().Trim();
                Console.WriteLine("Note: you can put this value in the file passed after \"-cookiefile\" argument");
                Console.WriteLine("(or \"_session_id.txt\") to avoid entering it manually each time");
                CurrentNetworkState.Init(cookie);
            }
            else
            {
                var sessionfile = File.ReadAllLines(CurrentAppConfig.CookieFile);
                if (sessionfile.Count() > 0)
                {
                    string cookie = sessionfile[0].Trim();
                    CurrentNetworkState.Init(cookie);
                }
                else
                {
                    Console.WriteLine("Bad _session_id. File must contain one line with the cookie value");
                    return;
                }
            }

            string fanclub = "";

            if (!string.IsNullOrEmpty(CurrentAppConfig.FanclubId))
            {
                fanclub = CurrentAppConfig.FanclubId;
            }
            else
            {
                Console.WriteLine("fanclub id (digital)?");
                fanclub = Console.ReadLine().Trim();
            }
            try
            {
                Console.WriteLine(string.Format("Fanclub: {0}", int.Parse(fanclub)));
            }
            catch (Exception)
            {
                Console.WriteLine("Bad Fanclub id. It's supposed to be a number, you see~");
                return;
            }

            string hrname = "";

            if (!string.IsNullOrEmpty(CurrentAppConfig.FanclubName))
            {
                hrname = StaticTools.SafeFilename(CurrentAppConfig.FanclubName);
            }
            else if (string.IsNullOrEmpty(CurrentAppConfig.FanclubId))
            {
                Console.WriteLine("human-readable name? (leave blank to use id)");
                hrname = StaticTools.SafeFilename(Console.ReadLine().Trim());
            }
            if (hrname != "")
            {
                CurrentNetworkState.SetRootDest(hrname);
            }

            ProcessFanclub(fanclub);

            // dump "db"
            File.WriteAllText(PubDatesDbFilename, serializer.Serialize(CurrentAppConfig.PublicationDates));

            Console.WriteLine("Done!");
        }
Beispiel #2
0
        /// network code for this largely stolen from furdown @ github.com/crouvpony47/furdown
        public void DownloadFile(string url, string dest,
                                 List <TransformFilenameFunction> fnts = null, bool abortOnExisting = false)
        {
            // this conversion might be not quite portable, but it's good enough, i suppose
            Uri    uri   = new Uri(url);
            string fname = StaticTools.SafeFilename(System.IO.Path.GetFileName(uri.LocalPath));

            if (fnts != null)
            {
                foreach (var fnt in fnts)
                {
                    fname = fnt(fname);
                }
            }

            string fnamefull = "";

retryPickName:

            fnamefull = Path.Combine(Path.Combine(Path.Combine(
                                                      "./",
                                                      rootdest != null ? rootdest : "unknown"), dest), fname
                                     );

            Directory.CreateDirectory(Path.GetDirectoryName(fnamefull));

            // download file
            Console.WriteLine("Preparing to download " + fname);
            if (File.Exists(fnamefull))
            {
                if (abortOnExisting)
                {
                    Console.WriteLine("Already exists, skipped");
                    return;
                }
                fname = "+" + fname;
                goto retryPickName;
            }
            int fattempts = 3;

fbeforeawait:
            try
            {
                Console.WriteLine("Downloading file... ");
                using (
                    HttpResponseMessage httpResponse = httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead).Result
                    )
                {
                    // if redirected, get the new filename
                    if ((int)httpResponse.StatusCode >= 400)
                    {
                        throw new Exception(string.Format("Unexpected HTTP status code ({0})", httpResponse.StatusCode.ToString()));
                    }
                    var actualUrl = httpResponse.RequestMessage.RequestUri;
                    fname = StaticTools.SafeFilename(Path.GetFileName(actualUrl.LocalPath));
                    if (fnts != null)
                    {
                        foreach (var fnt in fnts)
                        {
                            fname = fnt(fname);
                        }
                    }
retryPickName2:
                    fnamefull = Path.Combine(Path.Combine(Path.Combine(
                                                              "./",
                                                              rootdest != null ? rootdest : "unknown"), dest), fname
                                             );
                    if (File.Exists(fnamefull))
                    {
                        if (abortOnExisting)
                        {
                            Console.WriteLine("Already exists, skipped");
                            return;
                        }
                        fname = "+" + fname;
                        goto retryPickName2;
                    }
                    Console.WriteLine("final filename: " + fname);
                    // actually download the files
                    using (
                        Stream contentStream = httpResponse.Content.ReadAsStreamAsync().Result,
                        stream = new FileStream(
                            fnamefull,
                            FileMode.Create, FileAccess.Write, FileShare.None, 1024 * 1024 /*Mb*/, true)
                        )
                    {
                        ReadNetworkStream(contentStream, stream, 5000).Wait();
                    }
                }
            }
            catch (Exception E)
            {
                // write error message
                if (E is ObjectDisposedException)
                {
                    Console.WriteLine("Network error (data receive timeout)");
                }
                else
                {
                    Console.WriteLine(string.Format("Http request error (file {0}): {1}", fname, E.Message));
                }
                // remove incomplete download
                if (File.Exists(fnamefull))
                {
                    File.Delete(fnamefull);
                }
                // try again or abort operation
                fattempts--;
                System.Threading.Thread.Sleep(2000);
                if (fattempts > 0)
                {
                    goto fbeforeawait;
                }
                {
                    Console.WriteLine("Giving up on downloading " + fname);
                    return;
                }
            }
            Console.WriteLine("Done: " + fname);
        }