Beispiel #1
0
    def test_01(self):
        youtube_id = "4VYAaLh3XZg"

        scopes = ["https://www.googleapis.com/auth/youtube.readonly"]

        cachefile = os.path.join(FILE_DIR, "token.pickle")
        cachefuncs = CredentialCache.filepath2cachefuncs_pickle(cachefile)

        flow = InstalledAppFlow.from_client_secrets_file(
            FoxylibGoogleapi.Oauth.filepath_credentials(), scopes)
        credentials = GoogleAPITool.cache_or_func2cred(
            cachefuncs, partial(flow.run_local_server, port=0))

        youtube = googleapiclient.discovery.build("youtube",
                                                  "v3",
                                                  credentials=credentials,
                                                  cache_discovery=False)

        request = youtube.videos().list(
            part="snippet,contentDetails,statistics", id=youtube_id)
        response = request.execute()

        hyp_01 = JsonTool.down(response, ["kind"])
        self.assertEqual(hyp_01, "youtube#videoListResponse")

        item = l_singleton2obj(JsonTool.down(response, ["items"]))

        hyp_02 = JsonTool.down(item, ["id"])
        self.assertEqual(hyp_02, youtube_id)

        hyp_03 = JsonTool.down(item, ["kind"])
        self.assertEqual(hyp_03, "youtube#video")
Beispiel #2
0
 def doc2product_list(cls, doc):
     tradegoods = doc.get("tradegoods") or []
     product_list = [{
         Product.Field.TRADEGOOD:
         JsonTool.down(tg, ["name", "en"]),
         Product.Field.PRICE:
         JsonTool.down(tg, ["price"]),
     } for tg in tradegoods]
     return product_list
Beispiel #3
0
        def result_item2price(item):
            port_id = JsonTool.down(item, ["_id", cls.Field.PORT_ID])
            tradegood_id = JsonTool.down(item, ["_id", cls.Field.TRADEGOOD_ID])

            price = {MarketpriceDoc.Field.PORT: PortDoc.id2codename(port_id),
                     MarketpriceDoc.Field.TRADEGOOD: TradegoodDoc.id2codename(tradegood_id),
                     MarketpriceDoc.Field.RATE: int(item[cls.Field.RATE]),
                     MarketpriceDoc.Field.TREND: cls.trend_int2trend_price(item[cls.Field.TREND]),
                     }
            return price
Beispiel #4
0
        def item2doc(item):
            port_codename = JsonTool.down(item, ["_id", cls.Field.PORT])
            tradegood_codename = JsonTool.down(item, ["_id", cls.Field.TRADEGOOD])

            price = merge_dicts([DictTool.keys2filtered(item, fields_others, ),
                                 {cls.Field.PORT: port_codename,
                                  cls.Field.TRADEGOOD: tradegood_codename,
                                  },
                                 ], vwrite=vwrite_no_duplicate_key
                                )

            return price
Beispiel #5
0
    def test_04(self):
        logger = FoxylibLogger.func_level2logger(self.test_04, logging.DEBUG)

        channel = "C014DTPM24V"
        headers = {"Content-type": 'application/json',
                   "Authorization": "Bearer {}".format(FoxylibSlack.xoxb_token()),
                   }
        json1 = {"channel": channel,
             "text": "Hello world :tada:",
             }

        response1 = requests.post("https://slack.com/api/chat.postMessage",
                                 headers=headers,
                                 json=json1)

        self.assertEqual(response1.status_code, requests.codes.ok)

        j1 = response1.json()
        self.assertTrue(j1.get("ok"), j1)

        ts = JsonTool.down(j1, ["message", "ts"])
        json2 = {"channel": channel,
             "ts": ts,
             }

        response2 = requests.post("https://slack.com/api/chat.delete",
                                 headers=headers,
                                 json=json2)

        self.assertEqual(response2.status_code, requests.codes.ok)

        j2 = response2.json()
        self.assertTrue(j2.get("ok"), j2)
Beispiel #6
0
    def marketprice_doc_iter(cls):
        logger = HenriqueLogger.func_level2logger(cls.marketprice_doc_iter,
                                                  logging.DEBUG)

        # h_id2culture = CultureTable.dict_id2codename()
        # logger.debug({"h_id2culture":h_id2culture})

        with HenriquePostgres.cursor() as cursor:
            sql = SQL("SELECT * FROM {} ORDER BY id ASC").format(
                Identifier(cls.NAME), )
            cursor.execute(sql)

            for t in PostgresTool.fetch_iter(cursor):
                port_codename = cls.tuple2port_name_en(t)
                tradegood_codename = cls.tuple2tradegood_name_en(t)
                rate = cls.tuple2rate(t)
                trend = Trend.int2trend(cls.tuple2trend(t))

                data = cls.tuple2j(t)
                server_codename = JsonTool.down(data, ["server"])

                doc = {
                    MarketpriceDoc.Field.PORT: port_codename,
                    MarketpriceDoc.Field.TRADEGOOD: tradegood_codename,
                    MarketpriceDoc.Field.RATE: rate,
                    MarketpriceDoc.Field.TREND: trend,
                    MarketpriceDoc.Field.SERVER: server_codename,
                    # MarketpriceDoc.Field.CHATROOM_USER: channel_user,
                }
                yield doc
Beispiel #7
0
        def codename2texts(codename):
            for lang in langs:
                aliases = JsonTool.down(h_lang2codename2aliases, [lang, codename])
                if not aliases:
                    continue

                yield from aliases
Beispiel #8
0
    def env2ip(cls, env):
        logger = HenriqueLogger.func_level2logger(cls.env2ip, logging.DEBUG)

        j_server = cls.j_server()
        # logger.debug({"j_server":j_server})

        ip = JsonTool.down(j_server, [cls.Constant.ROOT, env, "ip"])
        #logger.debug({"[cls.Constant.ROOT,env,ip]":[cls.Constant.ROOT,env,"ip"]})
        return ip
Beispiel #9
0
    def v_unit_lang2str(cls, v, unit, lang):
        j_yaml = cls.yaml()
        str_unit = JsonTool.down(j_yaml, [unit, lang])[0]

        if lang in {"en"}:
            return " ".join([str(v), str_unit])

        if lang in {"ko"}:
            return "".join([str(v), str_unit])

        raise Exception("Invalid language: {}".format(lang))
Beispiel #10
0
    def test_01(self):
        logger = HenriqueLogger.func_level2logger(self.test_01, logging.DEBUG)

        response1 = ErrorsChannel.post("testing...")
        self.assertEqual(response1.status_code, requests.codes.ok, response1)

        j1 = response1.json()
        # logger.debug({"j1":j1})
        self.assertTrue(j1.get("ok"), response1.content)

        ts = JsonTool.down(j1, ["message", "ts"])
        response2 = ErrorsChannel.delete(ts)
        self.assertEqual(response2.status_code, requests.codes.ok, response2)

        j2 = response2.json()
        self.assertTrue(j2.get("ok"), response2.content)
Beispiel #11
0
 def codename_lang2text(cls, codename, lang):
     return JsonTool.down(cls.yaml(), [codename, lang])
Beispiel #12
0
 def port_lang2comments(cls, port, lang):
     return JsonTool.down(port, [cls.Field.COMMENTS, lang])
Beispiel #13
0
 def lang2str_idk(cls, lang):
     return JsonTool.down(cls.yaml(), [cls.Key.I_DONT_KNOW, lang])
Beispiel #14
0
 def str_timedelta2relativetimedelta(cls, str_timedelta, lang):
     suffix_format = JsonTool.down(cls.yaml(), [cls.Key.SUFFIX, lang])
     return suffix_format.format(str_timedelta)
Beispiel #15
0
 def tuple2chatuser_uuid(cls, t):
     return JsonTool.down(cls.tuple2j(t), ["chatuser", "uuid"])
Beispiel #16
0
 def tuple2trend(cls, t):
     trend = JsonTool.down(cls.tuple2j(t),
                           ["market_status", "trend", "value"])
     return trend - 2
Beispiel #17
0
 def tuple2tradegood_name_en(cls, t):
     return JsonTool.down(cls.tuple2j(t),
                          ["market_status", "tradegood", "name", "en"])
Beispiel #18
0
 def doc_lang2names(cls, doc, lang):
     names = JsonTool.down(doc, [cls.Field.NAMES, lang]) or []
     return names
Beispiel #19
0
 def item2message(cls, item):
     msg = JsonTool.down(item,
                         ["snippet", "textMessageDetails", "messageText"])
     return msg
Beispiel #20
0
 def tuple2port_name_en(cls, t):
     return JsonTool.down(cls.tuple2j(t),
                          ["market_status", "port", "name", "en"])
Beispiel #21
0
 def document2text(cls, document):
     return GoogledocsElement.read_strucutural_elements(
         JsonTool.down(document, ["body", "content"]))
Beispiel #22
0
 def server_lang2aliases(cls, server, lang):
     return JsonTool.down(server, [cls.Field.ALIASES, lang]) or []
Beispiel #23
0
 def tradegood_lang2aliases(cls, tradegood, lang):
     return JsonTool.down(tradegood, [cls.Field.ALIASES, lang])
Beispiel #24
0
 def port_lang2aliases(cls, port, lang):
     return JsonTool.down(port, [cls.Field.ALIASES, lang])
Beispiel #25
0
 def tuple2rate(cls, t):
     return JsonTool.down(cls.tuple2j(t), ["market_status", "rate"])
Beispiel #26
0
    def culture_lang2aliases(cls, culture, lang):
        logger = HenriqueLogger.func_level2logger(cls.culture_lang2aliases, logging.DEBUG)
        aliases = JsonTool.down(culture, [cls.Field.ALIASES, lang])

        # logger.debug({"culture": culture, "lang": lang, "aliases": aliases})
        return aliases
Beispiel #27
0
 def data_lang2aliases_en(cls, data, lang):
     name_en = JsonTool.down(data, ["name", lang])
     nicknames_en = JsonTool.down(data, ["nicknames", lang]) or []
     return luniq(chain([name_en], nicknames_en))