summaryrefslogtreecommitdiff
path: root/taste.py
diff options
context:
space:
mode:
Diffstat (limited to 'taste.py')
-rw-r--r--taste.py166
1 files changed, 144 insertions, 22 deletions
diff --git a/taste.py b/taste.py
index 8471473..95139b4 100644
--- a/taste.py
+++ b/taste.py
@@ -2,8 +2,13 @@
# -*- coding: utf-8 -*-
+import re
+import sys
import tweepy
-from config import white_kwds
+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 random import randint
+from time import sleep
COLOR_RESET = "\033[0m"
@@ -13,6 +18,10 @@ 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."""
@@ -25,16 +34,60 @@ def get_displayable_content(orig, margin):
return result
-def analyse(sid, username, content, api, memory):
+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 analyse(sid, username, displayed, lang, content, api, memory):
"""Analyse a Tweet content."""
- like = False
+ global _like_count
- words = content.split(' ')
+ liked = False
- white = [ s.lower().replace('_', ' ') for s in white_kwds.split(' ') ]
+ if not is_blacklisted(username, displayed) and (lang in accepted_languages.split(' ')):
- for kwd in white:
+ 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:
@@ -44,33 +97,102 @@ def analyse(sid, username, content, api, memory):
if like:
break
- if like:
+ # White list, case sensitive
- if memory.is_original_content(content):
+ if not like:
- try:
+ white = [ s.replace('_', ' ') for s in cs_white_kwds.split(' ') ]
- api.create_favorite(sid)
+ for kwd in white:
- memory.save_liked_status(sid, username, content)
+ for w in words:
+ if w == kwd:
+ like = True
+ break
- displayable = get_displayable_content(content, len('Liking') + len(' @%s: "' % username))
+ if like:
+ break
- print(COLOR_ACCEPTED + 'Liking' + COLOR_RESET + ' @%s: "%s"' % (username, displayable))
- print(' -> https://twitter.com/%s/status/%d' % (username, sid))
+ # Black list
- except tweepy.error.TweepError:
+ if like:
- pass
+ black = [ s.lower().replace('_', ' ') for s in black_kwds.split(' ') ]
- else:
+ for kwd in black:
+
+ for w in words:
+ if w.lower() == kwd:
+ like = False
+ break
- displayable = get_displayable_content(content, len('Already seen "'))
+ 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)
- print(COLOR_ALREADY + 'Already seen' + COLOR_RESET + ' "%s"' % displayable)
+ 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:
- else:
+ displayable = get_displayable_content(content, len('Reject') + len(' @%s: "' % username))
- displayable = get_displayable_content(content, len('Reject') + len(' @%s: "' % username))
+ print(COLOR_REJECTED + 'Reject' + COLOR_RESET + ' @%s: "%s"' % (username, displayable))
- print(COLOR_REJECTED + 'Reject' + COLOR_RESET + ' @%s: "%s"' % (username, displayable))
+ return liked