summaryrefslogtreecommitdiff
path: root/taste.py
blob: 847147346229415ed99c66d48ac77d941c9650f5 (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
#!/usr/bin/python3
# -*- coding: utf-8 -*-


import tweepy
from config import white_kwds


COLOR_RESET = "\033[0m"

COLOR_REJECTED = "\033[1;31m"
COLOR_ACCEPTED = "\033[1;32m"
COLOR_ALREADY  = "\033[1;33m"


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 analyse(sid, username, content, api, memory):
    """Analyse a Tweet content."""

    like = False

    words = content.split(' ')

    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

    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))

                print(COLOR_ACCEPTED + 'Liking' + COLOR_RESET + ' @%s: "%s"' % (username, displayable))
                print(' -> https://twitter.com/%s/status/%d' % (username, sid))

            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))