Exemplo n.º 1
0
        public static void Auth()
        {
            if (FlickrManager.OAuthToken == null)
            {
                var f            = FlickrManager.GetInstance();
                var requestToken = f.OAuthGetRequestToken("oob");

                string url = f.OAuthCalculateAuthorizationUrl(requestToken.Token, AuthLevel.Write);

                System.Diagnostics.Process.Start(url);

                Console.Write("Auth token: ");
                var token = Console.ReadLine().Trim();
                try {
                    var accessToken = f.OAuthGetAccessToken(requestToken, token);
                    FlickrManager.OAuthToken = accessToken;
                    Console.WriteLine("Successfully authenticated as " + accessToken.FullName);
                } catch (FlickrApiException ex) {
                    Console.WriteLine("Failed to get access token. Error message: " + ex.Message);
                }
                FlickrConfig.Save();
            }
            else
            {
                var accessToken = FlickrManager.OAuthToken;
                Console.WriteLine("Successfully authenticated as " + accessToken.FullName);
            }
        }
Exemplo n.º 2
0
 public static void Load()
 {
     Directory.CreateDirectory(Path.GetDirectoryName(ConfigFile));
     Console.WriteLine("Load config from: " + ConfigFile);
     if (File.Exists(ConfigFile))
     {
         _instance = File.ReadAllText(ConfigFile).FromJson <FlickrConfig> () ?? new FlickrConfig();
     }
     else
     {
         _instance = new FlickrConfig();
     }
 }
Exemplo n.º 3
0
        static async Task Run(string[] args)
        {
            var do_full = args.Contains("--full");

            Console.WriteLine("Parameters:");
            Console.WriteLine("  full: " + (do_full ? "true" : "false"));

            Console.WriteLine("");
            Console.WriteLine("Set this:\n" +
                              "  http://www.flickr.com/account/geo/privacy/?from=privacy\n" +
                              "to 'Anyone', and tick this one:\n" +
                              "  http://www.flickr.com/account/geo/exif/?from=privacy\n" +
                              "These affect only SUBSEQUENT uploads.\n");
            Console.WriteLine("");

            FlickrConfig.Load();
            LocalDatabase.Load();

            FlickrAuth.Auth();
            var indexLocal     = new LocalFileIndex();
            var photosToUpload = new LocalFile[0];

            if (do_full)
            {
                await indexLocal.Build();

                var indexRemote = new RemoteFileIndex();
                await indexRemote.Build();

                photosToUpload = indexLocal.Photos
                                 .Where(p => !indexRemote.ByDesc.ContainsKey(p.Description))
                                 .OrderByDescending(p => p.FullPath)
                                 .ToArray();
            }
            else
            {
                await indexLocal.Build();

                photosToUpload = indexLocal.Photos
                                 .Where(p => !LocalDatabase.Instance.RemoteFiles.ContainsKey(p.Description))
                                 .OrderByDescending(p => p.FullPath)
                                 .ToArray();
            }

            // geo tagging
            var remoteMissingGeoPhotos = indexLocal.Photos.Where(p => p.GeoLocation.IsNonZero).Select(p => {
                RemoteFile rf;
                if (LocalDatabase.Instance.RemoteFiles.TryGetValue(p.Description, out rf))
                {
                    if (rf.GeoLocation.IsZero)
                    {
                        return(new { local = p, remote = rf });
                    }
                }
                return(null);
            }).Where(o => o != null).Select((o, j) => new { local = o.local, remote = o.remote, i = j }).ToArray();

            Console.WriteLine("Local photos with geo location: " + indexLocal.Photos.Count(p => p.GeoLocation.IsNonZero));
            Console.WriteLine("Remote photos with geo location: " + LocalDatabase.Instance.RemoteFiles.Values.Count(p => p.GeoLocation.IsNonZero));
            Console.WriteLine("Local photos with geo location that are missing on flickr: " + remoteMissingGeoPhotos.Length);
            foreach (var o in remoteMissingGeoPhotos)
            {
                FlickrUploader.FixGeoLocation(o.local, o.remote);
                await Task.Delay(1000);

                if (o.i % 50 == 0)
                {
                    LocalDatabase.RunLocked(() => {
                        LocalDatabase.Save();
                    });
                }
            }
            LocalDatabase.Save();

            // upload
            // photosToUpload = photosToUpload.Where (p => p.FullPath.ContainsAny ("Francisco", "York", "Reisen", "Tante", "Familie", "Thomas", "Schule", "arty", "Ausfluege", "Maxi", "Tobias")).ToArray ();

            var r = new Random();
            int i = 0;

            foreach (var p in photosToUpload)
            {
                FlickrUploader.Upload(p, i, photosToUpload.Length);

                // sleep depending on the time of day
                var  timeofday = DateTime.Now.TimeOfDay;
                var  dayofweek = (int)DateTime.Now.DayOfWeek;
                var  hours     = timeofday.TotalHours;
                bool isDayTime = false;
                if ((dayofweek == 0 || dayofweek >= 5))                   // weekend
                {
                    if (hours >= 9.5 || hours <= 1.5)
                    {
                        isDayTime = true;
                    }
                }
                else
                {
                    if (hours >= 9.5 || hours <= 0.33)
                    {
                        isDayTime = true;
                    }
                }
                if (isDayTime)
                {
                    LocalDatabase.Save();
                    var sleep = r.Next(20, 70);
                    Console.WriteLine("It's " + ((int)hours).ToString().PadLeft(2, '0') + ":" + timeofday.Minutes.ToString().PadLeft(2, '0') + ", so we wait " + sleep + " seconds");
                    await Task.Delay(sleep * 1000);
                }
                else if (i % 10 == 0)
                {
                    LocalDatabase.Save();
                }

                i++;
            }
        }
Exemplo n.º 4
0
        public async Task Build()
        {
            while (FlickrConfig.Instance.BaseDirectory == null)
            {
                Console.Write("Base Directory: ");
                var dir = Console.ReadLine().Trim();
                if (Directory.Exists(dir))
                {
                    FlickrConfig.Instance.BaseDirectory = dir;
                    FlickrConfig.Save();
                }
            }

            var photos = new List <LocalFile> ();
            var videos = new List <LocalFile> ();
            var others = new List <LocalFile> ();
            var byDesc = new DefaultMap <string, LocalFile> ();

            var baseDir           = FlickrConfig.Instance.BaseDirectory;
            var baseDirNormalized = baseDir.NormalizePath();

            foreach (string fullPath in Directory.EnumerateFiles(baseDir, "*.*", SearchOption.AllDirectories))
            {
                var fullPathNormalized     = fullPath.NormalizePath();
                var relativePathNormalized = fullPathNormalized.Replace(baseDirNormalized, string.Empty).NormalizePath();
                var file = new LocalFile(fullPath: fullPath, relativePathNormalized: relativePathNormalized);

                var ext = Path.GetExtension(fullPath);
                if (ext.IsAny(".jpg", ".png" /*, ".gif"*/))
                {
                    photos.Add(file);
                }
                else if (ext.IsAny(".mp4", ".mkv"))
                {
                    videos.Add(file);
                }
                else
                {
                    others.Add(file);
                    //Console.WriteLine (file);
                }
                byDesc [file.Description] = file;
            }

            Photos = photos.ToArray();
            Videos = videos.ToArray();
            Others = others.ToArray();
            ByDesc = byDesc;

            // geo location?
            var photosJpeg           = photos.Where(p => p.FullPath.EndsWithAny("jpg", "jpeg")).ToArray();
            var photosJpegWithoutGeo = new List <LocalFile> ();

            foreach (var file in photosJpeg)
            {
                string geoStr;
                if (LocalDatabase.Instance.GeoLocations.TryGetValue(file.Description, out geoStr))
                {
                    file.GeoLocation = GeoLocation.Deserialize(geoStr);
                }
                else
                {
                    photosJpegWithoutGeo.Add(file);
                }
            }
            Console.WriteLine("Local photos without geo location: " + photosJpegWithoutGeo.Count + " of " + photosJpeg.Length);
            var parallelism = 4;
            var i           = 0;
            var tasks       = new Task[parallelism];
            var timeStart   = DateTime.Now;

            for (int m = 0; m < parallelism; m++)
            {
                tasks [m] = parallel(photosJpegWithoutGeo, parallelism, m, (file) => {
                    var geo          = ExifHelper.GetLatlngFromImage(file.FullPath);
                    file.GeoLocation = geo;
                    LocalDatabase.RunLocked(() => {
                        i++;
                        LocalDatabase.Instance.GeoLocations [file.Description] = geo.Serialize();
                    });
                    if (i % 200 == 0)
                    {
                        LocalDatabase.RunLocked(() => {
                            var timeNow       = DateTime.Now;
                            var speed         = (timeNow - timeStart).TotalMilliseconds / Math.Max(i, 1);
                            var timeRemaining = TimeSpan.FromMilliseconds(speed * (photosJpegWithoutGeo.Count - i));
                            LocalDatabase.Save();
                            Console.WriteLine("  " + i + " of " + photosJpegWithoutGeo.Count + " (" + Math.Round(10000.0 / (double)photosJpegWithoutGeo.Count * (double)i) / 100 + "%, remaining: " + new DateTime(timeRemaining.Ticks).ToString("HH:mm:ss") + ")");
                        });
                    }
                });
            }
            await Task.WhenAll(tasks);

            LocalDatabase.Save();
        }