<commit_before># -*- coding: utf-8 -*-

import datetime
import time
import urllib
import json
import socket
import re
import random
import requests
from slackbot.bot import Bot, listen_to
from slackbot.manager import PluginsManager
from tinydb import TinyDB, Query
import bertil_secrets
from apiclient.discovery import build
from apiclient.errors import HttpError


def fetch_food_json():
    response = urllib.urlopen('http://www.hanssonohammar.se/veckansmeny.json')
    return json.loads(response.read().decode('utf-8'))


def get_food_from_json(data, day):
    if day not in data:
        return "(no mat " + str(day) + ")"

    mat_today = data[day][0]

    if 'IKSU' not in mat_today:
        return "(no IKSU today)"

    return "\n".join(mat_today['IKSU'])


def get_food(day):
    # Get JSON
    data = fetch_food_json()
    return get_food_from_json(data, day)


@listen_to(r'^help$')
def bertil_help(message):
    func_names = [p.pattern for p, _ in PluginsManager.commands['listen_to'].iteritems()]
    message.reply(u'Jag kan följade kommandon:\n```{}```'.format('\n'.join(func_names)))


@listen_to(r'^veckans mat$')
def veckans_mat(message):
    days = [u"Måndag", u"Tisdag", u"Onsdag", u"Torsdag", u"Fredag"]
    today = datetime.datetime.today().weekday()
    nextweek = 0

    if today > 4:
        nextweek = 7 - today
        today = 0

    data = fetch_food_json()

    fulltext = u""
    for daynum in range(0, len(days) - today):
        date = datetime.date.fromtimestamp(time.time() + (86400 * nextweek) + (86400 * daynum))
        try:
            fulltext += u"\n{}\n{}\n".format(days[today+daynum],
                                             get_food_from_json(data, str(date)))
        except Exception as exception:
            if exception.message:
                fulltext += u"\n{}\n{}\n".format(days[today+daynum], exception.message)
            else:
                fulltext += u"\n{}\n{}\n".format(days[today+daynum], u"Okänt fel")

    if fulltext:
        message.reply(u"```{}```".format(fulltext))
    else:
        message.reply(u"Hittade ingen mat.")


@listen_to(r'^vecka$')
def vecka(message):
    week = datetime.datetime.now().isocalendar()[1]
    message.reply(u"Vecka {}".format(week))


@listen_to(r'^datum$')
def datum(message):
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    message.reply(u"{}".format(date))


@listen_to(r'^mat(\+*)$')
def mat(message, plus):
    date = datetime.date.fromtimestamp(time.time() + (86400 * len(plus)))
    try:
        message.reply(u"```IKSU - {}\n{}```".format(str(date), get_food(str(date))))
    except Exception as exception:
        message.reply(u"Kom inte åt maten 😞 ({what})".format(what=exception.message))


@listen_to(r'^youtube(.*)')
def youtube(message, query):
    developer_key = bertil_secrets.YOUTUBE_API_KEY
    youtube_api_service_name = 'youtube'
    youtube_api_version = 'v3'
    max_results = 1

    try:
        youtube_api = build(youtube_api_service_name,
                            youtube_api_version,
                            developerKey=developer_key)

        search_response = youtube_api.search().list(
            q=query,
            part='id, snippet',
            maxResults=max_results
        ).execute()

    except HttpError, err:
        message.reply('HTTP error {} happen:\n{}'.format(err.resp.status, err.content))

    videos = []

    for search_result in search_response.get('items', []):
        if search_result['id']['kind'] == 'youtube#video':
            videos.append('{} (https://www.youtube.com/watch?v={})'.format(
                search_result['snippet']['title'],
                search_result['id']['videoId']))<commit_msg>Maybe fix unicode crap<commit_after># -*- coding: utf-8 -*-

import datetime
import time
import urllib
import json
import socket
import re
import random
import requests
from slackbot.bot import Bot, listen_to
from slackbot.manager import PluginsManager
from tinydb import TinyDB, Query
import bertil_secrets
from apiclient.discovery import build
from apiclient.errors import HttpError


def fetch_food_json():
    response = urllib.urlopen('http://www.hanssonohammar.se/veckansmeny.json')
    return json.loads(response.read().decode('utf-8'))


def get_food_from_json(data, day):
    if day not in data:
        return "(no mat " + str(day) + ")"

    mat_today = data[day][0]

    if 'IKSU' not in mat_today:
        return "(no IKSU today)"

    return "\n".join(mat_today['IKSU'])


def get_food(day):
    # Get JSON
    data = fetch_food_json()
    return get_food_from_json(data, day)


@listen_to(r'^help$')
def bertil_help(message):
    func_names = [p.pattern for p, _ in PluginsManager.commands['listen_to'].iteritems()]
    message.reply(u'Jag kan följade kommandon:\n```{}```'.format('\n'.join(func_names)))


@listen_to(r'^veckans mat$')
def veckans_mat(message):
    days = [u"Måndag", u"Tisdag", u"Onsdag", u"Torsdag", u"Fredag"]
    today = datetime.datetime.today().weekday()
    nextweek = 0

    if today > 4:
        nextweek = 7 - today
        today = 0

    data = fetch_food_json()

    fulltext = u""
    for daynum in range(0, len(days) - today):
        date = datetime.date.fromtimestamp(time.time() + (86400 * nextweek) + (86400 * daynum))
        try:
            fulltext += u"\n{}\n{}\n".format(days[today+daynum],
                                             get_food_from_json(data, str(date)))
        except Exception as exception:
            if exception.message:
                fulltext += u"\n{}\n{}\n".format(days[today+daynum], exception.message)
            else:
                fulltext += u"\n{}\n{}\n".format(days[today+daynum], u"Okänt fel")

    if fulltext:
        message.reply(u"```{}```".format(fulltext))
    else:
        message.reply(u"Hittade ingen mat.")


@listen_to(r'^vecka$')
def vecka(message):
    week = datetime.datetime.now().isocalendar()[1]
    message.reply(u"Vecka {}".format(week))


@listen_to(r'^datum$')
def datum(message):
    date = datetime.datetime.now().strftime('%Y-%m-%d')
    message.reply(u"{}".format(date))


@listen_to(r'^mat(\+*)$')
def mat(message, plus):
    date = datetime.date.fromtimestamp(time.time() + (86400 * len(plus)))
    try:
        message.reply(u"```IKSU - {}\n{}```".format(str(date), get_food(str(date))))
    except Exception as exception:
        message.reply(u"Kom inte åt maten 😞 ({what})".format(what=exception.message))


@listen_to(r'^youtube(.*)')
def youtube(message, query):
    developer_key = bertil_secrets.YOUTUBE_API_KEY
    youtube_api_service_name = 'youtube'
    youtube_api_version = 'v3'
    max_results = 1

    try:
        youtube_api = build(youtube_api_service_name,
                            youtube_api_version,
                            developerKey=developer_key)

        search_response = youtube_api.search().list(
            q=query,
            part='id, snippet',
            maxResults=max_results
        ).execute()

    except HttpError, err:
        message.reply(u'HTTP error {} happen:\n{}'.format(err.resp.status, err.content))

    videos = []

    for search_result in search_response.get('items', []):
        if search_result['id']['kind'] == 'youtube#video':
            videos.append(u'{} (https://www.youtube.com/watch?v={})'.format(
                search_result['snippet']['title'],
                search_result['id']['videoId']))
{'fraction_non_alphanumeric': 0.15513740354677136, 'fraction_numerical': 0.007039393529172871, 'mean_word_length': 2.963519313304721, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2385654', 'n_tokens_mistral': 2573, 'n_tokens_neox': 2397, 'n_words': 478}