Saturday, July 6, 2013

How to send messages to billions of users on facebook with one simple script

 Although title looks good,but due to facebook's spam preventing algorithm this message will be sent to other folder[rarely people will see them],but if the user is a friend of your's then you can send message directly



#usr/bin/env/python
"""
This script can get the user data from facebook graph api.
This is written for better understanding of python 
Modules required:BeautifulSoup
Author:Ajay Kumar Medepalli
Blog:http://pythonnotesbyajay.blogspot.in/
"""
import smtplib
import email
from email.MIMEMultipart import MIMEMultipart
from email.parser import Parser
from email.MIMEText import MIMEText
import urllib2
#from BeautifulSoup import BeautifulSoup
"""There are two versions of BeautifulSoup.This script is updated with the latest one"""
from bs4 import Beautifulsoup
import time
import random


"""
Algorithm Explaination:
Aim: To send messages to fb users 
To send messages we need to have their usernames. (FB has introduced this messaging system like if i've my username i can receive message from 
    email clients and fb email id would be username@facebook.com)

FB Graphi API provides us to acess usernames if users have created one.
get_fb_username ==> This function will get you all usernames 
send_mail ==> This functions will send messages to users (Note :  The message will be sent to other folder if you are not a friend )

"""
 
user_name_array=[]  #List or array to store usernames of facebook
def get_fb_username(id):
    try:
        """
        In fb graph api every url doesn't point to a user or json object from which username can be extracted
        There are some urls which we don't need,since we are using range function there will be errors.To avoid errors we are using
        try 
        """
        url=urllib2.urlopen('https://graph.facebook.com/'+str(id)).read()
        """
        id ==> it's the number.So i'm converting it into string.
        Our urls look like
        https://graph.facebook.com/1
        open your browser or install RESTFUL api addon and check the url
        https://graph.facebook.com/4 
        The above url leads you to the Mark Zuckerberg's page which has his details in json 

        {
   "id": "101",
   "first_name": "xx",
   "gender": "male",
   "last_name": "yyy",
   "link": "https://www.facebook.com/zz",
   "locale": "en_US",
   "name": "Xxx YYy",
   "username": "cam"
}
    
        """

        soup = BeautifulSoup(url)
        all_attr=soup.prettify()
        print all_attr
        gend=all_attr.find("gender")
        if(all_attr[gend+9] == 'm'): # just a check to see if user is a male 
            gender='male'
        elif (all_attr[gend+9] == 'f'): #checking if the user is a female
            gender = 'female'
        else:
            gender="The user didn't specify any gender"
        if all_attr.find('username') != -1: #if there is a username then proceed
            start_quote=all_attr.find('username')+10 #find the first occurence of username
            end_quote=all_attr.find('"',start_quote+1) 
#find the '"' after the username
 
            user_name=all_attr[start_quote:end_quote+1].strip('"')+'@facebook.com' 
#generated username and adding @facebook.com to the username
 
            user_name_array.append(str(user_name)) # adding username to list or array
            print "username ==>"+'\t'+user_name +'\t'+ "gender ==>"+"\t"+gender
            print "\n"
         
    except urllib2.HTTPError:
        pass
 
 
     
 """
The range function in python is so useful . We can generate 1000s of username .I've called the function inside range function.
 """
for i in range(124896015,124896016,1):
#for i in range(startvalue,stopvalue,stepvalue):
    get_fb_username(i+1)
print user_name_array
 
def send_mail():
    random_text=["hi","hello","Nice to meet you","How are you","wassup","hi!!!",'just wanted to say hi']
    server = smtplib.SMTP()
    server.connect('smtp.gmail.com', 587) # for eg. host = 'smtp.gmail.com', port = 587
    server.ehlo()
    server.starttls()
    server.login('username@gmail.com', 'password')
    #replace this with u r gmail id
    #password ==> ur gmail password
    fromaddr ='username@gmail.com'
 
    for i in range(len(user_name_array)-1):
 
        msg = email.MIMEMultipart.MIMEMultipart()
        msg['From'] = fromaddr
        msg['To'] = user_name_array[i]
        msg['Subject'] = 'hi'
         
        msg.attach(MIMEText(random_text[random.randint(0,len(random_text)-1)]))
        #msg.attach(MIMEText('put some custom message.', 'plain')) 
        server.sendmail(fromaddr,user_name_array[i],msg.as_string())
    server.quit()  
send_mail()

But you need to make few changes
1.change the numbers in the range function,if you've a fast computer then you can start all the way from 4 to 5000000 (Mark zuckerberg has id =4)
2.change the user@gmail.com,password with your details
3.Edit the message you want to send aswell
How to use this??
pip install BeautifulSoup ==>Download this module
a.copy and paste this script somewhere on u rcomputer
b.cd somewhere
c.python scriptname.py



Learn python for fun.The popular blog with questions and answers to the python.Solutions to facebookhackercup,codejam,codechef.The fun way to learn python with me.Building some cool apps.

5 comments:

  1. this thing no longer works as facebook updated their API

    ReplyDelete
    Replies
    1. https://developers.facebook.com/tools/explorer?method=GET&path=4%3Ffields%3Dabout&version=v2.5
      You can go to this link and create an access token and attach it to the end of the URL as a query string and this works.

      Delete
    2. How ? could you show me a bit more clearly ?

      Delete
  2. Facebook remove user_name fields in graph api, so this is no longer helpful

    ReplyDelete