Advertisement
Guest User

Govno

a guest
Aug 31st, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5.  
  6. namespace KonachanViewer
  7. {
  8. [Serializable]
  9. public class KonachanCollection
  10. {
  11. public ImageCollection Collection { get; }
  12.  
  13. private const string Pattern = @"[Kk]onachan.com\s*-\s*(?<number>\d+)\s*-*\s*(?<tags>[\w .-]+)";
  14.  
  15. public KonachanCollection(string path)
  16. {
  17. if (Directory.Exists(path))
  18. {
  19. Collection = new ImageCollection();
  20. var fileNames = Directory.GetFiles(path).Where(s => s.Contains("Konachan.com")).ToArray();
  21. var regex = new Regex(Pattern);
  22. foreach (var fileName in fileNames)
  23. {
  24. var match = regex.Match(fileName);
  25. var groups = match.Groups;
  26. var id = Int32.Parse(groups["number"].Value);
  27. var tags = groups["tags"].Value.Split(' ');
  28. Collection.Add(new Image(id, tags, fileName));
  29. }
  30. }
  31. }
  32.  
  33. public static ImageCollection CreateCollection(string path)
  34. {
  35. var collection = new ImageCollection();
  36. if (!Directory.Exists(path)) throw new DirectoryNotFoundException("Не найдена папка с картинками)))");
  37. var fileNames = Directory.GetFiles(path).Where(s => s.Contains("Konachan.com")).ToArray();
  38. var regex = new Regex(Pattern);
  39. foreach (var fileName in fileNames)
  40. {
  41. var match = regex.Match(fileName);
  42. var groups = match.Groups;
  43. var id = Int32.Parse(groups["number"].Value);
  44. var tags = groups["tags"].Value.Split(' ');
  45. collection.Add(new Image(id, tags, fileName));
  46. }
  47. return collection;
  48. }
  49.  
  50. public ImageCollection FindImages(params string[] tags)
  51. {
  52. return Collection.FindImages(tags);
  53. }
  54.  
  55. public ImageCollection GetImages(int count)
  56. {
  57. var col = new ImageCollection();
  58. for (int i = 0; i <= count; i++)
  59. {
  60. col.Add(Collection[i]);
  61. }
  62. return col;
  63. }
  64. }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement