<commit_before>#!/usr/bin/python

# ###########################################################################
# LIFX API CONTROLLER
# AUTHOR: <NAME>
# DESC: Implements LIFX control via their Cloud API
# ###########################################################################

import config  # import our configuration
import lifx_config  # import lifx config
import paho.mqtt.client as mqtt
import os

# Interfacing with LIFX additional improts......
import pycurl
from io import BytesIO


def _lifx_json_get(url):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, lifx_config.lifx_apiserver + url)
    c.setopt(c.HTTPHEADER, ['Authorization: Bearer ' +
                            lifx_config.lifx_apikey])
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()

    body = buffer.getvalue().decode('iso-8859-1')

    return body


def _lifx_json_put(url, data):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, lifx_config.lifx_apiserver + url)
    c.setopt(c.HTTPHEADER, ['Authorization: Bearer ' +
                            lifx_config.lifx_apikey])
    c.setopt(c.CUSTOMREQUEST, 'PUT')
    c.setopt(c.POSTFIELDS, data)
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()

    body = buffer.getvalue().decode('iso-8859-1')

    return body


def handle_lifx_message(client, msg, smsg):
    # client = mqtt client
    # msg = msg object
    # smsg = string of content

    parts = smsg.split("|")

    if(len(parts) < 1):
        return

    cmd = parts[0]

    # parts == 2
    if(len(parts) == 2):
        if(cmd == "turn_on"):  # !turn_on,light_id
            light_id = parts[1]
            print(_lifx_json_put("/lights/id:" +
                                 light_id + "/power",
                                 "state=on&duration=0.0"))

        if(cmd == "turn_off"):  # !turn_off,light_id
            light_id = parts[1]
            print(_lifx_json_put("/lights/id:" +
                                 light_id + "/power",
                                 "state=off&duration=0.0"))

    if(len(parts) == 3):
        if(cmd == "set_brightness"):  # !turn_off,light_id
            light_id = parts[1]
            brightness = parts[2]
            print(_lifx_json_put("/lights/id:" +
                                 light_id + "/color",
                                 "power_on=false&duration=0.0" +
                                 "&color=brightness:" + brightness))<commit_msg>Should have fixed all occurances of incorrect multi line concats (fixes #6)<commit_after>#!/usr/bin/python

# ###########################################################################
# LIFX API CONTROLLER
# AUTHOR: <NAME>
# DESC: Implements LIFX control via their Cloud API
# ###########################################################################

import config  # import our configuration
import lifx_config  # import lifx config
import paho.mqtt.client as mqtt
import os

# Interfacing with LIFX additional improts......
import pycurl
from io import BytesIO


def _lifx_json_get(url):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, lifx_config.lifx_apiserver + url)
    c.setopt(c.HTTPHEADER, ['Authorization: Bearer ' + \
                            lifx_config.lifx_apikey])
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()

    body = buffer.getvalue().decode('iso-8859-1')

    return body


def _lifx_json_put(url, data):
    buffer = BytesIO()
    c = pycurl.Curl()
    c.setopt(c.URL, lifx_config.lifx_apiserver + url)
    c.setopt(c.HTTPHEADER, ['Authorization: Bearer ' + \
                            lifx_config.lifx_apikey])
    c.setopt(c.CUSTOMREQUEST, 'PUT')
    c.setopt(c.POSTFIELDS, data)
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    c.close()

    body = buffer.getvalue().decode('iso-8859-1')

    return body


def handle_lifx_message(client, msg, smsg):
    # client = mqtt client
    # msg = msg object
    # smsg = string of content

    parts = smsg.split("|")

    if(len(parts) < 1):
        return

    cmd = parts[0]

    # parts == 2
    if(len(parts) == 2):
        if(cmd == "turn_on"):  # !turn_on,light_id
            light_id = parts[1]
            print(_lifx_json_put("/lights/id:" + \
                                 light_id + "/power",
                                 "state=on&duration=0.0"))

        if(cmd == "turn_off"):  # !turn_off,light_id
            light_id = parts[1]
            print(_lifx_json_put("/lights/id:" + \
                                 light_id + "/power",
                                 "state=off&duration=0.0"))

    if(len(parts) == 3):
        if(cmd == "set_brightness"):  # !turn_off,light_id
            light_id = parts[1]
            brightness = parts[2]
            print(_lifx_json_put("/lights/id:" + \
                                 light_id + "/color",
                                 "power_on=false&duration=0.0" + \
                                 "&color=brightness:" + brightness))
{'fraction_non_alphanumeric': 0.18190934616927396, 'fraction_numerical': 0.01022864019253911, 'mean_word_length': 2.074599260172626, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4898524', 'n_tokens_mistral': 1648, 'n_tokens_neox': 1580, 'n_words': 302}