Advertisement
Guest User

facebook

a guest
Oct 21st, 2018
437
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.03 KB | None | 0 0
  1. from fbchat import Client
  2. from fbchat.models import *
  3. from getpass import *
  4. import datetime
  5. from textwrap import wrap
  6.  
  7.  
  8. def printw(list):
  9.     for text in list:
  10.         print(text)
  11.  
  12.  
  13. def printd(dict):
  14.     for key in dict:
  15.         print(str(key) + " - " + dict[key])
  16.  
  17.  
  18. def read_thread(client, thread, num_of_messages):
  19.     messages = client.fetchThreadMessages(thread_id=thread.uid, limit=num_of_messages)
  20.     print("\nShowing messages from thread: " + thread.name)
  21.     print("----------------------------------------------------------------------")
  22.     for message in reversed(messages):
  23.         author_dic = client.fetchUserInfo(message.author)
  24.         author_name = author_dic[next(iter(author_dic))].name
  25.         print("Author: " + author_name)
  26.         print(datetime.datetime.fromtimestamp(round(int(message.timestamp) / 1000)))
  27.         if message.attachments:
  28.             print("Message has %d attachments!" % len(message.attachments))
  29.         if message.text:
  30.             printw(wrap(message.text))
  31.         print("----------------------------------------------------------------------")
  32.     print("\n")
  33.     return True
  34.  
  35.  
  36. def read_unread(client):
  37.     # TO BE DONE
  38.     # Choose, which messages to show, according to the sender or the time
  39.     # If message is too long, modify it to the size of the window or predefined length
  40.     # Enable possibility to respond
  41.     # Modify formatting the text to display message
  42.     # Catch case, that there are more than 20 unread messages
  43.  
  44.     unread_threads = client.fetchUnread()
  45.     for thread in reversed(unread_threads):
  46.         thread_info = client.fetchThreadInfo(thread)
  47.         print("Showing messages from thread: " + thread_info[next(iter(thread_info))].name)
  48.         print("----------------------------------------------------------------------")
  49.  
  50.         messages = client.fetchThreadMessages(thread_id=thread)
  51.         for message in reversed(messages):
  52.             if not message.is_read:
  53.                 author_dic = client.fetchUserInfo(message.author)
  54.                 author_name = author_dic[next(iter(author_dic))].name
  55.                 print("Author: " + author_name)
  56.                 print(datetime.datetime.fromtimestamp(round(int(message.timestamp) / 1000)))
  57.                 if message.attachments:
  58.                     print("Message has %d attachments!" % len(message.attachments))
  59.                 printw(wrap(message.text))
  60.                 print("----------------------------------------------------------------------")
  61.         print("\n")
  62.  
  63.     return True
  64.  
  65.  
  66. def read_latest(client):
  67.     ret_value = True
  68.     answer_valid = True
  69.     answer = input("How many threads do you want to see? ")
  70.     if answer.isnumeric():
  71.         threads = client.fetchThreadList(limit=int(answer))
  72.         threads_name_dict = {}
  73.         threads_dict = {}
  74.         idx = 1
  75.  
  76.         for thread in threads:
  77.             threads_dict[thread.name] = thread
  78.             threads_name_dict[idx] = thread.name
  79.             idx += 1
  80.  
  81.         print("What thread do you want to open?")
  82.         printd(threads_name_dict)
  83.         answer = input("Your answer: ")
  84.         if answer.isnumeric():
  85.             thread_name = threads_name_dict[int(answer)]
  86.             answer = input("How many messages do you wan to read? ")
  87.             if answer.isnumeric():
  88.                 thread = threads_dict[thread_name]
  89.                 ret_value = read_thread(client, thread, int(answer))
  90.                 answer = input("0 - Continue\n1 - send an answer\n")
  91.                 if answer.isnumeric():
  92.                     if int(answer) == 1:
  93.                         text = input("Type in your message: ")
  94.                         sent = client.send(Message(text=text), thread_id=thread.uid, thread_type=thread.type)
  95.                         if sent:
  96.                             print("Message sent successfully!")
  97.  
  98.     else:
  99.         answer_valid = False
  100.  
  101.     if not answer_valid:
  102.         print("\nYour answer was invalid, choose again: ")
  103.         ret_value = read_latest(client)
  104.     return ret_value
  105.  
  106.  
  107. def read_any(client):
  108.     return True
  109.  
  110.  
  111. def send(client):
  112.     name = input("Whom do you want to send the message? ")
  113.     users = client.searchForUsers(name)
  114.     user = users[0]
  115.     print("Is this the user?")
  116.     print(user)
  117.     answer = input("0 - No, 1 - Yes")
  118.     if answer.isnumeric():
  119.         if int(answer) == 1:
  120.             text = input("Type in your message: ")
  121.         sent = client.send(Message(text=text), thread_id=user.uid, thread_type=ThreadType.USER)
  122.         if sent:
  123.             print("Message sent successfully!")
  124.     return True
  125.  
  126.  
  127. def logout(client):
  128.     if client.logout():
  129.         print("You have been successfully logged out.")
  130.     return False
  131.  
  132.  
  133. def ask(question, dict1, dict2, client):
  134.     answer_valid = True
  135.     ret_value = False
  136.     print(question)
  137.     printd(dict1)
  138.     answer = input("Your answer: ")
  139.     if answer.isnumeric():
  140.         if int(answer) <= len(dict1):
  141.             ret_value = dict2[dict1[int(answer)]](client)
  142.         else:
  143.             answer_valid = False
  144.     else:
  145.         answer_valid = False
  146.  
  147.     if not answer_valid:
  148.         print("\nYour answer was invalid, choose again: ")
  149.         ret_value = ask(question, dict1, dict2, client)
  150.     return ret_value
  151.  
  152.  
  153. Choices_num2str = {
  154.     1: "Read unread messages",
  155.     2: "Read latest messages",
  156.     3: "Read messages from any user or group",
  157.     4: "Send message",
  158.     5: "Logout"
  159. }
  160.  
  161. Choices_str2func = {
  162.     "Read unread messages": read_unread,
  163.     "Read latest messages": read_latest,
  164.     "Read messages from any user or group": read_any,
  165.     "Send message": send,
  166.     "Logout": logout
  167. }
  168.  
  169. ThreadTypes = {
  170.     1: ThreadType.USER,
  171.     2: ThreadType.GROUP
  172. }
  173.  
  174.  
  175. def main():
  176.     username = input("Email address: ")
  177.     password = getpass()
  178.     me = Client(username, password)
  179.     test = True
  180.     try:
  181.         while test:
  182.             test = ask("What do you want to do?", Choices_num2str, Choices_str2func, me)
  183.     except:
  184.         logout(me)
  185.     return 0
  186.  
  187.  
  188. if __name__ == "__main__":
  189.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement