示例#1
0
        public static void Run(Database db)
        {
            _db = db;
            ImageRecord selected = GetRandomImageRecord();

            CleanDestination();
            DownloadImage(selected, Globals.SymlinkToCurrent);
            _db.MarkImageDownloaded(selected);
            _db.Save();
        }
示例#2
0
        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;
        }
示例#3
0
        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);
            }
        }
示例#4
0
        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
                }
            };
示例#5
0
 public void MarkImageDownloaded(ImageRecord img)
 {
     _images[img.Key] = img.ToggleHasBeenUsed();
 }