captcha.d (1968B)
1 import fbstream; 2 import std.net.curl; 3 import std.stdio; 4 import std.file; 5 import std.path; 6 import std.regex; 7 import std.format; 8 import std.range; 9 import std.process; 10 import std.algorithm.searching; 11 12 13 immutable string probe_url="https://www.facebook.com/Facebook"; 14 15 /** 16 * Tries to fetch the captcha and set the cookies 17 * 18 * Returns: 0 if the captcha is solved, 1 otherwise. 19 */ 20 int main(string[] args){ 21 auto h=HTTP(); 22 char[] buf; 23 24 h.url=probe_url; 25 h.setUserAgent(FBStream.userAgent); 26 h.setCookieJar(getCookiePath()); 27 h.onReceive = (ubyte[] data){ 28 buf~=cast(char[])data; 29 return data.length; 30 }; 31 h.perform(); 32 33 if(FBStream.captchaSolved(buf)){ 34 writeln("Captcha already solved :)"); 35 return 0; 36 } 37 38 auto url_regex=ctRegex!(".*(https://www.facebook.com/captcha/tfbimage.php[^\"]+).*"); 39 auto url=matchFirst(buf, url_regex)[1]; 40 auto datr_regex=ctRegex!(".*\"_js_datr\",\"([^\"]+)\".*"); 41 auto datr=matchFirst(buf, datr_regex); 42 43 h.setCookie("_js_datr="~datr[1]); 44 45 46 auto captcha_regex=ctRegex!(".*name=\"captcha_persist_data\" value=\"([^\"]+)\".*"); 47 auto captcha_hash=matchFirst(buf, captcha_regex)[1]; 48 49 buf=null; 50 h.url=url; 51 h.perform(); 52 53 File f; 54 string file=buildPath(tempDir(),"fb2rss_captcha.png"); 55 f.open(file, "w+"); 56 scope(exit){ 57 f.close(); 58 remove(file); 59 } 60 f.write(buf); 61 f.close(); 62 writeln("The captcha has been written to "~file); 63 writeln("Please enter the text below:"); 64 auto pid = spawnProcess(["/usr/bin/pqiv", "-i", file]); 65 66 char[] captcha; 67 readln(captcha); 68 kill(pid); 69 captcha=captcha[0..$-1]; //Exclude '\n' 70 71 buf=null; 72 h.url=probe_url; 73 h.method=HTTP.Method.post; 74 h.setPostData( 75 format( 76 "captcha_persist_data=%s&captcha_response=%s&captcha_submit=1", 77 captcha_hash, 78 captcha 79 ), 80 "application/x-www-form-urlencoded" 81 ); 82 h.perform(); 83 84 if(FBStream.captchaSolved(buf)){ 85 writeln("Success"); 86 } 87 else{ 88 writeln("Sorry, didn't work :C"); 89 writeln("Please, try again!"); 90 return 1; 91 } 92 return 0; 93 }