<commit_before>import isodate

import smartbot
from smartbot import utils
from smartbot.exceptions import *
from smartbot.formatting import *


class Plugin(smartbot.Plugin):
    """Get information about posted YouTube videos."""
    names = ["youtube", "utube"]

    def __init__(self, key):
        self.key = key

    def _get_reply(self, video):
        title = video["snippet"]["title"]
        channelTitle = video["snippet"]["channelTitle"]
        duration = isodate.parse_duration(video["contentDetails"]["duration"])
        views = video["statistics"]["viewCount"]
        likes = video["statistics"]["likeCount"]
        dislikes = video["statistics"]["dislikeCount"]

        return "{} by {} | {} | {} {} {}".format(
            self.bot.format(title, Style.underline),
            self.bot.format(channelTitle, Style.underline),
            duration,
            views,
            self.bot.format(likes, Colour.fg_green),
            self.bot.format(dislikes, Colour.fg_red)
        )

    def _get_video_info(self, video_id):
        url = "https://www.googleapis.com/youtube/v3/videos"
        payload = {
            "key": self.key,
            "id": video_id,
            "part": ",".join(["contentDetails", "snippet", "statistics"])
        }

        s = utils.web.requests_session()
        res = s.get(url, params=payload).json()
        if res["items"]:
            return res["items"][0]

    def on_command(self, msg, stdin, stdout, reply):
        for video_id in msg["args"][1:]:
            video = self._get_video_info(video_id)
            if video:
                print(self._get_reply(video), file=stdout)

    def on_help(self):
        return "{} {} …".format(
            super().on_help(),
            self.bot.format("id", Style.underline)
        )<commit_msg>Add searching youtube with plugin, close #53<commit_after>import isodate

import smartbot
from smartbot import utils
from smartbot.exceptions import *
from smartbot.formatting import *


class Plugin(smartbot.Plugin):
    """Get information about posted YouTube videos."""
    names = ["youtube", "utube"]

    def __init__(self, key):
        self.key = key

    def _search(self, query):
        url = "https://www.googleapis.com/youtube/v3/search"
        payload = {
            "key": self.key,
            "q": query,
            "maxResults": 3,
            "part": "snippet"
        }

        s = utils.web.requests_session()
        res = s.get(url, params=payload).json()
        return res.get("items", [])

    def _format_result(self, i, result):
        url = "https://www.youtube.com/watch?v={}".format(result["id"]["videoId"])
        return "{}: {} {}".format(
            self.bot.format("[{}]".format(i), Style.bold),
            self.bot.format(result["snippet"]["title"], Style.underline),
            url
        )

    def on_command(self, msg, stdin, stdout, reply):
        query = " ".join(msg["args"][1:])
        if not query:
            query = stdin.read().strip()

        results = self._search(query)
        for i, result in enumerate(results):
            print(self._format_result(i, result), file=stdout)

    def on_help(self):
        return "{} {}".format(
            super().on_help(),
            self.bot.format("query", Style.underline)
        )
{'fraction_non_alphanumeric': 0.15174506828528073, 'fraction_numerical': 0.0024279210925644916, 'mean_word_length': 2.3059177532597794, 'pattern_counts': {'":': 7, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 3, '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': '2856109', 'n_tokens_mistral': 987, 'n_tokens_neox': 979, 'n_words': 204}