1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2### BEGIN LICENSE
3# Copyright (c) 2019, James Miller
4# Copyright (c) 2019, Peter Levi <peterlevi@peterlevi.com>
5# This program is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License version 3, as published
7# by the Free Software Foundation.
8#
9# This program is distributed in the hope that it will be useful, but
10# WITHOUT ANY WARRANTY; without even the implied warranties of
11# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
12# PURPOSE.  See the GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License along
15# with this program.  If not, see <http://www.gnu.org/licenses/>.
16### END LICENSE
17
18from locale import gettext as _
19
20import requests
21
22from variety.plugins.IQuoteSource import IQuoteSource
23
24
25class UrbanDictionarySource(IQuoteSource):
26    @classmethod
27    def get_info(cls):
28        return {
29            "name": "Urban Dictionary",
30            "description": _("Displays definitions from Urban Dictionary"),
31            "author": "James Miller",
32            "version": "0.1",
33        }
34
35    def get_random(self):
36        dict_dict = requests.get("https://api.urbandictionary.com/v0/random").json()
37
38        def _clean(s):
39            return s.strip().replace("[", "").replace("]", "")
40
41        result = []
42        for entry in dict_dict["list"]:
43            word = entry["word"]
44            definition = _clean(entry["definition"])
45            example = _clean(entry["example"])
46            quote = (
47                '"{}"'.format(word)
48                + "\n\n"
49                + definition
50                + ("\n\nExample:\n{}".format(example) if example else "")
51            )
52
53            result.append(
54                {
55                    "quote": quote,
56                    "author": entry["author"],
57                    "sourceName": "Urban Dictionary",
58                    "link": entry["permalink"],
59                }
60            )
61
62        return result
63