/SeriesSelector/Data/MovieService.cs

https://github.com/LordTakeshiXVII/SeriesSelector · C# · 117 lines · 97 code · 20 blank · 0 comment · 9 complexity · d70b1a9d343dd6d93c96fd2fd6c2a766 MD5 · raw file

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.ComponentModel.Composition;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Net;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. using System.Xml;
  11. using SeriesSelector.Frame;
  12. using SeriesSelector.Properties;
  13. namespace SeriesSelector.Data
  14. {
  15. [Export(typeof(IMovieService))]
  16. public class MovieService : IMovieService
  17. {
  18. public IList<EpisodeType> GetMovies(string sourcePath)
  19. {
  20. var fileTypeValues = Settings.Default.FileTypes.Split('|');
  21. var fileList = new ArrayList();
  22. foreach (var fileTypeValue in fileTypeValues)
  23. {
  24. var di = Directory.GetFiles(sourcePath, string.Format("*.{0}", fileTypeValue),
  25. SearchOption.AllDirectories);
  26. fileList.AddRange(di);
  27. }
  28. IList<EpisodeType> movieList = new List<EpisodeType>();
  29. foreach (string file in fileList)
  30. {
  31. var episodeType = new EpisodeType();
  32. var fileName = Path.GetFileName(file);
  33. if (string.IsNullOrEmpty(fileName))
  34. continue;
  35. if (fileName.ToLower().Contains("sample"))
  36. continue;
  37. var checker = BootStrapper.ResolveAll<IEpisodeChecker>();
  38. Tuple<string, string> result = null;
  39. foreach (var c in checker)
  40. {
  41. result = c.CheckSeasonEpisode(CleanName(fileName));
  42. if (result != null)
  43. break;
  44. }
  45. if (result != null)
  46. continue;
  47. episodeType.FileName = fileName;
  48. episodeType.FullPath = file;
  49. episodeType.FileSize = Math.Round((((double)new FileInfo(file).Length) / 1048576), 2).ToString();
  50. movieList.Add(episodeType);
  51. }
  52. return movieList;
  53. }
  54. public IList<EpisodeType> TryMapMovies(IList<EpisodeType> list)
  55. {
  56. foreach (var movie in list)
  57. {
  58. var mappings = SearchFor(movie.FileName);
  59. if (mappings.Count == 1)
  60. {
  61. movie.NewName = mappings[0];
  62. movie.IsSelected = true;
  63. }
  64. movie.FileType = Path.GetExtension(movie.FileName);
  65. }
  66. return list;
  67. }
  68. public List<string> SearchFor(string oldName)
  69. {
  70. var requestName = CleanName(oldName).Replace(' ', '+');
  71. requestName = Settings.Default.FileTypes.Split('|').Aggregate(requestName, (current, ending) => current.Replace(ending, string.Empty));
  72. var resultList = new List<string>();
  73. var doc = new XmlDocument();
  74. doc.Load(string.Format("http://www.imdb.com/xml/find?xml=1&nr=1&tt=on&q={0}", requestName));
  75. var itemNodes = doc.SelectNodes("//IMDbResults/ResultSet/ImdbEntity");
  76. foreach (XmlNode itemNode in itemNodes)
  77. {
  78. var title = itemNode.InnerXml.Substring(0, itemNode.InnerXml.IndexOf('<'));
  79. var year = Regex.Match(itemNode.InnerXml, @"\d\d\d\d");
  80. resultList.Add(string.Format("{0} ({1})", title, year));
  81. }
  82. return resultList;
  83. }
  84. private string CleanName(string fileName)
  85. {
  86. var cleanedName = fileName;
  87. var cleanlist = Settings.Default.CleanList.Split('|');
  88. cleanedName = cleanlist.Aggregate(cleanedName.ToLower(), (current, word) => current.Replace(word.ToLower(), string.Empty));
  89. cleanedName = cleanedName.Replace('.', ' ');
  90. if(cleanedName.LastIndexOf(' ') >= 0)
  91. cleanedName = cleanedName.Insert(cleanedName.LastIndexOf(' '), ".").Remove(cleanedName.LastIndexOf(' '), 1);
  92. cleanedName = cleanedName.Replace('_', ' ');
  93. cleanedName = cleanedName.Trim();
  94. return cleanedName;
  95. }
  96. }
  97. }