summaryrefslogtreecommitdiff
path: root/taste.py
blob: 069e2736be9c40661a2459065a5eb3d6fffd71da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/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