#!/usr/bin/python3 # -*- coding: utf-8 -*- import re import sys import tweepy from config import accepted_languages, white_kwds, cs_white_kwds, black_kwds, cs_black_kwds from config import banned_accounts, banned_accounts_re, banned_titles_re from config import sensitive_ratio from random import randint from time import sleep COLOR_RESET = "\033[0m" COLOR_REJECTED = "\033[1;31m" COLOR_ACCEPTED = "\033[1;32m" COLOR_ALREADY = "\033[1;33m" # Counter for this session _like_count = 0 def get_displayable_content(orig, margin): """Format content to get it displayable.""" padding = '\n' + ' ' * margin useful = [ l for l in orig.split('\n') if len(l) > 0 ] result = padding.join(useful) return result def is_blacklisted(username, displayed): """Define if a given account is blacklisted or not.""" result = username in banned_accounts.split(' ') if not result: for exp in banned_accounts_re.split(' '): preg = re.compile(exp) match = preg.match(username) if match: result = True if result: break if not result: for exp in banned_titles_re.split(' '): preg = re.compile(exp) match = preg.match(displayed) if match: result = True if result: break return result def is_spam(content): """Define if a given content is suitable or not.""" keywords = content.split(' ') uc_counter = 0 for kw in keywords: if kw == kw.upper(): uc_counter += 1 ratio = (uc_counter * 100.0) / len(keywords) result = (ratio > sensitive_ratio) return result def analyse(sid, username, displayed, lang, content, api, memory): """Analyse a Tweet content.""" global _like_count liked = False if not is_blacklisted(username, displayed) \ and not is_spam(content) \ and (lang in accepted_languages.split(' ')): like = False words = content.split(' ') # White list white = [ s.lower().replace('_', ' ') for s in white_kwds.split(' ') ] for kwd in white: for w in words: if w.lower() == kwd: like = True break if like: break # White list, case sensitive if not like: white = [ s.replace('_', ' ') for s in cs_white_kwds.split(' ') ] for kwd in white: for w in words: if w == kwd: like = True break if like: break # Black list if like: black = [ s.lower().replace('_', ' ') for s in black_kwds.split(' ') ] for kwd in black: for w in words: if w.lower() == kwd: like = False break if not like: break # Black list, case sensitive if like: black = [ s.replace('_', ' ') for s in cs_black_kwds.split(' ') ] for kwd in black: for w in words: if w == kwd: like = False break if not like: break # Final step if like: if memory.is_original_content(content): try: api.create_favorite(sid) memory.save_liked_status(sid, username, content) displayable = get_displayable_content(content, len('Liking') + len(' @%s: "' % username)) _like_count += 1 print(COLOR_ACCEPTED + 'Liking' + COLOR_RESET + ' @%s: "%s"' % (username, displayable)) print(' %u -> https://twitter.com/%s/status/%d' % (_like_count, username, sid)) liked = True # # Do not be so aggressive! # if _like_count % 100 == 0: # time = randint(10, 54) # print('[*] Reached %u likes; sleeping %u minutes...' % (_like_count, time)) # sleep(60 * time) # # Do not be so aggressive and respect the limits! # if _like_count > 900: # print('[*] Enough for today! Reached %u likes...' % _like_count) # sys.exit() except tweepy.error.TweepError: pass else: displayable = get_displayable_content(content, len('Already seen "')) print(COLOR_ALREADY + 'Already seen' + COLOR_RESET + ' "%s"' % displayable) else: displayable = get_displayable_content(content, len('Reject') + len(' @%s: "' % username)) print(COLOR_REJECTED + 'Reject' + COLOR_RESET + ' @%s: "%s"' % (username, displayable)) return liked