perl script to post ads to facebook

andyt

New member
Dec 20, 2006
677
9
0
I updated my facebook ad submitter perl program. The previous thread was 119 days old, so I couldn't reply to it.

Also check this out- Iran Plans to Execute Woman in Stoning Case , and call your senator/donate to Amnesty International if you use this program and don't believe in capital punishment.



Code:
#!/usr/bin/perl
use strict;
use LWP;
use HTTP::Cookies;
use HTTP::Request::Common;
use URI::Escape;
use Data::Dumper;
my ($l, $p) = ('youruser@email.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 $intname = 'intnametest';

my ($url, $title, $body, $campaign) = (uri_escape('http://test.com'), 'testad', 'testbody', 'test');
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 = 'zzz.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 ($form_id) = /name="post_form_id" value="(.*?)"/s;
        my ($dtsg) = /name="fb_dtsg" value="(.*?)"/s;
        my ($act) = /id="act" value="(.*?)"/s;
        my ($imgform_url) = /Image_Frame" src="(.*?)"/;
        my $imgcont = $ua->get('http://www.facebook.com'.$imgform_url)->content;
        open (FL, ">1.5.html"); print FL $imgcont; close FL;
        my ($img_id) = $imgcont=~/.+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, 'UIAdCreativeImage_RedirectDomain' => 'http://www.facebook.com/'
        ];
        $imgcont = $ua->request($req)->content;
        my ($ipath, $isrc, $ivol, $iw, $ih) = $imgcont=~/URL=.*?path=(.*?).amp;src=(.*?).amp;vol=(.*?).amp;w=(.*?).amp;h=(.*?).amp/s;
        open (FL, ">1.6.html"); print FL $imgcont; close FL;
        my @hashed;
        my ($fid) = /id="globalContainer"><div id="(.{8})/s;
        foreach my $field ('UIAdTargeting_CountriesTokenizer', 'location_type', 'UIAdTargetingSex_Radio', 'UIAdTargetingInterestedIn_Radio',  'bid_type') {
            print "$field\n";
            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 $args = "post_form_id=$form_id&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=reviewad$sexparam$interestedparam$relationship$cparam&$campaign_params&broad_targeting_age=0";
        print "$args\n";
        my $result = http_post($create, $args);
        $result=~s/<noscript>.+?<\/noscript>//s;
        open (FL, ">2.html"); print FL $result; close FL;
        #exit;
        
        my ($oid) = $result=~/"oid" value="(.*?)"/s;
        my ($r_title) = $result=~/"title" value="(.*?)"/s;
        my ($r_body) = $result=~/"body" value="(.*?)"/s;
        my ($r_photo) = $result=~/"photo" value="(.*?)"/s;
        print "fb_dtsg=$dtsg&suggested_name=$intname&post_form_id=$form_id&oid=$oid&create_submit=create_submit&original_title=&original_body=&original_photo=&title=$r_title&body=$r_body&photo=$r_photo&one_stop_ad_actions=OS_NO_SUGGEST_AD%3AREVIEW_AD&act=$act&coupon_code=&create_submit=Place+order\n";

        my $result = http_post('https://secure.facebook.com/ads/create/review_ad.php', "charset_test=$ct&fb_dtsg=$dtsg&suggested_name=$intname&post_form_id=$form_id&oid=$oid&create_submit=create_submit&original_title=&original_body=&original_photo=&title=$r_title&body=$r_body&photo=$r_photo&one_stop_ad_actions=OS_NO_SUGGEST_AD%3AREVIEW_AD&act=$act&coupon_code=&create_submit=Place+order");

        $result=~s/<noscript>.+?<\/noscript>//s;
        open (FL, ">3.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;
}