Script to add new contacts automatically( inLinkedin )


This script will do the following things : 

1) will get all user url from each page in the  linkedinGroup
2) using that user url with will get the form 
3) once the form is got , it will fill the form and send sumbit 
4) have add multiple user agents  in the request headers  




==========================================================
#!/usr/bin/perl


use strict;
use warnings;
use WWW::Mechanize;
use LWP;
use JSON;
use Data::Dumper;
use Getopt::Long;
use POSIX qw/ strftime /;
use LWP::Simple;
use HTML::TreeBuilder;
use HTML::TreeBuilder::XPath;
use List::Util qw/shuffle/;
my $username = 'login id ';
my $password = ' password ';
my $loginurl=  $maiurl.'/uas/login?goback=&trk=hb_signin';

$|++;

my $mech = login_linkedin();
my $tree = HTML::TreeBuilder::XPath->new_from_content();




my @all_content = get_data($mech,$grp,"1","10");

my @all_new_linkein_users = get_linkedin_user_url($tree,@all_content) ;

send_invitation($mech,@all_new_linkein_users);




sub set_dynamic_user_agent {

my ($mech) = @_;
my @user_agents = (
   'Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0',
   'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10; rv:33.0) Gecko/20100101 Firefox/33.0',
   'Mozilla/5.0 (X11; Linux i586; rv:31.0) Gecko/20100101 Firefox/31.0',
    'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20130401 Firefox/31.0',
    'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0',
    'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:25.0) Gecko/20100101 Firefox/25.0',
    'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0',
    'Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0'
);
my @shuff_user_agents = shuffle( @user_agents );
    # dynamically set custom HTTP User-agents
    $mech->add_header( 'User-agent' => $shuff_user_agents[0]);
    print  sprintf("set User-Agent : %s \n", $shuff_user_agents[0] ) ;

return $mech;
}

sub send_invitation {

my ( $mech,@all_user_url) = @_;

  for my $user_url (@all_user_url){
    sleep(2);
  $mech= set_dynamic_user_agent($mech);
 $mech -> get("${maiurl}$user_url");
# print sprintf( "User-Agent : %s\n\n",  $mech->uri() );
#<input type="radio" name="reason" value="IF" id="IF-reason-iweReconnect" class="radio-btn">
#<input type="submit" name="iweReconnectSubmit" value="Send Invitation" id="send-invite-button" class="btn-primary"/
 #$mech ->click_button( name => "iweReconnectSubmit" );

 if ( $mech->form_name("iweReconnect") ) {
    print "sending invitation : $user_url \n";
 $mech ->set_fields( 'reason' => 'IF' );
 $mech->submit();
 $mech->success or die "form  POST fail";

  } elsif ( $mech->form_name("invitation")) {
  print " invitation was already sent to $user_url sent \n" ;
  next ;

  }else {
  print "No From matched $user_url\n ";
    next;
  }

 }

}





sub get_linkedin_user_url {
    my ($tree,@all_content) = @_;
    my $all_new_linkein_users;
    for my $content  (@all_content) {
        $tree->parse($content);

        my @newconn = $tree->findnodes('//li[@class="member"');
        print "getting url  that which is not in my connection \n";
        for my $custm (@newconn){
            my @all_a_tag = $custm->look_down('_tag' => 'a'  ) ;
             my $count =  @all_a_tag;

            my $invte_link  ;

             if (   $all_a_tag[$count - 1]->attr('href') =~ m/people/ig   ) {
                $invte_link = $all_a_tag[$count - 1]->attr('href')  ;
             } else {
             next;
             }
             print $invte_link ." ---- \n";
        push ( @all_new_linkein_users, "$invte_link")  ;

  exit unless(@all_new_linkein_users);

        }
    }
return (@all_new_linkein_users);
}


sub get_data {
    my ( $mech,$url,$spage,$epage ) = @_;

    my @all_content;
    print "Getting the content form page $spage to page $epage\n";
    for my $page ($spage .. $epage ) {
        print " ----------- ${url}$page -----------------"."\n";
        sleep(1) ;
        $mech -> get ("${url}$page");
        push ( @all_content,    $mech->content()) ;
    }
    return (@all_content);
}


sub login_linkedin {
    my $mech = WWW::Mechanize->new();
    $mech -> cookie_jar(HTTP::Cookies->new());
    $mech -> get("$loginurl");
    $mech -> form_id('login');
    $mech -> field ('session_key' => $username);
    $mech -> field ('session_password' => $password);
    $mech -> click_button (value => 'Sign In');
    return $mech ;
}

============================================================

python script to fetch gmail ids and script to send out mail with attachment


-----------------------------------------------------------------------------------------------------------------
script to fetch gmail ids
------------------------------------------------------------------------------------------------------------------
import imaplib, sys, email

mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('gmail@gmail.com', 'password')
mail.list()
# Out: list of "folders" aka labels in gmail.
mail.select("inbox") # connect to inbox.


result, data = mail.search(None, '(HEADER From "info@monsterindia.com")')

for num in reversed(data[0].split()):
    rv, data = mail.fetch(num, '(RFC822)')
    raw_email = data[0][1] # here's the body, which is raw text of the whole email
# including headers and alternate payloads

    email_message = email.message_from_string(raw_email)
    if email_message['Reply-To'] in ('naukrialerts@naukri.com', None):
        continue
    print email_message['Reply-To']

-----------------------------------------------------------------------------------------------------------


---------------------------------------------------------------------------------------------------------------
script to send out mail with attachment 
----------------------------------------------------------------------------------------------------------------
#!/usr/bin/python

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os,re

gmail_user = "some@gmail.com"
gmail_pwd = "passowrd"

def mail(to, subject, attach, recruitername ):
   msg = MIMEMultipart()

   msg['From'] = gmail_user
   msg['To'] = to
   msg['Subject'] = subject
   text = """
Hi  %s

balah 
balah 
balah 

balah 

Regards 

balah 
balah 

""" %  recruitername

   msg.attach(MIMEText(text))

   part = MIMEBase('application', 'octet-stream')
   part.set_payload(open(attach, 'rb').read())
   Encoders.encode_base64(part)
   part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
   msg.attach(part)

   mailServer = smtplib.SMTP("smtp.gmail.com", 587)
   mailServer.ehlo()
   mailServer.starttls()
   mailServer.ehlo()
   mailServer.login(gmail_user, gmail_pwd)
   mailServer.sendmail(gmail_user, to, msg.as_string())
   # Should be mailServer.quit(), but that crashes...
   mailServer.close()


myemailfile = "/scripts/cyber_send"
emaillist =  open( myemailfile, 'r')


for i in emaillist.readlines():
    match = re.search(r'[\w\.-]+@[\w\.-]+', i)
    if not  match:
        continue
    print "%s " % match.group(0)
    hrname =  str(re.split("\.|_|-", match.group(0).split('@')[0] )[0])
    #hrname = ''
    mail( match.group(0) , "Sr Engineer ( information technology )",
   "/scripts/resume_.pdf" , hrname )
-------------------------------------------------------------------------------------------------------------------

Other Articles

Enter your email address: