post ads to facebook from perl

andyt

New member
Dec 20, 2006
678
9
0
Here's some code you can use to post advertisements to Facebook ads from perl.

If you use this code and want to donate, you can make a donation to a charity. I read about some pretty fucked up things happening today - Iranian Woman Sentenced to Death by Stoning - and Amnesty International wants to stop it.

So to donate to amnesty international:
Donate to Amnesty International | Amnesty International

it'd make me happy if you post here if you make a donation

Instructions:
It's written in perl, so save this code as fb.pl, fill out your username, password, the ad title, ad text, ad url, image filename, whether it's an existing campaign or new campaign (varname =old_campaign), and all the other variables that are hardcoded, then run "perl fb.pl"

Warning:
Be sensible when posting ads, don't post a ton of the same type and at the same time, start small, especially when getting used to submitting ads via an api.

Code:
#!/usr/bin/perl
use strict;
use LWP;
use HTTP::Cookies;
use HTTP::Request::Common;
use URI::Escape;

my ($l, $p) = ('fbadsusername@gmail.com', 'urpassword');

my $ct = '%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84';
my %codes = ('United Kingdom', 'GB', 'Jamaica', 'JM', 'Austria', 'AT', 'Denmark', 'DK', 'Panama', 'PA', 'Qatar', 'QA', 'Puerto Rico', 'PR', 'Japan', 'JP', 'Morocco', 'MA', 'Malta', 'MT', 'El Salvador', 'SV', 'Kuwait', 'KW', 'Italy', 'IT', 'Croatia', 'HR', 'Czech Republic', 'CZ', 'Spain', 'ES', 'France', 'FR', 'Egypt', 'EG', 'Bolivia', 'BO', 'Australia', 'AU', 'Ukraine', 'UA', 'Tunisia', 'TN', 'Belgium', 'BE', 'Estonia', 'EE', 'South Korea', 'KR', 'Saudi Arabia', 'SA', 'Norway', 'NO', 'South Africa', 'ZA', 'Portugal', 'PT', 'Turkey', 'TR', 'Canada', 'CA', 'Macedonia', 'MK', 'Greece', 'GR', 'Slovenia', 'SI', 'Colombia', 'CO', 'Poland', 'PL', 'Jordan', 'JO', 'United Arab Emirates', 'AE', 'Bosnia and Herzegovina', 'BA', 'Thailand', 'TH', 'Chile', 'CL', 'Philippines', 'PH', 'Iceland', 'IS', 'Nicaragua', 'NI', 'Netherlands', 'NL', 'Ecuador', 'EC', 'Hong Kong', 'HK', 'Malaysia', 'MY', 'Costa Rica', 'CR', 'Serbia', 'RS', 'Sweden', 'SE', 'China', 'CN', 'Bulgaria', 'BG', 'Uruguay', 'UY', 'The Bahamas', 'BS', 'Paraguay', 'PY', 'Nigeria', 'NG', 'Kenya', 'KE', 'Mauritius', 'MU', 'Switzerland', 'CH', 'Ghana', 'GH', 'Oman', 'OM', 'United States', 'US', 'Sri Lanka', 'LK', 'Peru', 'PE', 'Cyprus', 'CY', 'Indonesia', 'ID', 'Ireland', 'IE', 'Taiwan', 'TW', 'Latvia', 'LV', 'Russia', 'RU', 'Iraq', 'IQ', 'Lebanon', 'LB', 'Guatemala', 'GT', 'Germany', 'DE', 'Finland', 'FI', 'Maldives', 'MV', 'Luxembourg', 'LU', 'Pakistan', 'PK', 'Venezuela', 'VE', 'Bahrain', 'BH', 'Lithuania', 'LT', 'Romania', 'RO', 'Singapore', 'SG', 'Argentina', 'AR', 'Mexico', 'MX', 'Trinidad and Tobago', 'TT', 'Honduras', 'HN', 'Brazil', 'BR', 'Israel', 'IL', 'Slovakia', 'SK', 'Dominican Republic', 'DO', 'Hungary', 'HU', 'New Zealand', 'NZ', 'Palestine', 'PS', 'Bangladesh', 'BD');



my ($url, $title, $body, $campaign) = (uri_escape('http://trackingurl.com/?s=123'), 'Get a Girlfriend', 'Find thousands of hot local singles with online dating. You can\'t lose!', 'thecampaignname');
my ($old_campaign) = 0;
my ($age_min, $age_max) = (21, 60);
my ($sex, $interested, $rship) = ('male', 'women', 'single'); 	  # sex = male|female|all; interested = men|women|all; rship = single|married|all
my ($bid_type, $daily_budget, $bid_value) = ('cpc', '50.00', '0.10');
my $countries = 'United States';
my $image_file = 'path/to/imagefilename.jpg';

	$|++;
	my $ua = LWP::UserAgent->new(agent =>'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.0)');
	$ua->cookie_jar(new HTTP::Cookies( ignore_discard=>1, 'file' => './stuff/cookies.lwp', 'autosave' => 1));
	push @{ $ua->requests_redirectable }, 'POST';

	my $content = $ua->get('http://facebook.com')->content;
	my ($lsd) = $content=~/name="lsd" value="(.*?)"/s;
	print "need login\n" and login() if ($content=~/login.php/s);

	submit_ad();



sub submit_ad {

	my $create = 'http://www.facebook.com/ads/create/';

	my $content = $ua->get($create)->content;
#	open (FL, ">1.html"); print FL $content; close FL;
        for ($content) {
		my ($dtsg, $act) = /fb_dtsg" value="(.*?)".*?name="act" value="(.*?)"/s;
		my ($form_id) = /"post_form_id" value="(.*?)"/s;

		my ($imgform_url) = /Image_Frame" src="(.*?)"/;
		my ($img_id) = $ua->get('http://www.facebook.com'.$imgform_url)->content=~/.+value="(.*?)" name="UIAdCreativeImage_Id"/s;
		my $req = POST 'http://creativeupload.facebook.com/ads/create/photos/admanager_creative_photo_upload.php', Content_Type => 'multipart/form-data', Content => [
			'post_form_id' => $form_id, 'fb_dtsg' => $dtsg, 'charset_test' => $ct, 'UIAdCreativeImage_FileUpload' => [$image_file], 'UIAdCreativeImage_RedirectUrl' => '/ads/create/photos/creative_uploader.php', 'UIAdCreativeImage_Id' => $img_id
		];
		my ($ipath, $isrc, $ivol, $iw, $ih) = $ua->request($req)->content=~/URL=.*?path=(.*?).amp;src=(.*?).amp;vol=(.*?).amp;w=(.*?).amp;h=(.*?).amp/s;

		my @hashed;
		my ($fid) = $content=~/id="globalContainer"><div id="(.{8})/s;
		foreach my $field ('UIAdTargeting_CountriesTokenizer', 'location_type', 'UIAdTargetingSex_Radio', 'UIAdTargetingInterestedIn_Radio', 'duration', 'bid_type') {
			my ($res) = /"(${field}_.*?)"/s;
			($res) = /.+name="($fid.*?_${field})"/s unless $res;
			push (@hashed, $res);
		}

		my $relationship;
		my $sexparam = ($sex=~/male/) ? "&sex%5B0%5D=$sex" : '';
		my $interestedparam = ($interested=~/men/) ? "&interested_in%5B0%5D=$interested" : '';
		$relationship = "&relationship%5B$rship%5D=$rship" unless ($rship eq 'all');

		my @clist = split(';', $countries);
		my ($cnt, $cparam) = 0;
		foreach (@clist) {
			$cparam.="&country_names%5B$cnt%5D=$_&countries%5B$cnt%5D=$codes{$_}&$hashed[0]%5B$cnt%5D=$codes{$_}";
			$cnt++;
		}

		my $campaign_params;
		my ($campaign_block) = $content=~/name="campaign_id">(.*?)<\/select>/s;
		my (%campaigns) = $campaign_block=~/value="(.*?)" >(.*?)<\//sg;
		%campaigns = reverse %campaigns;
		if ($old_campaign) {
			$campaign_params = "new_or_existing=existing_info&campaign_id=$campaigns{$campaign}";
		} else {
			$campaign_params = "new_or_existing=new_info&campaign_name=$campaign"
		}
		my ($timezone) = /"timezone_id"><option selected="selected" value="(\d+)"/s;
		my $result = http_post($create, "charset_test=$ct&fb_dtsg=$dtsg&create_submit=1&act=$act&UIAdCreativeDestination_WebsiteURL=$url&UIAdSocialSources_Selector=7865375835&UIAdCreativeDestination_LinkType=external_website&UIAdCreativeDestination_FrameType=0&UIAdCreativeTitle_TitleText=$title&UIAdCreativeTitle_FixedTitleText=&UIAdCreativeBody_BodyText=$body&UIAdCreativeImage_ImageUrl=$isrc&UIAdCreativeImage_ImagePath=$ipath&UIAdCreativeImage_ImageVolume=$ivol&UIAdCreativeImage_ImageWidth=$iw&UIAdCreativeImage_ImageHeight=$ih&UIAdCreativeImage_IsPermanent=0&$hashed[0]%5B%5D=UA&$hashed[1]=everywhere&UIAdTargetingLocation_RadiusInput=1&UIAdTargetingLocation_RadiusSelect=50&UIAdTargetingDemographics_AgeSelectMin=18&UIAdTargetingDemographics_AgeSelectMax=0&$hashed[2]=$sex&$hashed[3]=$interested&UIAdTargetingRelationship_Checkbox%5B%5D=$rship&AdTargetingEducationRadio=all&UIAdTargetingEduWork_CollegeYearsMin=0&UIAdTargetingEduWork_CollegeYearsMax=0&inventory_estimate_on=1&currency_code=USD&country_code=UA&timezone_id=$timezone&daily_budget=$daily_budget&$hashed[4]=continuous&start_time_month=6&start_time_day=7&start_time_year=2010&start_time_hour=4&start_time_ampm=am&end_time_month=7&end_time_day=7&end_time_year=2010&end_time_hour=4&end_time_ampm=am&simple_pricing_bid_value=0.31&$hashed[5]=$bid_type&cpc_bid_value=$bid_value&simple_pricing_mode=false&cpm_bid_value=$bid_value&simple_pricing_version=B&original_title=&original_body=&original_photo=&one_stop_ad_actions=NS_NO_SUGGEST_AD&show_one_stop_creative=&age_min=$age_min&age_max=$age_max&location_type=everywhere&education=all&duration=continuous&bid_type=$bid_type&ui_logging%5Bad_targeting%5D%5Bkw_suggester%5D=%5B%5B%5D%2C%5B%5D%2C%5B%22alloc%22%2C%22each%22%2C%22pull%22%2C%22clone%22%2C%22contains%22%2C%22remove%22%5D%5D&submit_type=oneclick$sexparam$interestedparam$relationship$cparam&$campaign_params");
		$result=~s/<noscript>.+?<\/noscript>//s;
		open (FL, ">2.html"); print FL $result; close FL;
	}
}



sub login {
	my $content = http_post('https://login.facebook.com/login.php?login_attempt=1', "charset_test=$ct&locale=en_US&persistent=1&email=$l&pass=$p&lsd=$lsd");
	die 'auth failed' unless $content=~/<title>Redirecting...</s;
}

sub http_post {
	my ($url, $res) = @_;

	my $req = HTTP::Request->new(POST=> $url);
	$req->content_type('application/x-www-form-urlencoded');
	$req->content($res);
	return $ua->request($req)->content;
}

another article on the stoning: http://www.cnn.com/2010/WORLD/meast/07/01/iran.stoning/
 
  • Like
Reactions: moltar


Awesome thanks :)

gonna make something like that in php also for my own tracking system. Currently working on something like that for POF
 
omfg you are god....!! i wanted to decypher that shit for a while, but a few times i quickly looked over, looked too complicated!! +rep !! wow