Krahwazi

FB Spam Remover

Feb 25th, 2024
200
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | Source Code | 0 0
  1. import facebook
  2. import time
  3. import logging
  4. import nltk
  5. from nltk.tokenize import word_tokenize
  6. from nltk.corpus import stopwords
  7. from nltk.stem import WordNetLemmatizer
  8. import string
  9. from sklearn.feature_extraction.text import TfidfVectorizer
  10. from sklearn.linear_model import LogisticRegression
  11. from sklearn.pipeline import Pipeline
  12.  
  13. # Set up logging
  14. logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
  15.  
  16. # Set up the Facebook Graph API
  17. graph = facebook.GraphAPI(access_token='YOUR_ACCESS_TOKEN', version='YOUR_API_VERSION')
  18.  
  19. # Example post ID
  20. post_id = 'POST_ID'
  21.  
  22. # Download NLTK resources
  23. nltk.download('punkt')
  24. nltk.download('stopwords')
  25. nltk.download('wordnet')
  26.  
  27. # Initialize NLTK components
  28. lemmatizer = WordNetLemmatizer()
  29. stop_words = set(stopwords.words('english'))
  30.  
  31. # Initialize the TF-IDF vectorizer
  32. vectorizer = TfidfVectorizer(tokenizer=word_tokenize, stop_words=stop_words)
  33.  
  34. # Initialize the logistic regression model
  35. model = LogisticRegression()
  36.  
  37. # Create a pipeline
  38. pipeline = Pipeline([
  39.     ('vectorizer', vectorizer),
  40.     ('model', model)
  41. ])
  42.  
  43. def fetch_comments(post_id):
  44.     try:
  45.         # Fetch comments
  46.         comments = graph.get_object(id=post_id, fields='comments')['comments']['data']
  47.         return comments
  48.     except facebook.GraphAPIError as e:
  49.         logging.error(f"Error fetching comments: {e}")
  50.         return []
  51.  
  52. def preprocess_text(text):
  53.     # Tokenize text
  54.     tokens = word_tokenize(text.lower())
  55.     # Remove punctuation and stop words, and lemmatize tokens
  56.     cleaned_tokens = [lemmatizer.lemmatize(token) for token in tokens if token not in string.punctuation and token not in stop_words]
  57.     return ' '.join(cleaned_tokens)
  58.  
  59. def is_spam(comment):
  60.     preprocessed_text = preprocess_text(comment['message'])
  61.     # Use the trained model to predict if the comment is spam
  62.     prediction = pipeline.predict([preprocessed_text])[0]
  63.     return prediction == 1
  64.  
  65. def remove_spam(comments):
  66.     for comment in comments:
  67.         if is_spam(comment):
  68.             try:
  69.                 graph.delete_object(id=comment['id'])
  70.                 logging.info(f"Removed spam comment: {comment['message']}")
  71.             except facebook.GraphAPIError as e:
  72.                 logging.error(f"Error removing comment: {e}")
  73.  
  74. while True:
  75.     comments = fetch_comments(post_id)
  76.     remove_spam(comments)
  77.     time.sleep(60)  # Check every minute (adjust as needed)
Advertisement
Comments
  • Krahwazi
    2 years
    # text 0.33 KB | 0 0
    1. In this code, we use a TfidfVectorizer to convert the comment text into a numerical representation suitable for the model, and then use a logistic regression model to classify the comments as spam or not. You can train the model on a spam dataset using supervised learning techniques to improve its accuracy in identifying spam comments.
Add Comment
Please, Sign In to add comment