/RiP-Ripper/Rip-Ripper/ImageHosts/ImageBam.cs

http://ripper.codeplex.com · C# · 204 lines · 149 code · 20 blank · 35 comment · 4 complexity · 1e6d324917f216026e471ccc2ed4fc2e MD5 · raw file

  1. //////////////////////////////////////////////////////////////////////////
  2. // Code Named: RiP-Ripper
  3. // Function : Extracts Images posted on RiP forums and attempts to fetch
  4. // them to disk.
  5. //
  6. // This software is licensed under the MIT license. See license.txt for
  7. // details.
  8. //
  9. // <copyright company="The Watcher" file="ImageBam.cs">
  10. // Copyright (c) The Watcher. Partial Rights Reserved.
  11. // </copyright>
  12. //////////////////////////////////////////////////////////////////////////
  13. // This file is part of the RiP Ripper project base.
  14. namespace RiPRipper.ImageHosts
  15. {
  16. #region
  17. using System;
  18. using System.Collections;
  19. using System.IO;
  20. using System.Net;
  21. using System.Text.RegularExpressions;
  22. using System.Threading;
  23. using System.Web;
  24. using RiPRipper.Objects;
  25. #endregion
  26. /// <summary>
  27. /// Worker class to get images hosted on ImageBam.com
  28. /// </summary>
  29. public class ImageBam : ServiceTemplate
  30. {
  31. #region Constructors and Destructors
  32. /// <summary>
  33. /// Initializes a new instance of the <see cref="ImageBam"/> class.
  34. /// </summary>
  35. /// <param name="sSavePath">
  36. /// The s save path.
  37. /// </param>
  38. /// <param name="strURL">
  39. /// The str url.
  40. /// </param>
  41. /// <param name="hTbl">
  42. /// The h tbl.
  43. /// </param>
  44. public ImageBam(ref string sSavePath, ref string strURL, ref string thumbURL, ref string imageName, ref Hashtable hTbl)
  45. : base(sSavePath, strURL, thumbURL, imageName, ref hTbl)
  46. {
  47. // Add constructor logic here
  48. }
  49. #endregion
  50. #region Methods
  51. /// <summary>
  52. /// Do the Download
  53. /// </summary>
  54. /// <returns>
  55. /// Returns if the Image was downloaded
  56. /// </returns>
  57. protected override bool DoDownload()
  58. {
  59. string strImgURL = this.ImageLinkURL;
  60. if (this.EventTable.ContainsKey(strImgURL))
  61. {
  62. return true;
  63. }
  64. try
  65. {
  66. if (!Directory.Exists(this.SavePath))
  67. {
  68. Directory.CreateDirectory(this.SavePath);
  69. }
  70. }
  71. catch (IOException ex)
  72. {
  73. MainForm.DeleteMessage = ex.Message;
  74. MainForm.Delete = true;
  75. return false;
  76. }
  77. string strFilePath = string.Empty;
  78. CacheObject ccObj = new CacheObject { IsDownloaded = false, FilePath = strFilePath, Url = strImgURL };
  79. try
  80. {
  81. this.EventTable.Add(strImgURL, ccObj);
  82. }
  83. catch (ThreadAbortException)
  84. {
  85. return true;
  86. }
  87. catch (Exception)
  88. {
  89. if (this.EventTable.ContainsKey(strImgURL))
  90. {
  91. return false;
  92. }
  93. this.EventTable.Add(strImgURL, ccObj);
  94. }
  95. string pageContent = this.GetImageHostPage(ref strImgURL);
  96. if (pageContent.Length < 10)
  97. {
  98. return false;
  99. }
  100. string strNewURL;
  101. var m = Regex.Match(pageContent, @";\"" src=\""(?<inner>[^\""]*)\"" alt=\""loading\""", RegexOptions.Singleline);
  102. if (m.Success)
  103. {
  104. strNewURL = m.Groups["inner"].Value;
  105. }
  106. else
  107. {
  108. return false;
  109. }
  110. strFilePath = strNewURL.Substring(strNewURL.LastIndexOf("/", StringComparison.Ordinal) + 1);
  111. if (strNewURL.Contains("amazonaws.com/bambackup/"))
  112. {
  113. var fileMatch = Regex.Match(
  114. strNewURL, @"bambackup/(?<inner>[^AWS]*)AWSAccessKeyId", RegexOptions.Singleline);
  115. if (fileMatch.Success)
  116. {
  117. strFilePath = string.Format("{0}.jpg", fileMatch.Groups["inner"].Value);
  118. }
  119. }
  120. if (strNewURL.Contains("filename="))
  121. {
  122. strFilePath = strNewURL.Substring(strNewURL.LastIndexOf("=", StringComparison.Ordinal) + 1);
  123. strNewURL = HttpUtility.HtmlDecode(strNewURL);
  124. }
  125. strFilePath = Path.Combine(this.SavePath, Utility.RemoveIllegalCharecters(strFilePath));
  126. //////////////////////////////////////////////////////////////////////////
  127. string sNewAlteredPath = Utility.GetSuitableName(strFilePath);
  128. if (strFilePath != sNewAlteredPath)
  129. {
  130. strFilePath = sNewAlteredPath;
  131. ((CacheObject)this.EventTable[this.ImageLinkURL]).FilePath = strFilePath;
  132. }
  133. try
  134. {
  135. WebClient client = new WebClient();
  136. client.Headers.Add(string.Format("Referer: {0}", strImgURL));
  137. client.DownloadFile(strNewURL, strFilePath);
  138. client.Dispose();
  139. }
  140. catch (ThreadAbortException)
  141. {
  142. ((CacheObject)this.EventTable[strImgURL]).IsDownloaded = false;
  143. ThreadManager.GetInstance().RemoveThreadbyId(this.ImageLinkURL);
  144. return true;
  145. }
  146. catch (IOException ex)
  147. {
  148. MainForm.DeleteMessage = ex.Message;
  149. MainForm.Delete = true;
  150. ((CacheObject)this.EventTable[strImgURL]).IsDownloaded = false;
  151. ThreadManager.GetInstance().RemoveThreadbyId(this.ImageLinkURL);
  152. return true;
  153. }
  154. catch (WebException)
  155. {
  156. ((CacheObject)this.EventTable[strImgURL]).IsDownloaded = false;
  157. ThreadManager.GetInstance().RemoveThreadbyId(this.ImageLinkURL);
  158. return false;
  159. }
  160. ((CacheObject)this.EventTable[this.ImageLinkURL]).IsDownloaded = true;
  161. CacheController.GetInstance().LastPic =
  162. ((CacheObject)this.EventTable[this.ImageLinkURL]).FilePath = strFilePath;
  163. return true;
  164. }
  165. #endregion
  166. //////////////////////////////////////////////////////////////////////////
  167. }
  168. }