<commit_before>import tingbot
from tingbot import *
import urllib2
import json
import time


temperature_str = "n/a"
description_str = ""
icon_str = "icons/na.png"
screen_index = 0
max_screens = 3
forecast_list = []
update_time_str = ""
default_brightness = 75

brightness_dict = {}

# location settings from settings.json
# replace any space with %20 so it can be passed in url
city = tingbot.app.settings ['city']
city_url = city.replace (" ", "%20")
state = tingbot.app.settings ['state']
state_url = state.replace (" ", "%20")
country = tingbot.app.settings ['country']
country_url = country.replace (" ", "%20")

# update screen primarily displaying time
def update_time_screen (current_time, current_date):
    
    global temperature_str
    global icon_str
    
    screen.fill (color='black')
    screen.text (temperature_str, align="topright", color="white", font_size=35)
    screen.text (current_time, align="center", color="white", font_size=65)
    screen.text (current_date, align="bottom", color="white", font_size=25)
    screen.text ("@ " + update_time_str, align="right", color="grey", xy=(320,55), font_size=15)
    if len (icon_str) > 0:
        screen.image (icon_str, align='topleft', scale='fit', max_width=75, max_height=75)


# update screen primarily displaying current weather
def update_weather_screen (current_time):
    
    global temperature_str
    global icon_str
    global description_str
    
    screen.fill (color='black')
    screen.text (current_time, align="topright", color="white", font_size=35)
    screen.text (temperature_str, align="center", color="white", font_size=70)
    screen.text ("@ " + update_time_str, align="right", color="grey", xy=(320,145), font_size=15)
    if len (forecast_list) > 0:
        hi_lo = forecast_list [0][1] + " / " + forecast_list [0][2]
        screen.text (hi_lo, align="center", color="white", xy=(160,180), font_size=25)
    screen.text (description_str, align="bottom", color="white", font_size=25)
    if len (icon_str) > 0:
        screen.image (icon_str, align="topleft", scale='fit', max_width=150, max_height=150)


# update screen displaying multi day forecast
def update_forecast_screen ():
    
    screen.fill (color='black')
    if len (forecast_list) == 0:
        screen.text ("forecast unavailable", align="top", color="white", font_size=30)
        return
    
    stop = min (len (forecast_list), 4)
    screen.text (city + " " + str (stop) + " day forecast", align="top", color="white", font_size=22)
    for i in range (0, stop):
        forecast = forecast_list [i]
        # create string <day> <high> <low> <desc>
        line = forecast [0] + ":\t" + forecast [1] + "/" + forecast [2] + "\t" + forecast [3]
        screen.text (line, align="left", xy=(0, (i+1) * 55), color="white", font_size = 20)
        if code_to_icon_map.has_key (forecast [4]):
            icon_str = code_to_icon_map [forecast [4]]
        else:
            icon_str = "icons/na.png"
        # alignment is relative to xy, xy with topright alignment makes xy the topright point in the icon
        screen.image (icon_str, align="right", xy=(320, (i+1) * 55), scale='fit', max_width=75, max_height=75)


# move to previous screen
@left_button.press
def screen_left ():
    
    global screen_index
    
    if screen_index == 0:
        screen_index = max_screens - 1
    else:
        screen_index -= 1


# decrease brightness by 5
@midleft_button.press
def screen_midleft ():
    
    brightness = screen.brightness

    brightness -= 5
    if brightness < 0:
        brightness = 0
        
    screen.brightness = brightness
    
    screen.rectangle (align='center', size=(280,40), color='black')
    screen.text ("Brightness " + str (brightness), align='center')


# increase brightness by 5
@midright_button.press
def screen_midright ():
    
    brightness = screen.brightness

    brightness += 5
    if brightness > 100:
        brightness = 100
        
    screen.brightness = brightness
    
    screen.rectangle (align='center', size=(280,40), color='black')
    screen.text ("Brightness " + str (brightness), align='center')


# move to next screen    
@right_button.press
def screen_right ():
    
    global screen_index
    
    screen_index += 1
    if screen_index >= max_screens:
        screen_index = 0


# update temperature from yahoo
@every(minutes=15.0)
def update_temperature_data_yahoo ():
    
    url_string = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city_url + "%2C%20" + state_url + "%2C%20" + country_url + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
    response = urllib2.urlopen (url_string).read ()
    
    response_dict = json.loads (response)
    
    print response_dict
 
    global temperature_str
    global description_str
    global icon_str
    global forecast_list
    global update_time_str
    
    try:
        print response_dict['query']['results']['channel']['item']['condition']['temp'] + " F"
        temperature_str = response_dict['query']['results']['channel']['item']['condition']['temp'] + " F"
    except:
        temperature_str = ""
        
    try:<commit_msg>support for openweathermap<commit_after>import tingbot
from tingbot import *
import urllib2
import json
import time


temperature_str = "n/a"
description_str = ""
icon_str = "icons/na.png"
screen_index = 0
max_screens = 3
forecast_list = []
update_time_str = ""
default_brightness = 75

brightness_dict = {}

# location settings from settings.json
# replace any space with %20 so it can be passed in url
city = tingbot.app.settings ['city']
city_url = city.replace (" ", "%20")
state = tingbot.app.settings ['state']
state_url = state.replace (" ", "%20")
country = tingbot.app.settings ['country']
country_url = country.replace (" ", "%20")

# update screen primarily displaying time
def update_time_screen (current_time, current_date):
    
    global temperature_str
    global icon_str
    
    screen.fill (color='black')
    screen.text (temperature_str, align="topright", color="white", font_size=35)
    screen.text (current_time, align="center", color="white", font_size=65)
    screen.text (current_date, align="bottom", color="white", font_size=25)
    screen.text ("@ " + update_time_str, align="right", color="grey", xy=(320,55), font_size=15)
    if len (icon_str) > 0:
        screen.image (icon_str, align='topleft', scale='fit', max_width=75, max_height=75)


# update screen primarily displaying current weather
def update_weather_screen (current_time):
    
    global temperature_str
    global icon_str
    global description_str
    
    screen.fill (color='black')
    screen.text (current_time, align="topright", color="white", font_size=35)
    screen.text (temperature_str, align="center", color="white", font_size=70)
    screen.text ("@ " + update_time_str, align="right", color="grey", xy=(320,145), font_size=15)
    if len (forecast_list) > 0:
        hi_lo = forecast_list [0][1] + " / " + forecast_list [0][2]
        screen.text (hi_lo, align="center", color="white", xy=(160,180), font_size=25)
    screen.text (description_str, align="bottom", color="white", font_size=25)
    if len (icon_str) > 0:
        screen.image (icon_str, align="topleft", scale='fit', max_width=150, max_height=150)


# update screen displaying multi day forecast
def update_forecast_screen ():
    
    screen.fill (color='black')
    if len (forecast_list) == 0:
        screen.text ("forecast unavailable", align="top", color="white", font_size=30)
        return
    
    stop = min (len (forecast_list), 4)
    screen.text (city + " " + str (stop) + " day forecast", align="top", color="white", font_size=22)
    for i in range (0, stop):
        forecast = forecast_list [i]
        # create string <day> <high> <low> <desc>
        line = forecast [0] + ":\t" + forecast [1] + "/" + forecast [2] + "\t" + forecast [3]
        screen.text (line, align="left", xy=(0, (i+1) * 55), color="white", font_size = 20)
        if code_to_icon_map.has_key (forecast [4]):
            icon_str = code_to_icon_map [forecast [4]]
        else:
            icon_str = "icons/na.png"
        # alignment is relative to xy, xy with topright alignment makes xy the topright point in the icon
        screen.image (icon_str, align="right", xy=(320, (i+1) * 55), scale='fit', max_width=75, max_height=75)


# move to previous screen
@left_button.press
def screen_left ():
    
    global screen_index
    
    if screen_index == 0:
        screen_index = max_screens - 1
    else:
        screen_index -= 1


# decrease brightness by 5
@midleft_button.press
def screen_midleft ():
    
    brightness = screen.brightness

    brightness -= 5
    if brightness < 0:
        brightness = 0
        
    screen.brightness = brightness
    
    screen.rectangle (align='center', size=(280,40), color='black')
    screen.text ("Brightness " + str (brightness), align='center')


# increase brightness by 5
@midright_button.press
def screen_midright ():
    
    brightness = screen.brightness

    brightness += 5
    if brightness > 100:
        brightness = 100
        
    screen.brightness = brightness
    
    screen.rectangle (align='center', size=(280,40), color='black')
    screen.text ("Brightness " + str (brightness), align='center')


# move to next screen    
@right_button.press
def screen_right ():
    
    global screen_index
    
    screen_index += 1
    if screen_index >= max_screens:
        screen_index = 0

# update temperature from openweathermap
@every(minutes=15.0)
def update_temperature_data_openweathermap ():
    
    url_string = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city_url + "%2C%20" + state_url + "%2C%20" + country_url + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
    response = urllib2.urlopen (url_string).read ()
    
    response_dict = json.loads (response)
    
    print response_dict

# update temperature from yahoo
#@every(minutes=15.0)
def update_temperature_data_yahoo ():
    
    url_string = "https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22" + city_url + "%2C%20" + state_url + "%2C%20" + country_url + "%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"
    response = urllib2.urlopen (url_string).read ()
    
    response_dict = json.loads (response)
    
    global temperature_str
    global description_str
    global icon_str
    global forecast_list
    global update_time_str
    
    try:
        print response_dict['query']['results']['channel']['item']['condition']['temp'] + " F"
        temperature_str = response_dict['query']['results']['channel']['item']['condition']['temp'] + " F"
    except:
        temperature_str = ""
        
    try:
{'fraction_non_alphanumeric': 0.1365196964221178, 'fraction_numerical': 0.03406216118539935, 'mean_word_length': 3.6724356268467706, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2064679', 'n_tokens_mistral': 3887, 'n_tokens_neox': 3584, 'n_words': 834}