Saturday 18 February 2017

Facebook+Python=Awesome

Do you like python? Do you use facebook?
If the answer is yes why not use facebook via python.
Below is the code to use facebook via python .This program has been inspired by http://www.geeksforgeeks.org/send-message-to-fb-friend-using-python
 Disclaimer of Warranties and Limitation of Liability.
a.Unless otherwise separately undertaken by the author, to the extent possible, the author offers the Licensed Material as-is and as-available, and makes no representations or warranties of any kind concerning the Licensed Material, whether express, implied, statutory, or other. This includes, without limitation, warranties of title, merchantability, fitness for a particular purpose, non-infringement, absence of latent or other defects, accuracy, or the presence or absence of errors, whether or not known or discoverable. Where disclaimers of warranties are not allowed in full or in part, this disclaimer may not apply to You.
b.The disclaimer of warranties and limitation of liability provided above shall be interpreted in a manner that, to the extent possible, most closely approximates an absolute disclaimer and waiver of all liability.
Check out fbchat's policies before continuing.
# This code to be considered under GPLv3.Use it under your responsibility.
Code:
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#Please use it carefully.Ensure loging out by using a browser
#Required dependencies:
#   requests
#   lxml
#   beautifulsoup4
#   fbchat
def main():
    '''main function: initiation of fbchat and login'''
    import fbchat
    from getpass import getpass
    username = str(raw_input("Username: "))
    client = fbchat.Client(username, getpass())
    '''
    #For sendig same message to specific number of clients
    no_of_friends = int(raw_input("Number of friends: "))
    for i in xrange(no_of_friends):
        name = str(raw_input("Name: "))
        friends = client.getUsers(name)  # return a list of names
        friend = friends[0]
        msg = str(raw_input("Message: "))
        ask = 'O'
        while ask.upper()!='Y' and ask.upper()!='N':
            ask = raw_input('Are you sure about sending this message to'+friend+'(Y/N):')
            if ask.upper()=='Y':
                sent = client.send(friend.uid, msg)
                if sent:
                    print("Message sent successfully!")
            elif ask.upper()=='N':
                print("Message was not sent .")'''
    def choicefrnd(friends):
        '''choose freind from list'''
        try:
             chno=input('Enter index of friend: ')
             friend = friends[chno-1]
             return friend
         except:
             print 'Please give index number as printed above from 1 to n'
             choicefrnd()
    def talk():
        '''talk with friends. Contains main loop and sub loops'''
        ch='y'
        while ch=='y':   
            name = str(raw_input("Name: "))
            friends = client.getUsers(name)  # return a list of names
            for i in xrange(len(friends)):
                print str(i+1)+str(friend[i])
            if len(friends)>=1:
                friend=choicefrnd(friends)
                msg = str(raw_input("Message: "))
                ask = 'O'
                while ask.upper()!='Q':
                   
                    ask = raw_input('Are you sure about sending this message to'+friend+'(Y/N):')
                    if ask.upper()=='Y':
                        sent = client.send(friend.uid, msg)
                        if sent:
                            print("Message sent successfully!")
                    elif ask.upper()=='N':
                        print("Message was not sent .")
                    elif ask.upper()=='Q':
                        break
                    else:
                        print 'Could not decipher your answer.'
                    print 'Press q to quit chatting this friend'
            else:
                pass           
        else:
            pass
        talk()
        client.logout()
main()

No comments:

AI in 2040