Пример #1
0
def remove_user(user_id):
    conn = app.get_connector()
    cur = conn.cursor()
    cur.execute('delete from users where user_id=%s', (user_id, ))
    conn.commit()
    cur.close()
    conn.close()
Пример #2
0
def change_words(user_id, words):
    conn = app.get_connector()
    cur = conn.cursor()
    cur.execute('update users set keywords=%s where user_id=%s',
                (words, user_id))
    conn.commit()
    cur.close()
    conn.close()
Пример #3
0
def get_words(user_id):
    conn = app.get_connector()
    cur = conn.cursor()
    cur.execute('select keywords from users where user_id=%s', (user_id, ))
    words = cur.fetchone()[0]
    cur.close()
    conn.close()
    return words
Пример #4
0
 def on_data(self, status):
     status = json.loads(status)
     try:
         if 'direct_message' in status.keys():
             text = status['direct_message']['text']
             user_id = status['direct_message']['sender']['screen_name']
             conn = app.get_connector()
             cur = conn.cursor()
             cur.execute('select user_id from users')
             users = [uid[0] for uid in cur.fetchall()]
             cur.close()
             conn.close()
             if user_id not in users or user_id == 'metablockerbot':
                 return True
             if re.match('bl:', text):
                 new_words = parse_to_words(text)
                 add_words(user_id, new_words)
             elif re.match('rm:', text):
                 removed_words = parse_to_words(text)
                 remove_words(user_id, removed_words)
     except tweepy.error.TweepError as e:
         print 'catch: tweepy.error.TweepError'
     return True
Пример #5
0
def main():
    conn = app.get_connector()
    cur = conn.cursor()
    cur.execute('select * from users')
    rows = cur.fetchall()
    users = [row[0] for row in rows if row[0] != 'metablockerbot']
    total_blocks_in_all_users = 0
    api_bot = None
    for row in rows:
        access_token_key, access_token_secret, words = row[1:4]
        total_blocks_per_day = row[5]
        api = app.get_auth_api(access_token_key, access_token_secret)
        if row[0] == 'metablockerbot':
            api_bot = api
            since_id = row[4]
            dms = api.direct_messages(since_id=since_id)
            for dm in dms:
                user_id = dm.sender_screen_name
                text = dm.text
                if user_id in users:
                    if re.match('bl:', text):
                        new_words = parse_to_words(text)
                        add_words(user_id, new_words)
                    elif re.match('rm:', text):
                        removed_words = parse_to_words(text)
                        remove_words(user_id, removed_words)
            if len(dms) != 0:
                since_id = max(dm.id for dm in dms)
                conn2 = app.get_connector()
                cur2 = conn2.cursor()
                cur2.execute(
                    'update users set dm_since_id=%s where user_id=%s',
                    (since_id, 'metablockerbot'))
                conn2.commit()
                cur2.close()
                conn2.close()
        for word in words:
            try:
                for tweet in api.search(q=word, count=100):
                    block_user_id = tweet.user.screen_name
                    friendship = api.lookup_friendships(
                        screen_names=[block_user_id])[0]
                    if not friendship.is_following and not friendship.is_followed_by:
                        api.create_block(screen_name=block_user_id)
                        total_blocks_per_day += 1
            except tweepy.error.TweepError as e:
                if isinstance(e.message, list) and \
                        isinstance(e.message[0], dict) and \
                        'Invalid or expired token.' == e.message[0].get('message', ''):
                    remove_user(row[0])
                break
        conn2 = app.get_connector()
        cur2 = conn2.cursor()
        cur2.execute(
            'update users set total_blocks_per_day=%s where user_id=%s',
            (total_blocks_per_day, row[0]))
        conn2.commit()
        cur2.close()
        conn2.close()
        total_blocks_in_all_users += total_blocks_per_day
    cur.close()
    conn.close()
    time_now = datetime.now()
    if time_now.hour == 0 and time_now.minute // 10 == 0:
        api_bot.update_status(
            '{} blocks, yesterday.'.format(total_blocks_in_all_users))
Пример #6
0
    if words is None:
        return
    words = [word for word in words if word not in removed_words]
    change_words(user_id, words)


def parse_to_words(text):
    words = text[3:].split(',')
    if words is None:
        return
    words = [word.strip() for word in words]
    return words


if __name__ == '__main__':
    conn = app.get_connector()
    cur = conn.cursor()
    cur.execute(
        'select access_token_key, access_token_secret from users where user_id=\'metablockerbot\''
    )
    access_token_key, access_token_secret = cur.fetchone()
    cur.close()
    conn.close()
    bot_api = app.get_auth_api(access_token_key, access_token_secret)

    class Listener(tweepy.StreamListener):
        """docstring for Listener."""
        def __init__(self, *args, **kwargs):
            super(Listener, self).__init__(self, *args, **kwargs)

        def on_data(self, status):