示例#1
0
        public async Task Upload(Album album)
        {
            ImageEndpoint endpoint = new ImageEndpoint(await ImgurHelper.GetClient());

            using (Stream src = File.OpenRead(Path.Combine(album.Root, FileName)))
            {
                await endpoint.UploadImageStreamAsync(src, album.Id, Name);
            }
        }
示例#2
0
文件: User.cs 项目: sunk818/Syncurr
        public async Task Save()
        {
            if (!Directory.Exists(Root))
            {
                Directory.CreateDirectory(Root);
            }
            File.WriteAllText(Path.Combine(Root, "syncurr.user.json"), JsonConvert.SerializeObject(this));
            bool me = (await ImgurHelper.GetToken()).AccountId == Id;

            if (!me && !Properties.Settings.Default.Users.Contains(Root))
            {
                Properties.Settings.Default.Users.Add(Root);
                Properties.Settings.Default.Save();
            }
        }
示例#3
0
        public async Task FetchSubredditImage(EduardoContext context, string subredditName)
        {
            IGalleryItem img = await ImgurHelper.SearchImgurSubreddit(_credentials.ImgurClientId, _credentials.ImgurClientSecret, subredditName);

            if (img != null)
            {
                switch (img)
                {
                case GalleryAlbum album:
                    await context.Channel.SendMessageAsync(album.Link);

                    break;

                case GalleryImage image:
                    await context.Channel.SendMessageAsync(image.Link);

                    break;
                }
            }
            else
            {
                await context.Channel.SendMessageAsync($"**Could not find subreddit with name {subredditName}.**");
            }
        }
示例#4
0
        public async Task SearchImgur(EduardoContext context, string searchQuery = null)
        {
            IGalleryItem img = await ImgurHelper.SearchImgur(_credentials.ImgurClientId, _credentials.ImgurClientSecret, searchQuery);

            if (img != null)
            {
                switch (img)
                {
                case GalleryAlbum album:
                    await context.Channel.SendMessageAsync(album.Link);

                    break;

                case GalleryImage image:
                    await context.Channel.SendMessageAsync(image.Link);

                    break;
                }
            }
            else
            {
                await context.Channel.SendMessageAsync($"**Could not find any images that match {searchQuery}**");
            }
        }
示例#5
0
        protected override async Task DoSync(ProgressDialogController pdc)
        {
            if (Me?.Name == null)
            {
                if (User.Exists(Properties.Settings.Default.MeRoot))
                {
                    Me = User.Get(Properties.Settings.Default.MeRoot);
                }
                if (Me?.Name == null)
                {
                    IOAuth2Token token = await ImgurHelper.GetToken();

                    Me = User.Create(Properties.Settings.Default.MeRoot, token.AccountUsername, token.AccountId);
                }
            }
            if (Me?.Name != null)
            {
                IsLoading = true;
                try
                {
                    pdc.SetMessage(string.Format("synchronizing account\n\n{0}\n{1}", Me.Name, Me.Root));
                    await Me.Sync(this);
                }
                catch (Exception ex)
                {
                    List <string> msgs = new List <string>();
                    while (ex != null)
                    {
                        msgs.Add(ex.Message);
                        ex = ex.InnerException;
                    }
                    await DialogCoordinator.Instance.ShowMessageAsync(this, "Failed to synchronize albums", string.Join("\n\n", msgs));
                }
                IsLoading = false;
            }
        }
示例#6
0
文件: Album.cs 项目: sunk818/Syncurr
        public async Task Sync(object context)
        {
            if (!Synchronize)
            {
                return;
            }
            await Task.Run(async() =>
            {
                // get local files
                FileInfo[] local = new DirectoryInfo(Root).GetFiles().Where(it => new string[] { ".jpg", ".gif", ".png", ".bmp" }.Contains(it.Extension.ToLowerInvariant())).ToArray();

                // get remote files
                AlbumEndpoint endpoint = new AlbumEndpoint(await ImgurHelper.GetClient());
                ImgurAlbum             = await endpoint.GetAlbumAsync(Id);
                bool owned             = (await ImgurHelper.GetToken()).AccountId == ImgurAlbum.AccountId.ToString();

                // (A) filter for files only in local (not in remote)
                FileInfo[] onlyLocal = local.Where(l => !ImgurAlbum.Images.Select(r => new Image(r)).Any(r => r.FileName == l.Name)).ToArray();
                // (B) filter for files only in remote (not in local)
                Image[] onlyRemote = ImgurAlbum.Images.Select(r => new Image(r)).Where(r => !local.Any(l => r.FileName == l.Name)).ToArray();

                if (owned)                 //own album
                {
                    // upload files (A) where not in json
                    FileInfo[] upload = onlyLocal.Where(l => !Images.Any(i => i == l.Name)).ToArray();
                    foreach (FileInfo img in upload)
                    {
                        Image image = new Image();
                        image.Name  = Path.GetFileNameWithoutExtension(img.Name);
                        image.Link  = img.Name;
                        await image.Upload(this);
                    }
                    // download files (B) where not in json
                    Image[] download = onlyRemote.Where(r => !Images.Any(i => i == r.FileName)).ToArray();
                    foreach (Image img in download)
                    {
                        await img.Download(this);
                    }
                    // delete files (A) from local where in json
                    if (Properties.Settings.Default.DeleteLocalImage)
                    {
                        FileInfo[] deleteLocal = onlyLocal.Where(l => Images.Any(i => i == l.Name)).ToArray();
                        foreach (FileInfo img in deleteLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local image?", img.FullName, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                File.Delete(img.FullName);
                            }
                        }
                    }
                    // delete files (B) from remote where in json
                    if (Properties.Settings.Default.DeleteRemoteImage)
                    {
                        Image[] deleteRemote = onlyRemote.Where(r => Images.Any(i => i == r.FileName)).ToArray();
                        foreach (Image img in deleteRemote)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteRemoteImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete Imgur image?", img.Name, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                await img.Delete();
                            }
                        }
                    }
                }
                else                 //not own album
                {
                    // remove files (A)
                    if (Properties.Settings.Default.DeleteLocalImage)
                    {
                        foreach (FileInfo img in onlyLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalImage)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local image?", img.FullName, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                File.Delete(img.FullName);
                            }
                        }
                    }
                    // download files (B)
                    foreach (Image img in onlyRemote)
                    {
                        await img.Download(this);
                    }
                }

                // update json
                string[] images = new DirectoryInfo(Root).GetFiles().Where(it => new string[] { ".jpg", ".gif", ".png", ".bmp" }.Contains(it.Extension.ToLowerInvariant())).Select(it => it.Name).ToArray();

                App.Current.Dispatcher.Invoke((Action) delegate
                {
                    Images.Clear();
                    foreach (string img in images)
                    {
                        Images.Add(img);
                    }
                });
                Save();
            });
        }
示例#7
0
文件: User.cs 项目: sunk818/Syncurr
        public async Task Sync(object context)
        {
            await Task.Run(async() =>
            {
                // get account
                AccountEndpoint endpoint = new AccountEndpoint(await ImgurHelper.GetClient());
                ImgurUser = await endpoint.GetAccountAsync(Name);
                bool me   = (await ImgurHelper.GetToken()).AccountId == ImgurUser.Id.ToString();

                // get local albums
                Album[] local = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => Album.Get(it.FullName, "", null, true)).ToArray();

                // get remote albums
                IAlbum[] remote = (await endpoint.GetAlbumsAsync(Name)).ToArray();

                // (A) filter for albums with id only in local (not in remote)
                Album[] onlyLocal = local.Where(l => l.Id != null && !remote.Any(r => r.Id == l.Id)).ToArray();

                // (B) filter for albums only in remote (not in local)
                IAlbum[] onlyRemote = remote.Where(r => !local.Any(l => l.Id == r.Id)).ToArray();

                if (me)
                {
                    // download albums (B) where not in json
                    IAlbum[] download = onlyRemote.Where(r => !Albums.Any(a => a.Id == r.Id)).ToArray();
                    foreach (IAlbum album in download)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                    // delete albums (A) from local where in json
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        Album[] deleteLocal = onlyLocal.Where(l => Albums.Any(a => a.Id == l.Id && a.Synchronize)).ToArray();
                        foreach (Album album in deleteLocal)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                Directory.Delete(album.Root, true);
                            }
                        }
                    }
                    // delete albums (B) from remote where in json
                    if (Properties.Settings.Default.DeleteRemoteFolder)
                    {
                        IAlbum[] deleteRemote = onlyRemote.Where(r => Albums.Any(a => a.Id == r.Id && a.Synchronize)).ToArray();
                        foreach (IAlbum album in deleteRemote)
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteRemoteFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete Imgur album?", album.Title, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                await endpoint.DeleteAlbumAsync(album.Id, Name);
                            }
                        }
                    }
                    // create albums without id
                    Album[] upload = local.Where(l => l.Id == null && l.Synchronize).ToArray();
                    foreach (Album album in upload)
                    {
                        AlbumEndpoint ep = new AlbumEndpoint(await ImgurHelper.GetClient());
                        IAlbum rAlbum    = await ep.CreateAlbumAsync(album.Title);
                        album.ImgurAlbum = rAlbum;
                        await album.Sync(context);
                    }
                }
                else
                {
                    // remove albums (A)
                    if (Properties.Settings.Default.DeleteLocalFolder)
                    {
                        foreach (Album album in onlyLocal.Where(it => it.Synchronize))
                        {
                            bool ok = true;
                            if (Properties.Settings.Default.AskDeleteLocalFolder)
                            {
                                MessageDialogResult result = await DialogCoordinator.Instance.ShowMessageAsync(context, "Delete local album?", album.Root, MessageDialogStyle.AffirmativeAndNegative);
                                ok = result == MessageDialogResult.Affirmative;
                            }
                            if (ok)
                            {
                                album.Remove();
                            }
                        }
                    }
                    // download albums (B)
                    foreach (IAlbum album in onlyRemote)
                    {
                        await Album.Get(Root, album.Title ?? album.Id, album.Id, true).Sync(context);
                    }
                }

                // udpate json
                AlbumRoots     = new DirectoryInfo(Root).GetDirectories().Where(it => it.Name[0] != '.').Select(it => it.FullName).ToList();
                Album[] albums = AlbumRoots.Select(it => Album.Get(it, "")).ToArray();

                App.Current.Dispatcher.Invoke((Action) delegate
                {
                    Albums.Clear();
                    foreach (Album album in albums)
                    {
                        Albums.Add(album);
                    }
                });
                await Save();

                // synchronize album images
                foreach (Album album in albums)
                {
                    await album.Sync(context);
                }
            });
        }
示例#8
0
        public void Process(Enums.Sources source, Enums.Classes classification = Enums.Classes.Any)
        {
            var url = "";

            try
            {
                //Get the board URL.
                var board = Core.Constants.SourceUrls[source];
                Helpers.LogMessage($"Starting Board: {board}");

                var tabs = new List <string> {
                    "top", "new", ""
                };

                foreach (var tab in tabs)
                {
                    var boardurl = $"{board}{tab}";
                    var lastPost = "";
                    //loop pages
                    for (var x = 1; x <= Settings.MaxPages; x++)
                    {
                        if (x != 1)
                        {
                            url = $"?count={(x - 1) * 25}&after={lastPost}";
                        }

                        Helpers.LogMessage($"Getting Page Contents for {boardurl}{url}");
                        var pageContents = Helpers.GetPageContents(boardurl + url);


                        // - - get threads on pages
                        //Helpers.LogMessage($"Getting Page Threads");
                        var threads = GetPageThreads(pageContents);
                        //Helpers.LogMessage($"Threads found :: {threads.Count}");

                        if (!threads.Any())
                        {
                            break;
                        }
                        //loop the threads on the this page
                        foreach (var thread in threads)
                        {
                            var imagelink = thread.GetAttribute("data-domain");
                            var dataUrl   = thread.GetAttribute("data-url");

                            try
                            {
                                if (imagelink == "imgur.com")
                                {
                                    var ext = dataUrl.Split("/").Last().Split(".").Last();
                                    if (new[] { "png", "jpeg", "jpg", "gif", "png" }.Contains(ext))
                                    {
                                        imagelink = "i.imgur.com";
                                    }
                                }
                            }
                            catch
                            {
                                //do nothing
                            }

                            //determine the action to run
                            switch (imagelink)
                            {
                            case "i.redd.it":
                            //if domain = i.redd.it this is a direct link
                            case "i.imgur.com":
                                //if domain = i.imgur.com this is a direct link
                                var image = new ImageDetail
                                {
                                    IndexSource = source
                                };

                                if (classification != Enums.Classes.Any)
                                {
                                    image.Class = classification;
                                }

                                if (!GetImageUrl(thread, image))
                                {
                                    continue;
                                }

                                GetWhoStamp(thread, image);

                                GetResolution(thread, image);

                                GetTags(thread, image);

                                try
                                {
                                    image.ImageId        = thread.GetAttribute("data-fullname");
                                    image.ImageExtension =
                                        image.ImageUrl?.Split('/')?.Last()?.Split('.')?.Last() ?? "";
                                }
                                catch
                                {
                                    // pass
                                }

                                // - - - - check if images have been scrapped before
                                if (ScrapeRepositories.ImageScrapeRepository.ExistsAsync(image.ImageId,
                                                                                         imagelink == "i.imgur.com" ? Enums.Sources.Imgur : source,
                                                                                         CancellationToken.None).GetAwaiter().GetResult())
                                {
                                    continue;
                                }


                                //Helpers.LogMessage($"Image not in Scrape Repo {image.ImageId}");

                                if (image != null)
                                {
                                    ScrapeRepositories.Queue.Enqueue(image);
                                }

                                break;

                            case "imgur.com":
                                //if domain = imgur.com this is a gallery gotta go loop this shit
                                ImgurHelper.ProcessImgurGallery(thread.GetAttribute("data-url") ?? "", source,
                                                                classification);
                                break;
                            }
                        }

                        if (threads.Any())
                        {
                            lastPost = threads.Last().GetAttribute("data-fullname");
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
 public string GetUrlSmallThumbnail(string imgurId, bool isHttps)
 {
     return(ImgurHelper.GetUrlSmallThumbnail(imgurId, isHttps));
 }
 public string GetUrlSmallSquare(string imgurId, bool isHttps)
 {
     return(ImgurHelper.GetUrlSmallSquare(imgurId, isHttps));
 }
 public string GetImgurUrl(string imgurId, bool isThumbnail, bool isHttps)
 {
     return(ImgurHelper.GetImgurUrl(imgurId, isThumbnail, isHttps));
 }
 public string GetFullResImageUrl(string imgurId, bool isHttps)
 {
     return(ImgurHelper.GetFullResImageUrl(imgurId, isHttps));
 }
 public string GetAlternateImageUrl(string url)
 {
     return(ImgurHelper.GetAlternateImageUrl(url));
 }
示例#14
0
 public async Task Delete()
 {
     ImageEndpoint endpoint = new ImageEndpoint(await ImgurHelper.GetClient());
     await endpoint.DeleteImageAsync(Id);
 }