예제 #1
0
        // Gets path from cache or downloads image to cache from drive
        public static int saveAll(int highest)
        {
            Commands.Gec.RefreshGec();
            FileUtils.checkForCacheExistance();

            DriveService driveService = AuthenticateServiceAccount(
                "*****@*****.**",
                "../../../GeckoBot-af43fa71833e.json");
            var listRequest = driveService.Files.List();

            listRequest.PageSize = 1000;                        // Only fetch one thousand
            listRequest.Q        = "mimeType contains 'image'"; // Filter out folders or other non image types

            Google.Apis.Drive.v3.Data.FileList     files2 = listRequest.Execute();
            IList <Google.Apis.Drive.v3.Data.File> files  = files2.Files;

            int totalAmount = 0;
            int amount      = 0;

            while (totalAmount < highest)
            {
                foreach (Google.Apis.Drive.v3.Data.File file in files)
                {
                    string name = file.Name.Remove(3);

                    if (name.Contains("b"))
                    {
                        name = file.Name.Remove(4);
                    }
                    if (CheckCache(name) == null)
                    {
                        string type = file.MimeType.Replace("image/", ""); // sorta hacky solution to get file type

                        using var fileStream = new FileStream(
                                  $"../../Cache/{name}_icon.{type}",
                                  FileMode.Create,
                                  FileAccess.Write);
                        driveService.Files.Get(file.Id).Download(fileStream);

                        amount++;
                    }

                    if (!Commands.Gec.geckos.ContainsKey(EmoteUtils.escapeforbidden(name)))
                    {
                        Commands.Gec.geckos.Add(EmoteUtils.escapeforbidden(name), EmoteUtils.escapeforbidden(file.Name));

                        if (CheckCache(name) != null)
                        {
                            continue;
                        }
                    }
                    else if (!Commands.Gec.geckos.ContainsValue(EmoteUtils.escapeforbidden(file.Name)))
                    {
                        Commands.Gec.geckos.Remove(EmoteUtils.escapeforbidden(name));
                        Commands.Gec.geckos.Add(EmoteUtils.escapeforbidden(name), EmoteUtils.escapeforbidden(file.Name));
                    }

                    totalAmount++;
                }
                listRequest.PageToken = files2.NextPageToken;
            }

            FileUtils.Save(Globals.DictToString(Commands.Gec.geckos, "{0} ⁊ {1} ҩ "), @"..\..\Cache\gecko7.gek");

            return(amount);
        }
예제 #2
0
        // Gets path from cache or downloads image to cache from drive
        public static string ImagePath(int num, bool isAlt, bool refresh = false)
        {
            Commands.Gec.RefreshGec();
            FileUtils.checkForCacheExistance();

            string name = addZeros(num);

            if (isAlt)
            {
                name = "b" + name;
            }

            // If image already exists in cache, use it
            string cached = CheckCache(name);

            if (cached != null && Commands.Gec.geckos.ContainsKey(EmoteUtils.escapeforbidden(name)))
            {
                if (!refresh)
                {
                    return(cached);
                }

                File.Delete(cached);
                cached = null;
            }

            if (refresh && Commands.Gec.geckos.ContainsKey(EmoteUtils.escapeforbidden(name)))
            {
                Commands.Gec.geckos.Remove(EmoteUtils.escapeforbidden(name));
            }

            // Otherwise, fetch it from google drive
            DriveService driveService = AuthenticateServiceAccount(
                "*****@*****.**",
                "../../../GeckoBot-af43fa71833e.json");
            var listRequest = driveService.Files.List();

            listRequest.PageSize = 1;                          // Only fetch one
            listRequest.Q        = $"name contains '{name}_'"; // Search for the image via the given number

            IList <Google.Apis.Drive.v3.Data.File> files = listRequest.Execute().Files;

            if (files != null && files.Count > 0)
            {
                // Use the first result
                var file = files[0];

                //adds name of gecko to list
                if (!Commands.Gec.geckos.ContainsKey(name))
                {
                    Commands.Gec.geckos.TryAdd(EmoteUtils.escapeforbidden(name), EmoteUtils.escapeforbidden(file.Name));

                    FileUtils.Save(Globals.DictToString(Commands.Gec.geckos, "{0} ⁊ {1} ҩ "), @"..\..\Cache\gecko7.gek");

                    if (cached != null)
                    {
                        return(cached);
                    }
                }

                //Console.WriteLine(file.MimeType);
                string type = file.MimeType.Replace("image/", ""); // sorta hacky solution to get file type

                for (int i = 0; i < 1000; i++)
                {
                    try
                    {
                        using (var fileStream = new FileStream($"../../Cache/{name}_icon.{type}", FileMode.Create, FileAccess.Write))
                        {
                            driveService.Files.Get(file.Id).Download(fileStream);
                        }
                        break;
                    }
                    catch
                    {
                        continue;
                    }
                }

                return(CheckCache(name) ?? throw new Exception("Drive download failed!"));
            }

            throw new Exception("File " + name + " not found!");
        }