<commit_before>#!/usr/bin/env python
# encoding: utf-8

from __future__ import unicode_literals

import codecs
import socket
import sys
import threading
import os

import tweepy

HASHTAGS = ['#IoT', 'python']


_CONSUMER_KEY = os.environ['CONSUMER_KEY']
_CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
_ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
_ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']


TIMERS = {}


def main():
    auth = tweepy.auth.OAuthHandler(_CONSUMER_KEY, _CONSUMER_SECRET)
    auth.set_access_token(_ACCESS_TOKEN, _ACCESS_TOKEN_SECRET)
    #api = tweepy.API(auth)

    stream = tweepy.Stream(auth, CustomStreamListener(tweet_callback))
    stream.filter(None, HASHTAGS)  # blocking function


def tweet_callback(status):
    #print('%s:  "%s" (%s via %s)\n' % (status.author.screen_name,
    #                                   status.text,
    #                                   status.created_at,
    #                                   status.source))
    for i, hashtag in enumerate(HASHTAGS):
        if hashtag.lower() in status.text.lower():
            print("'{}' in tweet".format(hashtag))
            turn_on_five_seconds(i)
        else:
            print("'{}' NOT in: {}".format(hashtag, status.text))


def turn_on_five_seconds(i):
    existing_timer = TIMERS.get(i)
    if existing_timer:
        existing_timer.cancel()

    turn_on_now(i)
    TIMERS[i] = threading.Timer(5, turn_off_now, [i])
    TIMERS[i].start()


def turn_on_now(i):
    print("ON: #{}".format(i))
    send_udp_data(IP, UDP_PORT, str('N:0000:1{}01:0001:1'.format(i + 2)))


def turn_off_now(i):
    print("OFF: #{}".format(i))
    send_udp_data(IP, UDP_PORT, str('N:0000:1{}01:0001:0'.format(i + 2)))


def send_udp_data(ip, port, data):
    sock = socket.socket(socket.AF_INET,     # Internet
                         socket.SOCK_DGRAM)  # UDP
    sock.sendto(data, (ip, port))


class CustomStreamListener(tweepy.StreamListener):

    def __init__(self, callback=None):
        self.callback = callback
        super(CustomStreamListener, self).__init__()

    # Alternatively, override on_data instead (then on_status won't be called)

    def on_status(self, status):
        if self.callback is not None:
            self.callback(status)

    def on_error(self, status_code):
        return True  # Don't kill the stream

    def on_timeout(self):
        return True  # Don't kill the stream


if __name__ == '__main__':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
    main()
<commit_msg>Get IP and port from environment<commit_after>#!/usr/bin/env python
# encoding: utf-8

from __future__ import unicode_literals

import codecs
import socket
import sys
import threading
import os

import tweepy

HASHTAGS = ['#IoT', 'python']

IP = os.environ['ULTAMATION_IP']
UDP_PORT = int(os.environ['ULTAMATION_UDP_PORT'])

_CONSUMER_KEY = os.environ['CONSUMER_KEY']
_CONSUMER_SECRET = os.environ['CONSUMER_SECRET']
_ACCESS_TOKEN = os.environ['ACCESS_TOKEN']
_ACCESS_TOKEN_SECRET = os.environ['ACCESS_TOKEN_SECRET']


TIMERS = {}


def main():
    auth = tweepy.auth.OAuthHandler(_CONSUMER_KEY, _CONSUMER_SECRET)
    auth.set_access_token(_ACCESS_TOKEN, _ACCESS_TOKEN_SECRET)
    #api = tweepy.API(auth)

    stream = tweepy.Stream(auth, CustomStreamListener(tweet_callback))
    stream.filter(None, HASHTAGS)  # blocking function


def tweet_callback(status):
    #print('%s:  "%s" (%s via %s)\n' % (status.author.screen_name,
    #                                   status.text,
    #                                   status.created_at,
    #                                   status.source))
    for i, hashtag in enumerate(HASHTAGS):
        if hashtag.lower() in status.text.lower():
            print("'{}' in tweet".format(hashtag))
            turn_on_five_seconds(i)
        else:
            print("'{}' NOT in: {}".format(hashtag, status.text))


def turn_on_five_seconds(i):
    existing_timer = TIMERS.get(i)
    if existing_timer:
        existing_timer.cancel()

    turn_on_now(i)
    TIMERS[i] = threading.Timer(5, turn_off_now, [i])
    TIMERS[i].start()


def turn_on_now(i):
    print("ON: #{}".format(i))
    send_udp_data(IP, UDP_PORT, str('N:0000:1{}01:0001:1'.format(i + 2)))


def turn_off_now(i):
    print("OFF: #{}".format(i))
    send_udp_data(IP, UDP_PORT, str('N:0000:1{}01:0001:0'.format(i + 2)))


def send_udp_data(ip, port, data):
    sock = socket.socket(socket.AF_INET,     # Internet
                         socket.SOCK_DGRAM)  # UDP
    sock.sendto(data, (ip, port))


class CustomStreamListener(tweepy.StreamListener):

    def __init__(self, callback=None):
        self.callback = callback
        super(CustomStreamListener, self).__init__()

    # Alternatively, override on_data instead (then on_status won't be called)

    def on_status(self, status):
        if self.callback is not None:
            self.callback(status)

    def on_error(self, status_code):
        return True  # Don't kill the stream

    def on_timeout(self):
        return True  # Don't kill the stream


if __name__ == '__main__':
    sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
    main()

{'fraction_non_alphanumeric': 0.12864360668480374, 'fraction_numerical': 0.011270890011659542, 'mean_word_length': 3.0023328149300155, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2900325', 'n_tokens_mistral': 1880, 'n_tokens_neox': 1778, 'n_words': 347}