Advertisement
cgrunwald

Untitled

Oct 30th, 2010
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. // /* This is a Reversing Juntalis project.
  2. // *
  3. // * Copyright (C) 2010
  4. // * http://juntalis.com
  5. // */
  6.  
  7. #region
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Net;
  13. using System.Text;
  14. using System.Text.RegularExpressions;
  15. using System.Web;
  16. using HtmlAgilityPack;
  17.  
  18. #endregion
  19.  
  20. namespace NSnipplr
  21. {
  22. /// <summary>
  23. /// Do a search for a snippet.
  24. /// </summary>
  25. public class Search
  26. {
  27. private readonly List<SnippetSearchResult> _lstResults;
  28.  
  29. /// <summary>
  30. /// Constructor
  31. /// </summary>
  32. /// <param name = "query">What to search for</param>
  33. public Search(string query)
  34. {
  35. _lstResults = new List<SnippetSearchResult>();
  36. }
  37.  
  38. /// <summary>
  39. /// Constructor
  40. /// </summary>
  41. public Search()
  42. {
  43. _lstResults = new List<SnippetSearchResult>();
  44. }
  45.  
  46. /// <summary>
  47. /// </summary>
  48. public List<SnippetSearchResult> Results
  49. {
  50. get { return _lstResults; }
  51. }
  52.  
  53. /// <summary>
  54. /// Execute the search
  55. /// </summary>
  56. /// <param name = "query">What to search for.</param>
  57. /// <param name = "language">Language to search in.</param>
  58. /// <returns></returns>
  59. public bool Execute(string query, SnippetLanguage language)
  60. {
  61. HttpWebResponse objResponse;
  62. string sLanguage = String.Format("{0}", language);
  63. string sSession;
  64. if (!getCookies(out sSession)) return false;
  65. if (doRequest(query, sLanguage, sSession, out objResponse)) {
  66. if (objResponse.StatusCode == HttpStatusCode.OK) {
  67. Stream resStream = objResponse.GetResponseStream();
  68. StringBuilder sb = new StringBuilder();
  69. byte[] buf = new byte[512];
  70. int count;
  71. do {
  72. if (resStream != null) count = resStream.Read(buf, 0, buf.Length);
  73. else return false;
  74. if (count == 0) continue;
  75. string tempString = Encoding.ASCII.GetString(buf, 0, count);
  76. sb.Append(tempString);
  77. } while (count > 0);
  78. objResponse.Close();
  79. string sResultsDirty =
  80. Regex.Match(sb.ToString(), "<ol class=\"snippets marg\">(?<results>.+?)</ol>",
  81. RegexOptions.Singleline | RegexOptions.IgnoreCase).Groups["results"].Value;
  82. traverseDOM("<ol>" + sResultsDirty + "</ol>");
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88.  
  89. /// <summary>
  90. /// Internal method for the actual HTTP web request.
  91. /// </summary>
  92. /// <param name = "query"></param>
  93. /// <param name = "language"></param>
  94. /// <param name = "cookie"></param>
  95. /// <param name = "response"><see cref = "WebResponse" /> of the result.</param>
  96. /// <returns><see cref = "bool" />Did it work?</returns>
  97. protected internal bool doRequest(string query, string language, string cookie, out HttpWebResponse response)
  98. {
  99. response = null;
  100. try {
  101. HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://snipplr.com/search.php");
  102. request.UserAgent =
  103. "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729; .NET4.0E)";
  104. request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json";
  105. request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
  106. request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
  107. request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  108. request.Headers.Add("Keep-Alive", "115");
  109. request.Headers.Set(HttpRequestHeader.Cookie, @"PHPSESSID=" + cookie + ";");
  110. request.KeepAlive = true;
  111. request.Referer = "http://snipplr.com/search.php?advanced";
  112. request.ContentType = "application/x-www-form-urlencoded";
  113. request.Method = "POST";
  114. string postString = @"q=" + HttpUtility.UrlEncode(query) + @"&lang=" + language + @"&scope=all&btnsubmit=SEARCH";
  115. byte[] postBytes = Encoding.UTF8.GetBytes(postString);
  116. Stream stream = request.GetRequestStream();
  117. stream.Write(postBytes, 0, postBytes.Length);
  118. stream.Close();
  119. response = (HttpWebResponse) request.GetResponse();
  120. } catch (WebException e) {
  121. if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse) e.Response;
  122. else return false;
  123. } catch (Exception) {
  124. if (response != null) response.Close();
  125. return false;
  126. }
  127.  
  128. return true;
  129. }
  130.  
  131. /// <summary>
  132. /// Tries to request the URL: http://snipplr.com/search.php?advanced
  133. /// </summary>
  134. /// <param name = "cookies">Cookie collection returned by Snipplr</param>
  135. /// <returns>True if the request was successful; false otherwise.</returns>
  136. protected internal bool getCookies(out string cookie)
  137. {
  138. HttpWebResponse response = null;
  139. try {
  140. //Create request to URL.
  141. HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://snipplr.com/search.php?advanced");
  142.  
  143. //Set request headers.
  144. request.UserAgent =
  145. "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729; .NET4.0E)";
  146. request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json";
  147. request.Headers.Set(HttpRequestHeader.AcceptLanguage, "en-us,en;q=0.5");
  148. request.Headers.Set(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
  149. request.Headers.Set(HttpRequestHeader.AcceptCharset, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
  150. request.Headers.Add("Keep-Alive", "115");
  151. request.KeepAlive = true;
  152. request.Referer = "http://snipplr.com/search.php";
  153.  
  154. //Get response to request.
  155. response = (HttpWebResponse) request.GetResponse();
  156. } catch (WebException e) {
  157. if (e.Status == WebExceptionStatus.ProtocolError) response = (HttpWebResponse) e.Response;
  158. else {
  159. cookie = null;
  160. return false;
  161. }
  162. } catch (Exception) {
  163. if (response != null) response.Close();
  164. cookie = null;
  165. return false;
  166. }
  167. cookie =
  168. Regex.Match(response.GetResponseHeader("Set-Cookie"), "PHPSESSID=(?<sessid>.+?);",
  169. RegexOptions.Singleline | RegexOptions.IgnoreCase).Groups["sessid"].Value;
  170. response.Close();
  171. return true;
  172. }
  173.  
  174. /// <summary>
  175. /// Traverse the DOM to get results.
  176. /// </summary>
  177. /// <param name = "dirtyText">Regular expression treated response from our web request.</param>
  178. protected void traverseDOM(string dirtyText)
  179. {
  180. if (string.IsNullOrEmpty(dirtyText)) return;
  181. HtmlDocument docTraversal = new HtmlDocument();
  182. docTraversal.LoadHtml(dirtyText);
  183.  
  184. // Cycle through nodes.
  185. foreach (HtmlNode snippet in docTraversal.DocumentNode.SelectNodes("/ol[1]/li")) {
  186. HtmlNode nodeSnippet = snippet.Element("h3").ChildNodes[1];
  187. SnippetSearchResult result = new SnippetSearchResult
  188. {
  189. Title = nodeSnippet.InnerText,
  190. Url = nodeSnippet.GetAttributeValue("href", string.Empty)
  191. };
  192. result.Id = Regex.Match(result.Url, @"^/view/(?<id>\d+)/.+/$", RegexOptions.IgnoreCase).Groups["id"].Value;
  193. result.Language = snippet.Element("span").Element("a").InnerText;
  194. _lstResults.Add(result);
  195. }
  196. }
  197. }
  198.  
  199. /// <summary>
  200. /// </summary>
  201. public struct SnippetSearchResult
  202. {
  203. /// <summary>
  204. /// </summary>
  205. public string Title { get; set; }
  206.  
  207. /// <summary>
  208. /// </summary>
  209. public string Language { get; set; }
  210.  
  211. /// <summary>
  212. /// </summary>
  213. public string Url { get; set; }
  214.  
  215. /// <summary>
  216. /// </summary>
  217. public string Id { get; set; }
  218. }
  219.  
  220.  
  221. /// <summary>
  222. /// Enum containing the languages we can search for.
  223. /// </summary>
  224. public enum SnippetLanguage
  225. {
  226. // ReSharper disable DeclarationDoesntConformToNamingConventions
  227. // ReSharper disable InconsistentNaming
  228. All = 0,
  229. ActionScript = 11,
  230. ActionScriptThree = 45,
  231. Apache = 46,
  232. AppleScript = 16,
  233. ASP = 29,
  234. Assembler = 28,
  235. AutoIt = 65,
  236. Awk = 83,
  237. Bash = 31,
  238. C = 69,
  239. CSharp = 19,
  240. Cpp = 14,
  241. Clojure = 80,
  242. ColdFusion = 38,
  243. CSS = 5,
  244. Delphi = 59,
  245. Diff = 30,
  246. Django = 57,
  247. DOSBatch = 64,
  248. EmacsLisp = 44,
  249. eZPublish = 49,
  250. Forth = 33,
  251. Fortran = 21,
  252. Gnuplot = 82,
  253. Groovy = 37,
  254. HAML = 66,
  255. Haskell = 77,
  256. HTML = 2,
  257. iPhone = 63,
  258. Java = 12,
  259. JavaScript = 1,
  260. jQuery = 76,
  261. LaTeX = 54,
  262. lighttpd = 61,
  263. Lisp = 17,
  264. Lua = 24,
  265. Makefile = 73,
  266. MatLab = 20,
  267. Maxscript = 74,
  268. Mel = 84,
  269. MXML = 58,
  270. MySQL = 75,
  271. NewtonScript = 39,
  272. ObjectiveC = 9,
  273. OpenFirmware = 32,
  274. Other = 3,
  275. Pascal = 27,
  276. Perl = 6,
  277. PHP = 4,
  278. PicBasic = 36,
  279. PLSQL = 72,
  280. Processing = 68,
  281. Prolog = 50,
  282. Pseudocode = 79,
  283. Python = 7,
  284. R = 47,
  285. Rails = 42,
  286. Regex = 34,
  287. Revolution = 71,
  288. Ruby = 8,
  289. SAS = 56,
  290. SASS = 67,
  291. Scala = 62,
  292. Scheme = 51,
  293. SmallTalk = 22,
  294. Smarty = 35,
  295. SML = 40,
  296. SPSS = 70,
  297. SQL = 10,
  298. SVN = 18,
  299. Symfony = 81,
  300. TCL = 23,
  301. Textpattern = 60,
  302. TYPO3 = 48,
  303. VbNET = 25,
  304. VHDL = 26,
  305. VisualBasic = 43,
  306. WLanguage = 41,
  307. PowerShell = 55,
  308. WinRegistry = 52,
  309. XHTML = 53,
  310. XML = 13,
  311. XSLT = 78
  312. // ReSharper restore InconsistentNaming
  313. // ReSharper restore DeclarationDoesntConformToNamingConventions
  314. }
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement