public static void Run(Database db) { _db = db; ImageRecord selected = GetRandomImageRecord(); CleanDestination(); DownloadImage(selected, Globals.SymlinkToCurrent); _db.MarkImageDownloaded(selected); _db.Save(); }
public void Load() { if (_isDbLoaded) { return; } if (_dbExistsOnFs == DbExistsOnFileSystem.False) { Create(); } using (StreamReader sr = new StreamReader(_dbFullName)){ string line, table = String.Empty; while ((line = sr.ReadLine()) != null) { if (IsIgnoreableLine(line)) { continue; } if (IsTableHeader(line)) { table = GetTableName(line); } else { switch (table.ToUpper()) { case "IMAGES": var record = new ImageRecord(line); _images.Add(record.Key, record); break; default: throw new ArgumentException( $"Unhandled database table {table}"); } } } } _isDbLoaded = true; }
private static void DownloadImage(ImageRecord img, bool createSymlink) { string filename = createSymlink ? img["Name"] : $"{DateTime.Now.ToString("yyMMddHHmmss")}.jpg"; if (!Directory.Exists(Globals.PictureDir)) { Directory.CreateDirectory(Globals.PictureDir); } Console.WriteLine("Downlaoding image..."); using (WebClient client = new WebClient()){ client.DownloadFile( img["Url"], Path.Combine(Globals.PictureDir, filename)); } if (createSymlink) { CreateSymlink(img); } }
private static void CreateSymlink(ImageRecord img) { Process link = new Process { StartInfo = new ProcessStartInfo { # if LINUX FileName = "ln", Arguments = String.Format("-sfn {0} {1}", Path.Combine( Globals.PictureDir, img["Name"]), Path.Combine( Globals.PictureDir, "current")), # else FileName = "echo", Arguments = "Symlinking not supported for this platform. " + "Skipping.", # endif UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true } };
public void MarkImageDownloaded(ImageRecord img) { _images[img.Key] = img.ToggleHasBeenUsed(); }