public static void FixGeoLocation(LocalFile file, RemoteFile remote)
 {
     try {
         Console.Write("Fix geo location: " + remote.Description + " (" + file.GeoLocation + ")");
         var f = FlickrManager.GetAuthInstance();
         f.OnUploadProgress += F_OnUploadProgress;
         f.PhotosGeoSetLocation(remote.PhotoId, file.GeoLocation.Lat, file.GeoLocation.Lng);
         Console.WriteLine();
         remote.GeoLocation = file.GeoLocation;
         LocalDatabase.RunLocked(() => LocalDatabase.Instance.RemoteFiles [remote.Description] = remote);
     } catch (Exception ex) {
         CountFailures++;
         if (CountFailures > 10)
         {
             throw;
         }
         Console.WriteLine("Exception during fix geo location:");
         Console.WriteLine(ex);
         int sec = 2 * Math.Min(5, CountFailures);
         Console.WriteLine("Wait for " + sec + " seconds");
         System.Threading.Thread.Sleep(1000 * sec);
     }
 }
 public static void Upload(LocalFile file, int i, int n)
 {
     try {
         Console.Write("Upload local file " + i + " of " + n + ": " + file + " => ");
         var f = FlickrManager.GetAuthInstance();
         f.OnUploadProgress += F_OnUploadProgress;
         string photoId = f.UploadPicture(file.FullPath, file.Title, file.Description, string.Join(",", new [] {
             FlickrManager.MasterTag,
             file.PathTag
         }.Union(file.RelativePathNormalized.Split('/'))), false, false, false);
         Console.WriteLine("photoId: " + photoId);
     } catch (Exception ex) {
         CountFailures++;
         if (CountFailures > 10)
         {
             throw;
         }
         Console.WriteLine("Exception during upload:");
         Console.WriteLine(ex);
         int sec = 10 * Math.Min(5, CountFailures);
         Console.WriteLine("Wait for " + sec + " seconds");
         System.Threading.Thread.Sleep(1000 * sec);
     }
 }
Esempio 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++;
            }
        }
Esempio 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();
        }