Example #1
0
    def query():
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'error': 'Identity not found'}

        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={'user_id': 'john'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'error': 'User not found'}

        # correct user, must have read permission
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/read',
            cookies={'user_id': 'chris'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'allowed': True}

        # correct user, don't have write permission
        resp = yield from aiohttp.request(
            'GET', 'http://127.0.0.1:8080/auth/write',
            cookies={'user_id': 'chris'},
            loop=loop)
        json_data = yield from resp.json()
        assert json_data == {'allowed': False}

        print('Success')
Example #2
0
    def test_valid_create(self):
        data = dict(
            data=dict(a=1, b=2)
        )
        resp = yield from request(
            'POST', self.full_url('/'), loop=self.loop, data=json.dumps(data)
        )

        self.assertEqual(resp.status, 201)
        location = resp.headers['Location']

        resp2 = yield from request(
            'GET', self.full_url(location), loop=self.loop
        )

        self.assertEqual(resp2.status, 200)
        jresp = yield from resp2.json()

        body = jresp['body']
        self.assertIn('id', body)
        self.assertIn('data', body)
        self.assertIn('created', body)

        self.assertEqual(body['data'], data['data'])
        resp.close()
        resp2.close()
Example #3
0
def _scan_for_triggers(bot, event, command):
    limit = 3
    count = 0
    lctext = event.text.lower()
    image_links = []
    for trigger in _lookup:
        pattern = '\\b' + trigger + '\.(jpg|png|gif|bmp)\\b'
        if re.search(pattern, lctext):
            image_links.append(_get_a_link(trigger))
            count = count + 1
            if count >= limit:
                break

    image_links = list(set(image_links)) # make unique

    if len(image_links) > 0:
        for image_link in image_links:
            if "gfycat.com/" in image_link:
                r = yield from aiohttp.request('get', image_link)
                raw = yield from r.read()
                image_link = re.search("href=\"(.*?)\">GIF</a>", str(raw, 'utf-8')).group(1)
            filename = os.path.basename(image_link)
            r = yield from aiohttp.request('get', image_link)
            raw = yield from r.read()
            image_data = io.BytesIO(raw)
            logger.debug("uploading: {}".format(filename))
            image_id = yield from bot._client.upload_image(image_data, filename=filename)
            yield from bot.coro_send_message(event.conv.id_, None, image_id=image_id)
Example #4
0
def test_queue_request_store_multiple_requests(api_server, redis):
    url = urljoin(api_server, '/callbacks/app/queue/')
    yield from aiohttp.request('POST', url, data=json_payload)
    yield from aiohttp.request('POST', url, data=json_payload)
    yield from aiohttp.request('POST', url, data=json_payload)

    assert redis.llen('app-queue') == 3
Example #5
0
def test_method_not_allowed(api_server):
    expectation = {
        'method': 'GET',
        'path': '/v1/users/',
        'response': {'body': {'email': '*****@*****.**', 'name': 'John Doe'},
                     'headers': {'content_type': 'application/json'},
                     'status_code': 200}
    }

    url = urljoin(api_server, '/mocks/')

    # Configure the mock
    response = yield from aiohttp.request('PUT', url,
                                          data=json.dumps(expectation),
                                          headers=request_headers)

    assert response.status == 201
    assert response.reason == 'Created'
    assert response.headers['content-type'] == content_type
    response_json = yield from response.json()
    assert 'path' in response_json.keys()

    # Use the mock
    put_url = urljoin(api_server, response_json['path'])
    response = yield from aiohttp.request('PUT', put_url)

    assert response.status == 405
    assert response.reason == 'Method Not Allowed'
Example #6
0
    def query():
        resp = yield from aiohttp.request("GET", "http://127.0.0.1:8080/auth/read", cookies={}, loop=loop)
        json_data = yield from resp.json()
        assert json_data == {"error": "Identity not found"}

        resp = yield from aiohttp.request(
            "GET", "http://127.0.0.1:8080/auth/read", cookies={"user_id": "john"}, loop=loop
        )
        json_data = yield from resp.json()
        assert json_data == {"error": "User not found"}

        # correct user, must have read permission
        resp = yield from aiohttp.request(
            "GET", "http://127.0.0.1:8080/auth/read", cookies={"user_id": "chris"}, loop=loop
        )
        json_data = yield from resp.json()
        assert json_data == {"allowed": True}

        # correct user, don't have write permission
        resp = yield from aiohttp.request(
            "GET", "http://127.0.0.1:8080/auth/write", cookies={"user_id": "chris"}, loop=loop
        )
        json_data = yield from resp.json()
        assert json_data == {"allowed": False}

        print("Success")
Example #7
0
def run(test, count, concurrency, *, loop, verbose):
    if verbose:
        print("Prepare")
    else:
        print('.', end='', flush=True)
    host, port = find_port()
    barrier = Barrier(2)
    server = Process(target=test, args=(host, port, barrier))
    server.start()
    barrier.wait()

    url = 'http://{}:{}'.format(host, port)

    connector = aiohttp.TCPConnector(loop=loop)

    for i in range(10):
        # make server hot
        resp = yield from aiohttp.request('GET', url+'/prepare',
                                          connector=connector, loop=loop)
        assert resp.status == 200, resp.status
        resp.release()

    if verbose:
        test_name = test.__name__
        print("Attack", test_name)
    rps, data = yield from attack(count, concurrency, connector, loop, url)
    if verbose:
        print("Done")

    resp = yield from aiohttp.request('GET', url+'/stop',
                                      connector=connector, loop=loop)
    assert resp.status == 200, resp.status
    resp.release()
    server.join()
    return rps, data
Example #8
0
        def go():
            app = web.Application(loop=self.loop)
            app.router.add_route('GET', '/', func)
            app.router.add_route('POST', '/', func)

            port = self.find_unused_port()
            srv = yield from self.loop.create_server(
                app.make_handler(), '127.0.0.1', port)
            url = "http://127.0.0.1:{}/".format(port)

            resp = yield from aiohttp.request('GET', url, loop=self.loop)
            self.assertEqual(200, resp.status)

            # make sure that EventSourceResponse supports passing
            # custom headers
            self.assertEqual(resp.headers.get('X-SSE'), 'aiohttp_sse')

            # check streamed data
            streamed_data = yield from resp.text()
            expected = 'data: foo\r\n\r\n' \
                       'event: bar\r\ndata: foo\r\n\r\n' \
                       'id: xyz\r\nevent: bar\r\ndata: foo\r\n\r\n' \
                       'id: xyz\r\nevent: bar\r\ndata: foo\r\nretry: 1\r\n\r\n'
            self.assertEqual(streamed_data, expected)

            # check that EventSourceResponse object works only
            # with GET method
            resp = yield from aiohttp.request('POST', url, loop=self.loop)
            self.assertEqual(405, resp.status)

            srv.close()
            self.addCleanup(srv.close)
Example #9
0
 def get_body(self, root_url):
     domain = root_url.rsplit('/', 1)[1]
     page_path = '/opt/snare/pages/{}'.format(domain)
     if not os.path.exists(page_path):
         os.mkdir(page_path)
     response = yield from aiohttp.request('GET', root_url)
     data = yield from response.read()
     with open(page_path + '/index.html', 'wb') as index_fh:
         index_fh.write(data)
     urls = re.findall(r'(?i)(href|src)=["\']?([^\s"\'<>]+)', str(data))
     for url in urls:
         url = url[1]
         parsed_url = urlparse(url)
         if '/' in parsed_url.path:
             url_dir, file_name = parsed_url.path.rsplit('/', 1)
             if not os.path.exists(url_dir):
                 if url_dir.startswith('/'):
                     url_dir = url_dir[1:]
                 url_dir = os.path.join(page_path, url_dir)
                 print(url_dir)
                 try:
                     os.makedirs(url_dir, exist_ok=True)
                 except (FileExistsError, NotADirectoryError):
                     pass
                 try:
                     with open(os.path.join(url_dir, file_name), 'wb') as fh:
                         response = yield from aiohttp.request('GET', root_url + parsed_url.path)
                         data = yield from response.read()
                         fh.write(data)
                 except (IsADirectoryError, NotADirectoryError):
                     continue
        def go():
            app, srv, handler = yield from self._setup_app(func)

            # make sure that toolbar button present on apps page
            # add cookie to enforce performance panel measure time
            conn = aiohttp.TCPConnector(loop=self.loop, force_close=True)
            cookie = {"pdtb_active": "pDebugPerformancePanel"}
            resp = yield from aiohttp.request('GET', self.url, cookies=cookie,
                                              loop=self.loop, connector=conn)
            self.assertEqual(200, resp.status)
            txt = yield from resp.text()
            self.assertTrue('pDebugToolbarHandle' in txt)
            resp.close()

            # make sure that debug toolbar page working
            url = "{}/_debugtoolbar".format(self.url)
            resp = yield from aiohttp.request('GET', url, loop=self.loop,
                                              connector=conn)
            yield from resp.text()
            self.assertEqual(200, resp.status)
            resp.close()

            conn.close()

            yield from handler.finish_connections()
            srv.close()
Example #11
0
def _get_comic(bot, num=None):
    if num:
        num = int(num)
        url = 'https://xkcd.com/%d/info.0.json' % num
    else:
        num = None
        url = 'https://xkcd.com/info.0.json'
    
    if num in _cache:
        return _cache[num]
    else:
        request = yield from aiohttp.request('get', url)
        raw = yield from request.read()
        info = json.loads(raw.decode())
        
        if info['num'] in _cache:
            # may happen when searching for the latest comic
            return _cache[info['num']]
        
        filename = os.path.basename(info["img"])
        request = yield from aiohttp.request('get', info["img"])
        raw = yield from request.read()
        image_data = io.BytesIO(raw)
        info['image_id'] = yield from bot._client.upload_image(image_data, filename=filename)
        _cache[info['num']] = info
        return info
Example #12
0
 async def file_check(self):
     urls = ["https://images-2.discordapp.net/.eJwNwcENwyAMAMBdGABDCWCyDSKIoCYxwuZVdff27qPWvNSuTpHBO8DRudA8NAvN3Kp"
             "uRO2qeXTWhW7IIrmcd32EwQbjMCRMaJNxPmwILxcRg_9Da_yWYoQ3dV5z6fE09f0BC6EjAw.B0sII_QLbL9kJo6Zbb4GuO4MQNw",
             "https://cdn.discordapp.com/attachments/218222973557932032/240223136447070208/FortuneCookieNF.ttf"]
     option = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0)'
               ' Gecko/20100101 Firefox/40.1'}
     if os.path.exists("data/horoscope/cookie.png"):
         async with aiohttp.request("GET", urls[0], headers=option) as resp:
             test = await resp.read()
             meow = False
             with open("data/horoscope/cookie.png", "rb") as e:
                 if len(test) != len(e.read()):
                     meow = True
             if meow:
                 with open("data/horoscope/cookie.png", "wb") as f:
                     f.write(test)
     elif not os.path.exists("data/horoscope/cookie.png"):
         async with aiohttp.request("GET", urls[0], headers=option) as resp:
             test = await resp.read()
             with open("data/horoscope/cookie.png", "wb") as f:
                 f.write(test)
     if not os.path.exists("data/horoscope/FortuneCookieNF.ttf"):
         async with aiohttp.request("GET", urls[1], headers=option) as resp:
             test = await resp.read()
             with open("data/horoscope/FortuneCookieNF.ttf", "wb") as f:
                 f.write(test)
        def go():
            app, srv, app_handler = yield from self._setup_app(func)
            # make sure that exception page rendered
            resp = yield from aiohttp.request('GET', self.url, loop=self.loop)
            txt = yield from resp.text()
            self.assertEqual(500, resp.status)
            self.assertTrue('<div class="debugger">' in txt)

            # get request id from history
            history = app[APP_KEY]['request_history']
            request_id = history[0][0]

            url = '{}/_debugtoolbar/sse'.format(self.url)
            resp = yield from aiohttp.request('GET', url, loop=self.loop)
            data = yield from resp.text()
            data = data.strip()

            # split and check EventSource data
            event_id, event, payload_raw = data.split('\n')
            self.assertEqual(event_id, 'id: {}'.format(request_id))
            self.assertEqual(event, 'event: new_request')

            payload_json = payload_raw.strip('data: ')
            payload = json.loads(payload_json)
            expected = [[request_id, {"path": "/",
                                      "scheme": "http",
                                      "method": "GET",
                                      "status_code": 500},
                         ""]]

            self.assertEqual(payload, expected, payload)
            yield from app_handler.finish_connections(timeout=600)
            srv.close()
            yield from srv.wait_closed()
        def go(dirname, filename):
            app, _, url = yield from self.create_server(
                'GET', '/static/' + filename
            )
            app.router.add_static('/static', dirname)

            resp = yield from request('GET', url, loop=self.loop)
            self.assertEqual(200, resp.status)
            txt = yield from resp.text()
            self.assertEqual('file content', txt.rstrip())
            ct = resp.headers['CONTENT-TYPE']
            self.assertEqual('application/octet-stream', ct)
            self.assertEqual(resp.headers.get('CONTENT-ENCODING'), None)
            resp.close()

            resp = yield from request('GET', url + 'fake', loop=self.loop)
            self.assertEqual(404, resp.status)
            resp.close()

            resp = yield from request('GET', url + 'x' * 500, loop=self.loop)
            self.assertEqual(404, resp.status)
            resp.close()

            resp = yield from request('GET', url + '/../../', loop=self.loop)
            self.assertEqual(404, resp.status)
            resp.close()
def test_render_toolbar_page(loop, create_server):
    @asyncio.coroutine
    def handler(request):
        return aiohttp_jinja2.render_template(
            'tplt.html', request,
            {'head': 'HEAD', 'text': 'text'})
    app, url = yield from create_server()
    app.router.add_route('GET', '/', handler)

    # make sure that toolbar button present on apps page
    # add cookie to enforce performance panel measure time
    conn = aiohttp.TCPConnector(loop=loop, force_close=True)
    cookie = {"pdtb_active": "pDebugPerformancePanel"}
    resp = yield from aiohttp.request('GET', url+'/', cookies=cookie,
                                      loop=loop, connector=conn)
    assert 200 == resp.status
    txt = yield from resp.text()
    assert 'pDebugToolbarHandle' in txt
    resp.close()

    # make sure that debug toolbar page working
    url = "{}/_debugtoolbar".format(url)
    resp = yield from aiohttp.request('GET', url, loop=loop, connector=conn)
    yield from resp.text()
    assert 200 == resp.status
    resp.close()
    conn.close()
    def go():
        app, url = yield from create_server()
        app.router.add_route('GET', '/', handler)
        # make sure that exception page rendered
        resp = yield from aiohttp.request('GET', url, loop=loop)
        txt = yield from resp.text()
        assert 500 == resp.status
        assert '<div class="debugger">' in txt

        # get request id from history
        history = app[APP_KEY]['request_history']
        request_id = history[0][0]

        url = '{}/_debugtoolbar/sse'.format(url)
        resp = yield from aiohttp.request('GET', url, loop=loop)
        data = yield from resp.text()
        data = data.strip()

        # split and check EventSource data
        event_id, event, payload_raw = data.split('\n')
        assert event_id == 'id: {}'.format(request_id)
        assert event == 'event: new_request'

        payload_json = payload_raw.strip('data: ')
        payload = json.loads(payload_json)
        expected = [[request_id, {"path": "/",
                                  "scheme": "http",
                                  "method": "GET",
                                  "status_code": 500},
                     ""]]

        assert payload == expected, payload
Example #17
0
    def test_create(self):
        data = dict(
            name='New Squad'
        )
        resp = yield from request(
            'POST', self.full_url('/api/squad'), loop=self.loop,
            data=data
        )

        self.assertEqual(resp.status, 201)
        resp.close()

        resp = yield from request(
            'GET', self.full_url('/api/squad'), loop=self.loop,
        )

        self.assertEqual(resp.status, 200)
        jresp = yield from resp.json()

        self.assertIn('index', jresp)
        self.assertEqual(list, type(jresp['index']))
        self.assertGreater(len(jresp['index']), 0)

        for element in jresp['index']:
            self.assertEquals({'id', 'name', 'url'}, set(element.keys()))

        resp.close()
Example #18
0
def test_get_requests_remove_key(api_server, redis):
    url = urljoin(api_server, '/callbacks/app/queue/')
    url_all = urljoin(api_server, '/callbacks/_all/app/queue/')
    yield from aiohttp.request('POST', url, data=json_payload)
    yield from aiohttp.request('GET', url_all)

    assert not redis.exists('app-queue')
Example #19
0
 def _login(self):
     """
     Logs in user and gets authkey from server
     """
     login_page = '{0}/login.php'.format(WHAT_CD_ROOT)
     data = {
         'username': self.username,
         'password': self.password,
         'keeplogged': 1,
         'login': '******',
     }
     self.connector.cookies = SimpleCookie()
     r = yield from aiohttp.request('get', login_page, connector=self.connector)
     yield from r.text()
     r = yield from aiohttp.request('post', login_page, data=data, headers=HEADERS,
                                    allow_redirects=False, connector=self.connector)
     if r.status != 302:
         raise LoginException(r)
     account_info = yield from self.request("index", try_login=False)
     self.authkey = account_info["response"]["authkey"]
     self.passkey = account_info["response"]["passkey"]
     login_cache = LoginCache(
         cookies=base64.b64encode(pickle.dumps(list(self.connector.cookies.items()))),
         authkey=self.authkey,
         passkey=self.passkey
     )
     LoginCache.set(login_cache)
Example #20
0
            def query():
                resp = yield from aiohttp.request('GET', url + '/check_origin',
                                                  headers={},
                                                  loop=self.loop)
                yield from resp.read()
                self.assertEqual(resp.status, 200)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-METHOD', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-HEADERS', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-CREDENTIALS',
                                 resp.headers)

                headers = {
                    'ORIGIN': 'localhost',
                    }
                resp = yield from aiohttp.request('GET', url + '/check_origin',
                                                  headers=headers,
                                                  loop=self.loop)
                yield from resp.read()
                self.assertEqual(resp.status, 200)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-METHOD', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-HEADERS', resp.headers)
                self.assertNotIn('ACCESS-CONTROL-ALLOW-CREDENTIALS',
                                 resp.headers)

                headers = {
                    'ORIGIN': 'http://example.com/',
                    }
                resp = yield from aiohttp.request('GET', url + '/check_origin',
                                                  headers=headers,
                                                  loop=self.loop)
                yield from resp.read()
                self.assertEqual(resp.status, 200)
                self.assertIn('ACCESS-CONTROL-ALLOW-ORIGIN', resp.headers)
Example #21
0
    def test_view(self):

        # Create some records in the database in case the table is empty
        for i in range(5):
            data = dict(name="Other Zone {}".format(i))

            resp = yield from request("POST", self.full_url("/api/zone/"), loop=self.loop, data=data)

            self.assertEqual(resp.status, 201)
            resp.close()

        resp = yield from request("GET", self.full_url("/api/zone/"), loop=self.loop)

        self.assertEqual(resp.status, 200)
        jresp = yield from resp.json()

        for element in jresp["objects"]:
            url = element["url"]
            eresp = yield from request("GET", self.full_url(url), loop=self.loop)
            self.assertEqual(eresp.status, 200)
            ejresp = yield from eresp.json()

            self.assertEquals({"id", "meta", "name", "url"}, set(ejresp.keys()))

            for k, v in element.items():
                self.assertEqual(v, ejresp[k])

            eresp.close()

        resp.close()
Example #22
0
        def go():
            nonlocal auth_err

            app, _, url = yield from self.create_server('POST', '/')
            app.router.add_route(
                'POST', '/', handler, expect_handler=expect_handler)

            form = FormData()
            form.add_field('name', b'123',
                           content_transfer_encoding='base64')

            resp = yield from request(
                'post', url, data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)

            self.assertEqual(200, resp.status)
            resp.close(force=True)

            auth_err = True
            resp = yield from request(
                'post', url, data=form,
                expect100=True,  # wait until server returns 100 continue
                loop=self.loop)
            self.assertEqual(403, resp.status)
            resp.close(force=True)
Example #23
0
            def queries():
                connector = aiohttp.TCPConnector(share_cookies=True,
                                                 loop=self.loop)
                # initiate session; set start value to 2
                resp = yield from aiohttp.request('GET', url + "/2",
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 3})

                # do increment
                resp = yield from aiohttp.request('GET', url,
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 4})

                # try to override start value
                resp = yield from aiohttp.request('GET', url + '/3',
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 5})

                # session deleted; try count
                resp = yield from aiohttp.request('GET', url,
                                                  connector=connector,
                                                  loop=self.loop)
                data = yield from resp.json()
                self.assertEqual(resp.status, 200)
                self.assertEqual(data, {'result': 1})
Example #24
0
    def _request(self, method, url_or_endpoint, **kwargs):
        if not url_or_endpoint.startswith(('http://', 'https://')):
            api_base_url = kwargs.pop('api_base_url', self.API_BASE_URL)
            url = '{base}{endpoint}'.format(
                base=api_base_url,
                endpoint=url_or_endpoint
            )
        else:
            url = url_or_endpoint

        if 'params' not in kwargs:
            kwargs['params'] = {}
        if isinstance(kwargs['params'], dict) and \
                'access_token' not in kwargs['params']:
            kwargs['params']['access_token'] = self.access_token

        data = kwargs.get('data', {})
        files = kwargs.pop('files', {})
        if files:
            # data must be dict
            assert isinstance(data, dict), 'data must be a dict'
            data.update(files)
        else:
            if isinstance(data, dict):
                body = json.dumps(data, ensure_ascii=False)
            else:
                body = data
            kwargs['data'] = body

        result_processor = kwargs.pop('result_processor', None)
        timeout = kwargs.pop('timeout', self.timeout)
        try:
            if timeout is None:
                res = yield from aiohttp.request(
                    method=method,
                    url=url,
                    **kwargs
                )
            else:
                res_future = aiohttp.request(
                    method=method,
                    url=url,
                    **kwargs
                )
                res = yield from asyncio.wait_for(res_future, timeout)
                # reset kwargs for later retrying
                kwargs['timeout'] = timeout
                kwargs['files'] = files
        except ClientError:
            raise WeChatClientException(
                errcode=None,
                errmsg=None,
                client=self,
            )
        # dirty hack
        res = yield from self._decode_result(res)
        return self._handle_result(
            res, method, url, result_processor, **kwargs
        )
Example #25
0
def test_queue_request_store_request_different_queues(api_server, redis):
    url1 = urljoin(api_server, '/callbacks/app/queue1/')
    url2 = urljoin(api_server, '/callbacks/app/queue2/')
    yield from aiohttp.request('POST', url1, data=json_payload)
    yield from aiohttp.request('POST', url2, data=json_payload)

    assert redis.llen('app-queue1') == 1
    assert redis.llen('app-queue2') == 1
Example #26
0
def weather(bot, city, day=None):
    dt = check_and_return_datetime(day)
    timestamp = dt.replace(tzinfo=timezone.utc).timestamp()

    delta_days = (dt - datetime.now()).days
    is_long_forecast = (delta_days > 5)
    if not is_long_forecast:
        response = yield from request('GET', URL_API, params={
            'q': city,
            'units': 'metric',
            'lang': 'pl',
            'APPID': bot.config.get('app_id'),
        })
    else:
        response = yield from request('GET', URL_API_LONG, params={
            'q': city,
            'units': 'metric',
            'lang': 'pl',
            'cnt': min(delta_days, 16),
            'APPID': bot.config.get('app_id'),
        })
    try:
        json_data = yield from response.json()
    finally:
        response.close()
    if json_data.get('cod') == 401:
        return
    data_list = json_data.get("list")
    if data_list is None:
        bot.reply('404 lol')
        return
    data = sorted(data_list, key=lambda x: abs(x['dt'] - timestamp))[0]
    try:
        weather = data['weather'][0]
    except IndexError:
        weather = {'description': '', 'icon': ''}

    if not is_long_forecast:
        temperature = data['main']['temp']
    else:
        if dt.hour < 6:
            period = 'night'
        elif dt.hour < 10:
            period = 'morn'
        elif dt.hour < 18:
            period = 'day'
        elif dt.hour < 22:
            period = 'eve'
        else:
            period = 'night'
        temperature = data['temp'][period]
    bot.reply('{city}: {temp} °C {icon} {desc}'.format(
        city=format.bold(city),
        temp=temperature,
        desc=weather['description'],
        icon=ICON_TO_UTF.get(weather['icon'], '')
    ))
Example #27
0
def test_clean_requests_multiple_requests(api_server, redis):
    url = urljoin(api_server, '/callbacks/app/queue/')
    yield from aiohttp.request('POST', url, data=json_payload)
    yield from aiohttp.request('PUT', url, data=json_payload)
    yield from aiohttp.request('PATCH', url, data=json_payload)
    yield from aiohttp.request('DELETE', url)

    url_clean = urljoin(api_server, '/callbacks/_clean/app/queue/')
    yield from aiohttp.request('GET', url_clean)
    assert not redis.exists('app-queue')
Example #28
0
def test_first_request_one_request(api_server):
    url = urljoin(api_server, '/callbacks/app/queue/')
    yield from aiohttp.request('POST', url, data=json_payload)

    url_first = urljoin(api_server, '/callbacks/_first/app/queue/')
    response = yield from aiohttp.request('GET', url_first)
    response_json = yield from response.json()

    assert response_json['request']['method'] == 'POST'
    assert response_json['request']['data'] == payload
Example #29
0
def randomness():
    url_pattern = "http://i.imgur.com/{}{}{}{}{}.jpg"
    url_symbols = string.digits + string.ascii_letters
    headers = {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.10 Safari/537.36'}
    imgur_url = url_pattern.format(*(random.choice(url_symbols) for _ in range(5)))
    r = yield from aiohttp.request('head', imgur_url)
    while r.status != 200 or "removed" in r.url:
        imgur_url = url_pattern.format(*(random.choice(url_symbols) for _ in range(5)))
        r = yield from aiohttp.request('head', imgur_url)
    return imgur_url
Example #30
0
    def append_file(self, path, file_data, **kwargs):
        """
        Appends to an existing file on HDFS

        :param path: the HDFS file path without a leading '/'
        :param file_data: data to append to existing file

        The function wraps the WebHDFS REST call:

        POST http://<HOST>:<PORT>/webhdfs/v1/<PATH>?op=APPEND

        [&buffersize=<INT>]

        The function accepts all WebHDFS optional arguments shown above

        Example:

        >>> hdfs = Client(host='host',port='50070', user_name='hdfs')
        >>> my_data = '01010101010101010101010101010101'
        >>> my_file = 'user/hdfs/data/myfile.txt'
        >>> asyncio.get_event_loop().run_until_complete(hdfs.append_file(my_file, my_data))

        Example with optional args:

        >>> asyncio.get_event_loop().run_until_complete(hdfs.append_file(my_file, my_data, overwrite=True, buffersize=4096))

        Note: The append_file function does not follow automatic redirects but
        instead uses a two step call to the API as required in the
        WebHDFS documentation

        Append is not supported in Hadoop 1.x
        """

        # make the initial APPEND call to the HDFS namenode
        optional_args = kwargs
        uri = self._create_uri(path, operations.APPEND, **optional_args)
        init_response =  yield from request('post', uri, allow_redirects=False)

        if not init_response.status == http_client.TEMPORARY_REDIRECT:
            msg=yield from init_response.content.read()
            _raise_aiohdfs_exception(
                init_response.status,msg)

        # Get the address provided in the location header of the
        # initial response from the namenode and make the APPEND request
        # to the datanode
        uri = init_response.headers['location']
        response =  yield from request('post',  uri, data=file_data,
                   headers={'content-type': 'application/octet-stream'})

        if not response.status == http_client.OK:
            msg=yield from response.content.read()
            _raise_aiohdfs_exception(response.status, msg)

        return True
Example #31
0
 async def fetch(self):
     with async_timeout.timeout(6.5):
         async with aiohttp.request('GET', self.image_link) as r:
             return await r.read()
Example #32
0
def start_client(loop, url):
    name = input('Please enter your name: ').encode()

    sec_key = base64.b64encode(os.urandom(16))

    # send request
    response = yield from aiohttp.request('get',
                                          url,
                                          headers={
                                              'UPGRADE':
                                              'WebSocket',
                                              'CONNECTION':
                                              'Upgrade',
                                              'SEC-WEBSOCKET-VERSION':
                                              '13',
                                              'SEC-WEBSOCKET-KEY':
                                              sec_key.decode(),
                                          },
                                          timeout=1.0)

    # websocket handshake
    if response.status != 101:
        raise ValueError("Handshake error: Invalid response status")
    if response.get('upgrade', '').lower() != 'websocket':
        raise ValueError("Handshake error - Invalid upgrade header")
    if response.get('connection', '').lower() != 'upgrade':
        raise ValueError("Handshake error - Invalid connection header")

    key = response.get('sec-websocket-accept', '').encode()
    match = base64.b64encode(hashlib.sha1(sec_key + WS_KEY).digest())
    if key != match:
        raise ValueError("Handshake error - Invalid challenge response")

    # switch to websocket protocol
    stream = response.stream.set_parser(websocket.WebSocketParser)
    writer = websocket.WebSocketWriter(response.transport)

    # input reader
    def stdin_callback():
        line = sys.stdin.buffer.readline()
        if not line:
            loop.stop()
        else:
            writer.send(name + b': ' + line)

    loop.add_reader(sys.stdin.fileno(), stdin_callback)

    @asyncio.coroutine
    def dispatch():
        while True:
            try:
                msg = yield from stream.read()
            except aiohttp.EofStream:
                # server disconnected
                break

            if msg.tp == websocket.MSG_PING:
                writer.pong()
            elif msg.tp == websocket.MSG_TEXT:
                print(msg.data.strip())
            elif msg.tp == websocket.MSG_CLOSE:
                break

    yield from dispatch()
Example #33
0
    def request(self, method, url, **kwargs):
        method = method.upper()

        # Join parameters provided in the URL
        # and the ones passed as argument.
        params = kwargs.get('params')
        if params:
            url = _combine_query_params(url, params)

        # Convert headers dictionary to
        # twisted raw headers format.
        headers = kwargs.get('headers')
        if headers:
            _headers = []
            for key, val in headers.items():
                if isinstance(val, list):
                    for v in val:
                        _headers.append((key, v))
                else:
                    _headers.append((key, val))
            headers = _headers
        else:
            headers = {}

        # Here we choose a right producer
        # based on the parameters passed in.
        data = kwargs.get('data')
        files = kwargs.get('files')
        if files:
            # If the files keyword is present we will issue a
            # multipart/form-data request as it suits better for cases
            # with files and/or large objects.

            # TODO: Must check multipart aiohttp support
            files = list(_convert_files(files))
            boundary = uuid.uuid4()
            headers['Content-Type'] = [
                'multipart/form-data; boundary=%s' % (boundary, )
            ]
            if data:
                data = _convert_params(data)
            else:
                data = []
            data += files
        elif data:
            # Otherwise stick to x-www-form-urlencoded format
            # as it's generally faster for smaller requests.
            if isinstance(data, (dict, list, tuple)):
                headers['Content-Type'] = 'application/x-www-form-urlencoded'
                data = urlencode(data, doseq=True)

        cookies = kwargs.get('cookies', {})

        if not isinstance(cookies, CookieJar):
            cookies = cookiejar_from_dict(cookies)

        cookies = merge_cookies(self._cookiejar, cookies)
        allow_redirects = kwargs.get('allow_redirects', True)

        auth = kwargs.get('auth')
        if auth:
            auth = aiohttp.helpers.BasicAuth(*auth)
        else:
            auth = None

        if isinstance(headers, dict):
            headers['accept-encoding'] = 'gzip'
        else:
            headers.append(('accept-encoding', 'gzip'))
        loop = asyncio.get_event_loop()
        timeout = kwargs.get('timeout')

        request_args = {
            'auth': auth,
            'allow_redirects':
            allow_redirects if not allow_redirects else None,
            'headers': headers,
            'data': data,
            'cookies': cookies if cookies else None
        }

        for k in list(request_args.keys()):
            if not request_args[k]:
                request_args.pop(k)

        resp = yield from asyncio.wait_for(
            loop.create_task(aiohttp.request(method, url, **request_args)),
            timeout)

        return _Response(resp, cookies)
Example #34
0
async def request_data(url):
    # use aiohttp.request (as a context manager) to get data from url
    # then return data as str
    async with aiohttp.request('GET', url) as response:
        return await response.text()
Example #35
0
async def areq(u):
    print(f'a req {u}')
    await asyncio.sleep(randint(1, 2))
    async with aiohttp.request('GET', u) as resp:
        print(f'a res {u} - {resp.status}')
async def fetch_url(url):
    """
    Асинзронный HTTP-клиент для выполнения запросов к серверу.
    """
    async with aiohttp.request('get', url) as request:
        return url, await request.text()
Example #37
0
 def _get(url=''):
     return aiohttp.request('GET', 'https://coveralls.io{}'.format(url))
Example #38
0
def get(*args, **kwargs):
    response = yield from aiohttp.request('GET', *args, **kwargs)
    return (yield from response.text())
Example #39
0
def get(*args, **kwargs):
    response = yield from aiohttp.request('GET', *args, **kwargs)
    return response.headers
Example #40
0
 def go():
     _, srv, url = yield from self.create_server('GET', '/', handler)
     resp = yield from request('GET', url, loop=self.loop)
     self.assertEqual(200, resp.status)
     txt = yield from resp.text()
     self.assertEqual('OK', txt)
Example #41
0
 def go():
     _, srv, url = yield from self.create_server('GET', '/', handler)
     resp = yield from request('GET', url + '?arg=', loop=self.loop)
     self.assertEqual(200, resp.status)
     yield from resp.release()
Example #42
0
 async def post_issue(self, message, prefix, number):
     api = 'https://api.github.com/repos/{}/issues/{}'.format(self.settings[message.server.id][prefix]['gh'], number)
     fields = self.settings[message.server.id][prefix]['fields']
     async with aiohttp.request("GET", api, headers={'Accept': 'application/vnd.github.black-cat-preview+json'}) as r:
         # Check is the issue exists
         if r.status == 404:
             return False
         result = await r.json()
         # Check if the issue is a PR
         if 'pull_request' in result:
             issue_type = "pr"
             pr_api = 'https://api.github.com/repos/{}/pulls/{}'.format(self.settings[message.server.id][prefix]['gh'], number)
             async with aiohttp.get(pr_api, headers={'Accept': 'application/vnd.github.black-cat-preview+json'}) as r:
                 pr_result = await r.json()
         else:
             issue_type = "issue"
         # Check if the issue is open, closed or merged.
         if result['state'] == 'open':
             colour = self.colour['open']
         elif issue_type == 'pr' and pr_result['merged'] is True:
             colour = self.colour['merged']
         else:
             colour = self.colour['closed']
         # Check for title and description
         if fields['description'] is True:
             description = result['body']
             embed_description = (description[:175] + '...') if len(description) > 175 else description
             embed = discord.Embed(title='{} #{}'.format(result['title'], result['number']), description=embed_description, url=result['html_url'], colour=colour)
         else:
             embed = discord.Embed(title='{} #{}'.format(result['title'], result['number'], url=result['html_url'], colour=colour))
         if fields['author'] is True:
             embed.set_author(name=result['user']['login'], icon_url=result['user']['avatar_url'], url=result['user']['html_url'])
         # Check for assigned users
         if fields['assigned'] is True and len(result['assignees']) != 0:
             desc = ''
             for assigned in result['assignees']:
                 desc = desc + ('[{}]({})\n'.format(assigned['login'], assigned['html_url']))
             embed.add_field(name='Assigned', value=desc)
         # Check for Created at, Closed at and Closed By
         if fields['createdat'] is True or fields['closedby'] is True:
             desc = ''
             if fields['closedby'] is True and result['state'] == 'closed':
                 closed_user_avatar = result['closed_by']['avatar_url']
                 closed_at = datetime.strptime(result['closed_at'], '%Y-%m-%dT%H:%M:%SZ')
                 closed_at = closed_at.strftime('%-d %b %Y, %-H:%M')
                 desc = desc + 'Closed by {} on {}'.format(result['closed_by']['login'], closed_at)
             if fields['createdat'] is True:
                 created_at = datetime.strptime(result['created_at'], '%Y-%m-%dT%H:%M:%SZ')
                 created_at = created_at.strftime('%-d %b %Y, %-H:%M')
                 if result['state'] == 'closed' and fields['closedby'] is True:
                     desc = desc + ' | Created on {}'.format(created_at)
                 else:
                     desc = desc + 'Created on {}'.format(created_at)
             if result['state'] == 'closed' and fields['closedby'] is True:
                 embed.set_footer(icon_url=closed_user_avatar, text=desc)
             else:
                 embed.set_footer(text=desc)
         # Check for Labels
         if fields['labels'] is True and len(result['labels']) != 0:
             label_list = []
             for label in result['labels']:
                 label_list.append('{}'.format(label['name']))
             embed.add_field(name='Labels [{}]'.format(len(result['labels'])), value=', '.join(label_list))
         # Check for locked Issues
         if fields['locked'] is True:
             if result['locked'] is True:
                 embed.add_field(name='Locked', value='Yes')
             else:
                 embed.add_field(name='Locked', value='No')
         # Check for Merge Status
         if fields['mergestatus'] is True and issue_type == 'pr':
             if pr_result['merged'] is True:
                 merge_status = 'Merged'
             elif pr_result['mergeable_state'] == 'dirty':
                 merge_status = 'Conflicting'
             else:
                 merge_status = 'Not Merged'
             embed.add_field(name='Merge Status', value=merge_status)
         # Milestones: TODO lololololol
         # Check for Reviews
         if fields['reviews'] is True and issue_type == 'pr':
             # Need to make another connection *ugh*. Goodbye quick loading times.
             review_api = 'https://api.github.com/repos/{}/pulls/{}/reviews'.format(self.settings[message.server.id][prefix]['gh'], number)
             async with aiohttp.get(review_api, headers={'Accept': 'application/vnd.github.black-cat-preview+json'}) as r:
                 review_result = await r.json()
                 review_list = []
                 for review in review_result:
                     if review['state'] == 'APPROVED':
                         review_list.append([review['user']['login'], 'Approved'])
                     elif review['state'] == 'CHANGES_REQUESTED':
                         review_list.append([review['user']['login'], 'Requested Changes'])
                 if len(review_list) > 0:
                     desc = ''
                     for user in review_list:
                         desc = desc + '{}: {}'.format(*user)
                     embed.add_field(name='Reviews', value=desc)
         await self.bot.send_message(message.channel, embed=embed)
Example #43
0
    async def weatherCode(self, ctx, loc, is_cond, is_hr, is_day):
        # Remove any characters not in ranges a-z, A-Z, or 0-9
        # Exceptions: & _ - , and <space>
        # per the ASCII Table https://www.asciitable.com
        loc = re.sub("[^a-zA-Z0-9 ,&_-]+", "", loc)

        # Geocoding URL
        url_Geo_API = f'{URL_GEO}{loc}'

        self.geocode_api_key = BotSecrets.get_instance().geocode_key
        self.weather_api_key = BotSecrets.get_instance().weather_key

        geo_queryparams = {
            'auth': self.geocode_api_key,
            'json': '1',
        }

        # Message to Display while APIs are called
        wait_msg = await ctx.send('Converting location')

        # Try Except for catching errors that could give away either API key
        try:
            async with aiohttp.request("GET",
                                       url_Geo_API,
                                       params=geo_queryparams) as response:
                if (response.status != 200):
                    embed = discord.Embed(title='OpenWeatherMap Weather',
                                          color=Colors.Error)
                    ErrMsg = f'Error Code: {response.status}'
                    embed.add_field(name='Error with geocode API',
                                    value=ErrMsg,
                                    inline=False)
                    await ctx.send(embed=embed)
                    return
                res_geo_json = await response.json()
        except Exception as err:
            err_str = str(err)
            err_str = re.sub(self.geocode_api_key, "CLASSIFIED", err_str)
            err_str = re.sub(self.weather_api_key, "CLASSIFIED", err_str)
            raise Exception(err_str).with_traceback(err.__traceback__)

        city = res_geo_json.get('standard', {}).get('city', {})
        lon = res_geo_json.get('longt', {})
        lat = res_geo_json.get('latt', {})

        queryparams = {
            'lat': lat,
            'lon': lon,
            'appid': self.weather_api_key,
            'units': 'imperial',
            'lang': 'en'
        }

        weatherPages = []
        await wait_msg.edit(content='Checking the weather')

        try:
            async with aiohttp.request("GET", URL_WEATHER,
                                       params=queryparams) as response:
                if (response.status != 200):
                    embed = discord.Embed(title='OpenWeatherMap Weather',
                                          color=Colors.Error)
                    ErrMsg = f'Error Code: {response.status}'
                    embed.add_field(name='Error with weather API',
                                    value=ErrMsg,
                                    inline=False)
                    await ctx.send(embed=embed)
                    return
                res_weather_json = await response.json()
        except Exception as err:
            err_str = str(err)
            err_str = re.sub(self.geocode_api_key, "CLASSIFIED", err_str)
            err_str = re.sub(self.weather_api_key, "CLASSIFIED", err_str)
            raise Exception(err_str).with_traceback(err.__traceback__)

        weatherPages, num_hr, num_day = self.getPageData(
            lat, lon, res_weather_json, city, is_cond, is_hr, is_day)

        # Construct Title Message
        msg_title = ''
        if is_cond:
            msg_title += f'Current Conditions'
        if is_cond and (is_hr or is_day):
            msg_title += ' with '

        if is_hr and is_day:
            msg_title += 'Forecast'
        elif is_hr:
            msg_title += f'{num_hr}-Hour Forecast'
        elif is_day:
            msg_title += f'{num_day}-Day Forecast'

        await wait_msg.delete()
        await self.bot.messenger.publish(Events.on_set_pageable_text,
                                         embed_name='OpenWeatherMap Weather',
                                         field_title=msg_title,
                                         pages=weatherPages,
                                         author=ctx.author,
                                         channel=ctx.channel)
        """ 
Example #44
0
async def fetch(url, headers):
    async with request("GET", url=url, headers=headers) as response:
        return await response.text()
Example #45
0
async def url_media_type(url):
    async with aiohttp.request("HEAD", url) as response:
        return response.content_type
Example #46
0
 def go():
     _, _, url = yield from self.create_server('GET', '/', handler)
     resp = yield from request('GET', url, loop=self.loop)
     self.assertEqual(200, resp.status)
Example #47
0
async def load(word, trans):
    url = 'https://dictionary.cambridge.org/dictionary/' + trans + '/' + word
    async with aiohttp.request('GET', url) as resp:
        html = await resp.text()
        return html
async def command_whois(msg: twitchirc.ChannelMessage):
    if not bots or bots_invalidates <= time.time():
        await _load_bots()

    cd_state = main.do_cooldown('whois',
                                msg,
                                global_cooldown=30,
                                local_cooldown=60)
    if cd_state:
        return
    try:
        argv = shlex.split(msg.text.replace('\U000e0000', ''))
    except ValueError as e:
        return f'@{msg.user} FeelsWeirdMan {e.args}'

    args = whois_parser.parse_args(argv[1:] if len(argv) > 1 else [])
    if args is None:
        return f'@{msg.user} {whois_parser.format_usage()}'
    if args.id is not None:
        name = args.id
        id_ = True
    elif args.name is not None:
        name = args.name
        id_ = False
    else:
        return f'@{msg.user} Do you really want the bot to crash?'

    async with aiohttp.request(
            'get',
            f'https://api.ivr.fi/twitch/resolve/{name}',
            params=({
                'id': 1
            }) if id_ else {},
            headers={
                'User-Agent':
                'Mm\'sUtilityBot/v1.0 (by Mm2PL), Twitch chat bot',
                'Requested-By': msg.user
            }) as request:
        data = await request.json()
        if data['status'] == 404:
            return f'@{msg.user} No such user found.'

        roles = ''
        if data['roles']['isAffiliate']:
            roles += 'affiliate, '
        if data['roles']['isPartner']:
            roles += 'partner, '
        if data['roles']['isSiteAdmin']:
            roles += 'site admin, '
        if data['roles']['isStaff']:
            roles += 'staff, '
        if roles == '':
            roles = 'none'
        roles = (roles[::-1].replace(', '[::-1], '', 1))[::-1]
        # replace last ", " with "".
        if data['displayName'].lower() != data['login'].lower():
            login = f'({data["login"]})'
        else:
            login = ''
        created_on = datetime.datetime.strptime(data['createdAt'][:-8],
                                                '%Y-%m-%dT%H:%M:%S')
        bot_notes = ''
        bot = _find_bot(data['login'])
        if bot is not None:
            last_active = f', Last active: {bot["lastSeen"][:-4]}' if bot[
                'lastSeen'] is not None else ''
            bot_notes = (
                f' Prefix: {bot["prefix"] if bot["prefix"] is not None else "<blank>"}, '
                f'Description: {bot["description"] if bot["description"] is not None else "<blank>"}, '
                f'Language: {bot["language"] if bot["language"] is not None else "<unknown>"}'
                f'{last_active}')

        return (
            f'@{msg.user}, {"BANNED " if data["banned"] else ""}{"bot " if data["bot"] else ""}'
            f'user {data["displayName"]}{login}, '
            f'chat color: {data["chatColor"]}, '
            f'account created at {created_on}, '
            f'roles: {roles}, '
            f'id: {data["id"]}, '
            f'bio: '
            f'{data["bio"] if data["bio"] is not None else "<blank>"}'
            f'{bot_notes}')
Example #49
0
 def _get(url=''):
     return aiohttp.request(
         'GET', 'https://codecov.io/api/gh/{}{}?access_token={}'.format(
             secrets.CODECOV_REPO, url, secrets.CODECOV_ACCESS_TOKEN))
Example #50
0
 async def fetch_async(pid, url, query):
     logging.info('Fetch async process {} started'.format(pid))
     async with aiohttp.request('POST', url, json=query) as resp:
         response = await resp.text()
     logging.info('Fetch async process {} ended'.format(pid))
     return json.loads(response)
Example #51
0
async def mqwebsocket_handler(request):
    qws = web.WebSocketResponse()
    await qws.prepare(request)
    Authorization = b64encode(
        b'%s:%s' % ('wander'.encode(), 'Elements123'.encode())).decode()
    headers = {'Authorization': 'Basic %s' % Authorization}
    async for msg in qws:
        if msg.type == aiohttp.WSMsgType.TEXT:
            if msg.data == 'close':
                await qws.close()
            else:
                params = json.loads(msg.data)
                page = params.get('page')
                async with aiohttp.request(
                        method='get',
                        url=
                        'http://127.0.0.1:15672/api/queues?page=%s&page_size=100&name=&use_regex=false'
                        % page,
                        headers=headers) as resp:
                    if resp.status == 200:
                        text = await resp.text()
                        result = json.loads(text)
                        item_count = result.get('item_count')
                        items = result.get('items', [])
                        resp_list = []
                        for item in items:
                            mq_dict = {}
                            mq_dict['name'] = item.get('name')
                            features = ''
                            if item.get('durable'):
                                features += 'D'
                            if item.get('arguments').get('x-max-priority'):
                                features += '|Pri'
                            mq_dict['features'] = features
                            mq_dict['state'] = item.get('state')
                            mq_dict['ready'] = item.get('messages_ready')
                            mq_dict['unacked'] = item.get(
                                'messages_unacknowledged')
                            mq_dict['total'] = item.get('messages')
                            mq_dict['incoming'] = (
                                '%s/s' if item.get('message_stats', {}).get(
                                    'publish_details', {}).get('rate', 0) else
                                '%s') % item.get('message_stats', {}).get(
                                    'publish_details', {}).get('rate', 0)
                            mq_dict['deliver'] = (
                                '%s/s' if item.get('message_stats', {}).get(
                                    'deliver_get_details', {}).get('rate', 0)
                                else '%s') % item.get('message_stats', {}).get(
                                    'deliver_get_details', {}).get('rate', 0)
                            mq_dict['ack'] = (
                                '%s/s' if item.get('message_stats', {}).get(
                                    'ack_details', {}).get('rate', 0) else
                                '%s') % item.get('message_stats', {}).get(
                                    'ack_details', {}).get('rate', 0)
                            resp_list.append(mq_dict)
                        await qws.send_json({
                            "items":
                            resp_list,
                            "totalPage":
                            math.ceil(item_count / 100)
                        })

        elif msg.type == aiohttp.WSMsgType.ERROR:
            print('ws connection closed with exception %s' % qws.exception())

    print('websocket connection closed')

    return qws
Example #52
0
async def fetch_async(a):
    async with aiohttp.request('GET', URL.format(a)) as r:
        data = await r.json()

    return data['args']['a']
Example #53
0
def get_flag(cc):
	url = '{}/{cc}/{cc}.gif'.format(BASE_URL, cc=cc.lower())
	resp = yield from aiohttp.request('GET', url)
	image = yield from resp.read()
	return image
Example #54
0
async def generate_thumbnail(url):
    async with aiohttp.request('GET', url) as response:
        raw_image = await response.read()
    images = resize(raw_image)
    Image(url=url, **images).save()
Example #55
0
async def fetch(url):
    async with aiohttp.request("GET", url) as r:
        #_LOGGER.error(r.status)
        reponse = await r.text(encoding="utf-8")
Example #56
0
async def fetch(url):
    async with aiohttp.request('GET', url) as resp:  # resp是返回的json文本
        json = await resp.json()
        print(json)
Example #57
0
    async def meme(self, ctx, pic, *, msg):
        """
            {command_prefix}meme [User] [top_text]|[bottom_text]
            {command_prefix}meme [custom_url] [top_text]|[bottom_text]

        {help}
        """
        user = None
        #  msg = unidecode.unidecode(msg)
        if len(ctx.message.mentions) > (2 if self.bot.user.mention
                                        in ctx.prefix else 1):
            return await ctx.send(get_str(ctx, "cmd-meme-one-user"))
        if ctx.message.mentions:
            # mention on a nicknamed user on mobile creates weird issue (no '!' where it should)
            user = ctx.message.mentions[-1]
            if str(user.id) not in pic:  # It was a prefix
                user = None
                if len(ctx.message.mentions) > 1:
                    user = ctx.message.mentions[0]
        if ctx.guild:
            if pic.isdigit():
                target = ctx.guild.get_member(int(pic))
                if target:
                    user = target

            check_str = [
                u for u in ctx.guild.members if u.name.lower() == pic.lower()
            ]
            if check_str:
                user = check_str[0]

        if user:
            pic = str(user.avatar_url)

        pic = pic.strip('<>')

        msg = " ".join(msg.split())  # remove useless spaces
        msg = msg.replace('\r', '').replace('\n', '').replace(
            "-", "--").replace("_", "__").replace("?", "~q").replace(
                "#", "~h").replace(" ", "_").replace("%", "~p").replace(
                    "/", "~s").replace('"', "''")
        try:
            part1 = msg.split("|")[0]
        except IndexError:
            part1 = "_"
        try:
            part2 = msg.split("|")[1]
        except IndexError:
            part2 = "_"
        if part1 == "":
            part1 = "_"
        if part2 == "":
            part2 = "_"
        if not get_image_from_url(pic):
            return await ctx.send(
                get_str(ctx,
                        "command-invalid-usage").format("{}help meme".format(
                            get_server_prefixes(ctx.bot, ctx.guild))))
        if part2 != "_":
            total = "https://memegen.link/custom/{}/{}.jpg?alt={}".format(
                part1, part2, pic)
        else:
            total = "https://memegen.link/custom/{}/_.jpg?alt={}".format(
                part1, pic)
        embed = discord.Embed()
        embed.set_author(name=ctx.author.name, icon_url=ctx.author.avatar_url)

        # download file
        async with aiohttp.request("GET", total) as resp:
            img = BytesIO(await resp.read())

        f = discord.File(img, filename="image.png")
        embed.set_image(url="attachment://image.png")

        if not user:
            try:
                await ctx.message.delete()
            except discord.HTTPException:
                pass
        try:
            await ctx.send(file=f, embed=embed)
        except discord.Forbidden:
            await ctx.send(get_str(ctx, "need-embed-permission"))
Example #58
0
async def group_message_handler(app: GraiaMiraiApplication,
                                message: MessageChain, group: Group,
                                member: Member):

    url = ""
    b23_url = ""  # 先申请好两种要用的url
    if Msg_element.Xml in message:
        xml = message.get(Xml)
        xml_msg = etree.fromstring(
            message.get(Msg_element.Xml)[0].xml.encode('utf-8'))  #将xml进行解码
        url = xml_msg.xpath('/msg/@url')[0]  #这是xml中包含bv号的链接
        result = re.search(bvpattern, url)  #进行bv号的一个匹配
    else:
        result = re.search(bvpattern, message.asDisplay())  #说明是纯文本,直接找bv号

    if result != None:  #匹配到了的情况:说明是用电脑端的链接分享的
        BVname = result.group()
        print(BVname)
        videoInformation = bvcrawler(BVname)
        await app.sendGroupMessage(
            group,
            MessageChain.create([
                Image.fromNetworkAddress(videoInformation['cover_url']),
                Plain(videoInformation['information'])
            ]))
    else:  #没有匹配到bv号然而又是xml,说明这不是电脑端分享的链接,而是iphone分享的链接,这里的url是b23短链接
        if url != "":  #因为url初值是空,所以这里做一个判断避免每一次输入一个信息就进行查找
            b23_url = url
            resp = requests.get(
                b23_url, allow_redirects=False)  #向b23短链接发送请求,然后阻止其进行重定向到网站上去
            redirect_url = resp.headers.get('Location')  #得到重定向后的url
            result = re.search(bvpattern, redirect_url)  #得到重定向后的bv号
            if result != None:
                BVname = result.group()
                print(BVname)
                videoInformation = bvcrawler(BVname)
                await app.sendGroupMessage(
                    group,
                    MessageChain.create([
                        Image.fromNetworkAddress(
                            videoInformation['cover_url']),
                        Plain(videoInformation['information'])
                    ]))

    if Msg_element.App in message:  #说明是用手机分享的,是一个json格式的消息,我们可以从中解码得到b23短链接
        json_msg = json.loads(message.get(
            Msg_element.App)[0].content)  #这里的json格式要这样解码
        name = json_msg['desc']
        if name == "哔哩哔哩":  #如果这里的name叫哔哩哔哩,那说明是手机客户端分享的小程序
            b23_url = json_msg['meta']['detail_1']['qqdocurl']  #b23_url此时在这里
        else:  #这里的name不是叫哔哩哔哩了,准确来说是为空,那说明是ipad的HD客户端分享的小程序
            b23_url = json_msg['meta']['news']['jumpUrl']  #b23_url此时又是在这里
        resp = requests.get(b23_url,
                            allow_redirects=False)  #和上面一样的http302拦截,然后得到bv号
        redirect_url = resp.headers.get('Location')
        result = re.search(bvpattern, redirect_url)
        if result != None:
            BVname = result.group()
            print(BVname)
            videoInformation = bvcrawler(BVname)
            await app.sendGroupMessage(
                group,
                MessageChain.create([
                    Image.fromNetworkAddress(videoInformation['cover_url']),
                    Plain(videoInformation['information'])
                ]))

    if message.has(At):
        flag = 0
        for at in message.get(At):
            if at.target == 5980403:
                flag = 1
        if flag == 0:
            return
        else:
            msg = message.asSerializationString()
            message_a = MessageChain.create([
                Plain("消息监听:\n%s(%d)在群%s(%d)中对我说:\n%s" %
                      (member.name, member.id, group.name, group.id,
                       message.asDisplay()))
            ])
            message_b = message.asSendable()
            message_a.plus(message_b)
            for i in range(0, len(message_a.__root__)):
                if message_a.__root__[i].type == 'At':
                    message_a.__root__[i] = Plain(
                        message_a.__root__[i].display)

            await app.sendFriendMessage(5980403, message_a)

    if message.asDisplay() == "help":
        sstr = "目前已经公开的功能有:" + "\n\n"
        sstr += "①打招呼功能,输入hi说不定可以得到妹妹的回应哦~" + "\n\n"
        sstr += "②查bv号和av号的功能,并且能够解析任何形式分享的b站视频,能够显示视频的详细信息~" + "\n\n"
        sstr += "③随机提供涩图的功能,输入‘色图时间’或者‘来点涩图’就可以随机发送一张图片了~" + "\n\n"
        sstr += "④整点报时功能~\n\n"
        sstr += "⑤提供b站车万区周榜功能~\n\n"
        sstr += "⑥碧蓝航线实时推送功能,并且输入'碧蓝航线最新动态'可以得到碧蓝航线官方账号发送的最新动态哦~\n\n"
        sstr += "⑦点歌功能。输入【点歌 xxx】就可以查找到你喜欢的歌曲哦~\n"
        sstr += "凛夜sama赛高!(不要忘了所有的功能都是凛夜亲手敲的代码哦)"
        await app.sendGroupMessage(group, MessageChain.create([Plain(sstr)]))

    if message.asDisplay() == "hi":
        if (member.id == 5980403):
            await app.sendGroupMessage(
                group, MessageChain.create([At(5980403),
                                            Plain(" 哥哥爱死你了mua")]))
        elif (member.id == 349468958):
            await app.sendGroupMessage(
                group, MessageChain.create([Plain("哥哥我也爱你呢❤")]))
        elif (member.id == 865734287):
            await app.sendGroupMessage(
                group,
                MessageChain.create([Plain("mu..(害怕)mua?"),
                                     Face(faceId=111)]))
        elif (member.id == 744938425):
            await app.sendGroupMessage(
                group,
                MessageChain.create([
                    At(744938425),
                    Plain(" 欧尼酱要吃饭呢,要洗澡呢,还是要先吃我呢"),
                    Face(faceId=111)
                ]))
        else:
            await app.sendGroupMessage(
                group, MessageChain.create([At(member.id),
                                            Plain("hi~")]))

    if message.asDisplay() == "晚安":
        if (member.id == 5980403):
            await app.sendGroupMessage(
                group,
                MessageChain.create(
                    [At(5980403), Plain(" 哥哥晚安"),
                     Face(faceId=75)]))
        else:
            await app.sendGroupMessage(
                group, MessageChain.create([At(member.id),
                                            Plain(" 晚安~")]))

    if message.asDisplay() == "草" or message.asDisplay() == "艹":
        if random.random() <= 0.25:
            await app.sendGroupMessage(group,
                                       MessageChain.create([Plain("草")]))
        else:
            return

    if (member.id != 2083664136 and member.id != 2079373402):
        if message.asDisplay().startswith(
                "AV") or message.asDisplay().startswith("av"):
            videoInformation = avcrawler(message.asDisplay())
            await app.sendGroupMessage(
                group,
                MessageChain.create([
                    Image.fromNetworkAddress(videoInformation['cover_url']),
                    Plain(videoInformation['information'])
                ]))

    if message.asDisplay() == "色图时间" or message.asDisplay(
    ) == "来点涩图" or message.asDisplay() == "来点色图":
        url = "https://api.nmb.show/1985acg.php"
        conn = aiohttp.TCPConnector(ssl=False)
        async with aiohttp.request('GET', url, connector=conn) as resp:
            content = await resp.read()
        try:
            await app.sendGroupMessage(
                group, MessageChain.create([Image.fromUnsafeBytes(content)]))
        except:
            await app.sendGroupMessage(
                group,
                MessageChain.create([Plain("该图片无法显示qwq"),
                                     Face(faceId=107)]))

    if message.asDisplay() == "来点辉夜" or message.asDisplay() == "辉夜图":
        kaguyaDir = "./Kaguya"
        kaguyaNames = []
        for parent, dirnames, filenames in os.walk(kaguyaDir):
            kaguyaNames = filenames
        x = random.randint(0, len(kaguyaNames) - 1)
        pictureLocation = kaguyaDir + "/" + kaguyaNames[x]
        await app.sendGroupMessage(
            group, MessageChain.create([Image.fromLocalFile(pictureLocation)]))

    if message.asDisplay() == "车万周榜" or message.asDisplay() == "东方周榜":
        msg = Touhou()
        await app.sendGroupMessage(group, MessageChain.create([Plain(msg)]))

    if message.asDisplay() == "维护" and member.id == 5980403:
        msg = "就算是机器人的妹妹我也要休息了呢qwq,凛夜哥哥要对我进行功能维护了,大家好好期待吧~"
        groups = [372733015, 766517688, 875626950, 862315052, 729801800]
        for group in groups:
            await app.sendGroupMessage(group,
                                       MessageChain.create([Plain(msg)]))

    if message.asDisplay() == "停止维护" and member.id == 5980403:
        msg = "凛夜哥哥对我的维护已经结束了,我又可以继续被大家正常使用了呢~(羞涩)"
        groups = [372733015, 766517688, 875626950, 862315052, 729801800]
        for group in groups:
            await app.sendGroupMessage(group,
                                       MessageChain.create([Plain(msg)]))

    if message.asDisplay() == "碧蓝航线最新动态":
        await blhx(app, group)

    if message.asDisplay() == "lex凉了没" or message.asDisplay() == "lex":
        lexurl = "https://api.bilibili.com/x/relation/stat?vmid=777536"
        headers = {
            'User-Agent':
            'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1',
        }
        msg = requests.get(lexurl, headers=headers).json()
        followers = msg['data']['follower']
        string = "lex的粉丝数已经掉到" + str(followers) + "了~"
        await app.sendGroupMessage(group, MessageChain.create([Plain(string)]))

    if member.id == 5980403 and message.asDisplay() == '贴吧签到':
        ua = Faker()
        headers = {
            'cookie':
            "BIDUPSID=9D96E01732C84E3EF46E6D69F715EB8E; PSTM=1574597643; bdshare_firstime=1574667391465; rpln_guide=1; H_WISE_SIDS=147935_162057_156287_159609_162914_155225_161299_163303_161266_162371_159382_159937_161421_157263_161419_161970_127969_161770_160102_161958_160897_161729_162347_131423_160861_128698_161082_153149_162445_158055_160800_162169_161965_159954_160422_162474_162151_144966_162095_162187_161239_139883_158640_155530_163114_147552_162479_162267_162524_162861_162816_162642_159092_162264_162261_162155_110085_162026_163321; BAIDUID=CA1D410F7713287242D266621C18831C:FG=1; __yjs_duid=1_2f71f9689f273d49d3b607ed4bead1ca1611406958065; BDORZ=B490B5EBF6F3CD402E515D22BCDA1598; H_PS_PSSID=33423_33582_33273_31253_26350_33544; delPer=0; PSINO=7; BAIDUID_BFESS=308E2AF32F2705030DB38E99B12C6328:FG=1; BDRCVFR[feWj1Vr5u3D]=mk3SLVN4HKm; BA_HECTOR=2kal01a58ga42h8gqt1g27n8q0r; st_key_id=17; Hm_lvt_98b9d8c2fd6608d564bf2ac2ae642948=1612899973,1612899985,1612963100,1612963106; ab_sr=1.0.0_OTNlZDA4ZTNjNWQzYzEyZTg3NmU3ZTU2ZTM0OTYzMzM2NWFhOTgwMThmNWU4N2Y5YWExNWExOTM2ZThmM2JmMTJlOTZmZTRhYzE2ODZiOGJjMTQ4MjEyNTJkZjY1OTZlODZiZjg2NDE4MWRiZDJmZmUxNWRmN2JiZTgzM2ZmZTA=; st_data=6ff647c25e22e6e2098ddd2b4d912445ecd2b7a96a113d85893a95c7106afea705096a5203902ba371dce271f377c6fe1cf78cee29958d81bc1b2eefaafff0eb919f7810870e1562e9e0da7fd55f383a36176d3d772d68e90ff7eb8e121e5085d76aa9b6314c23eebd55995d0777b5950d21b55485d174f84dafb08ea9375a31; st_sign=8f3d7169; baidu_broswer_setup_sargarse=0; Hm_lpvt_98b9d8c2fd6608d564bf2ac2ae642948=1612963136; BDUSS=0lQdzl0LUtwRGdvSmJILTVuaDRsRjJndG9VV25rMVFnVDA5M0JjV0JKaG9ha3RnRVFBQUFBJCQAAAAAAAAAAAEAAACuIkFKc2FyZ2Fyc2UAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGjdI2Bo3SNgZ; BDUSS_BFESS=0lQdzl0LUtwRGdvSmJILTVuaDRsRjJndG9VV25rMVFnVDA5M0JjV0JKaG9ha3RnRVFBQUFBJCQAAAAAAAAAAAEAAACuIkFKc2FyZ2Fyc2UAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGjdI2Bo3SNgZ; STOKEN=1475d1ef2d029f121173478668e6605d6dd6dbc639869b78c0e1318306d5f9af",
            'user-Agent': str(ua.user_agent),
            'content-Type': 'application/json'
        }
        url = 'https://tieba.baidu.com/tbmall/onekeySignin1'
        param = {'ie': 'utf-8', 'tbs': 'dbcb633d0a5796b81612963177'}
        a = requests.post(url, data=param, headers=headers)
        msg = MessageChain.create([
            Plain("签到成功的贴吧为" + str(a.json()['data']['signedForumAmount']) +
                  '个\n' + "签到失败的贴吧为" +
                  str(a.json()['data']['signedForumAmountFail']) + '个')
        ])
        await app.sendGroupMessage(group, msg)

    if message.asDisplay().startswith("点歌"):
        await song(app, inc, group, member, message.asDisplay())

    # if member.id == 5980403 and message.asDisplay().startswith("订阅直播 "):
    #     room_id = message.asDisplay().replace("订阅直播 ",'')
    #     global info

    #     Localpath = 'data/data.json'
    #     data = {}
    #     fr = open(Localpath,encoding = 'utf-8')
    #     data = json.load(fr)
    #     fr.close

    #     for i in data['data']:
    #         if room_id == str(i['room_id']):
    #             if group.id in i['group']:
    #                 await app.sendGroupMessage(group,MessageChain.create([Plain("这个直播已经在订阅列表中了哦~")]))
    #                 break

    #         else:
    #             try:
    #                 if not room_id in info:
    #                     info['room_id'] = asyncio.create_task(entrance(app,room_id))
    #                 info = get_info(room_id)
    #                 await app.sendGroupMessage(group, MessageChain.create([Plain("开启对%s(%d)的直播间订阅" % (info['user'], info['uid']))]))
    #                 livewrite(group.id, int(room_id))
    #             except:
    #                 await app.sendGroupMessage(group, MessageChain.create([Plain("开启直播订阅失败,请检查房间号")]))
    #                 del info['room_id']

    if message.asDisplay() == "直播help":
        help_str = "目前红群直播的开启功能已经基本上由凛夜哥哥写完了,下面是大概的使用方法~\n"
        help_str += "首先,直播开启的权限是由白名单决定的,白名单存储在服务器上,只能由红群和tfcc群的管理员以及凛夜本人进行添加。添加方法为:【白名单添加 qq号】。管理员必须要注意的是:添加完qq号以后,请马上输入【添加id 名字】来添加qq号成员对应的名字,不然白名单可能会无法正常运行!\n"
        help_str += "然后开启直播的方法是【开始直播】。只有白名单上的成员才能开启直播哦!如果你想查看白名单的成员,请输入【直播白名单】,或者是之前莉莉白的查看方式【!白名单】。这里凛夜哥哥是为了迎合大家之前的习惯做的~\n"
        help_str += "如果需要修改白名单,请发送【白名单删除 qq号】。请管理员注意,删除完白名单上的qq号以后请务必删除qq号对应的id!方法为【删除id 名字】!\n"
        help_str += "如果有人在开启直播,其他白名单上的成员还发送了【开始直播】,那么机器人会提醒你有人在使用直播~如果使用直播的人需要下线,请发送【关闭直播】。而且关闭直播的权限只有开启直播的本人有哦~\n"
        help_str += "在进行tfcc比赛时可能会需要随时更改直播标题,请发送【修改直播标题 目标名字】即可~"
        await app.sendGroupMessage(group,
                                   MessageChain.create([Plain(help_str)]))

    if message.asDisplay().startswith("白名单添加 "):
        memper = member.permission
        memper = str(memper)
        if (group.id == 182721157 or group.id == 1158449372
                or group.id == 431987102 or group.id
                == 909918392) and (memper == "MemberPerm.Owner"
                                   or memper == "MemberPerm.Administrator"
                                   or member.id == 5980403):
            identification = message.asDisplay().replace("白名单添加 ", '')
            with open("./data/authority.txt", 'r+') as autho:
                exist_IDs = autho.readlines()
                for i in range(len(exist_IDs)):
                    exist_IDs[i] = exist_IDs[i].replace('\n', '')
                    if identification == exist_IDs[i]:
                        await app.sendGroupMessage(
                            group,
                            MessageChain.create([Plain("白名单上已经有该成员了!")]))
                        return
                autho.write(identification)
                autho.write('\n')
                autho.close()
            await app.sendGroupMessage(
                group, MessageChain.create([Plain("白名单添加成功!")]))

    if message.asDisplay().startswith("添加id "):
        memper = member.permission
        memper = str(memper)
        if (group.id == 182721157 or group.id == 1158449372
                or group.id == 431987102 or group.id
                == 909918392) and (memper == "MemberPerm.Owner"
                                   or memper == "MemberPerm.Administrator"
                                   or member.id == 5980403):
            with open("./data/authoid.txt", 'a') as temp_name:
                temp_identification = message.asDisplay().replace("添加id ", '')
                print(temp_identification)
                temp_name.write(temp_identification)
                temp_name.write('\n')
                temp_name.close()
            await app.sendGroupMessage(
                group, MessageChain.create([Plain("白名单id添加成功!")]))

    if message.asDisplay().startswith("白名单删除 "):
        memper = member.permission
        memper = str(memper)
        if (group.id == 182721157 or group.id == 1158449372
                or group.id == 431987102 or group.id
                == 909918392) and (memper == "MemberPerm.Owner"
                                   or memper == "MemberPerm.Administrator"
                                   or member.id == 5980403):
            identificate = message.asDisplay().replace("白名单删除 ", '')
            target_IDs = ['0']
            with open("./data/authority.txt", 'r+', encoding='utf-8') as aut:
                target_IDs = aut.readlines()
                where_flag = 0
                for i in range(len(target_IDs)):
                    if identificate == target_IDs[i].replace('\n', ''):
                        target_IDs.pop(i)
                        where_flag = 1
                        break
                aut.close()
            if where_flag == 0:
                await app.sendGroupMessage(
                    group, MessageChain.create([Plain("白名单上没有该成员~")]))
            else:
                with open("./data/authority.txt", 'w',
                          encoding='utf-8') as real_aut:
                    real_aut_content = ""
                    for i in range(len(target_IDs)):
                        real_aut_content += target_IDs[i]
                    real_aut.write(real_aut_content)
                    real_aut.close()
                await app.sendGroupMessage(
                    group, MessageChain.create([Plain("删除成功!")]))

    if message.asDisplay().startswith("删除id "):
        memper = member.permission
        memper = str(memper)
        if (group.id == 182721157 or group.id == 1158449372
                or group.id == 431987102 or group.id
                == 909918392) and (memper == "MemberPerm.Owner"
                                   or memper == "MemberPerm.Administrator"
                                   or member.id == 5980403):
            identificate_name = message.asDisplay().replace("删除id ", '')
            target_name = ['0']
            with open("./data/authoid.txt", 'r+', encoding='utf-8') as nam:
                target_name = nam.readlines()
                which_flag = 0
                for i in range(len(target_name)):
                    if identificate_name == target_name[i].replace('\n', ''):
                        target_name.pop(i)
                        which_flag = 1
                        break
                nam.close()
            if which_flag == 0:
                await app.sendGroupMessage(
                    group, MessageChain.create([Plain("你删除了错误的id!")]))
            else:
                with open("./data/authoid.txt", 'w',
                          encoding='utf-8') as real_nam:
                    real_nam_content = ""
                    for i in range(len(target_name)):
                        real_nam_content += target_name[i]
                    real_nam.write(real_nam_content)
                    real_nam.close()
                await app.sendGroupMessage(
                    group, MessageChain.create([Plain("id删除成功!")]))

    if message.asDisplay().startswith("角色曲 "):
        character = message.asDisplay().replace('角色曲 ', '')
        with open("./data/touhou.json",
                  encoding='utf-8') as touhou_characters_file:
            touhou_characters = json.load(touhou_characters_file)
            try:
                touhou_music = touhou_characters[character]
                touhou_characters_file.close()
            except:
                await app.sendGroupMessage(
                    group, MessageChain.create([Plain("没有该角色曲!")]))
                touhou_characters_file.close()
                return
        music_file = './voice/' + touhou_music + '.silk'
        print(music_file)
        await app.sendGroupMessage(
            group,
            MessageChain.create(
                [Voice.fromLocalFile(0, filepath=Path(music_file))]))

    if (message.asDisplay() == "!白名单" or message.asDisplay() == "查看白名单") and (
            group.id == 182721157 or group.id == 1158449372
            or group.id == 431987102 or group.id == 909918392):
        IDs = ['0']
        msg_str = "白名单如下:\n"
        with open("./data/authority.txt", encoding='utf-8') as auth:
            with open("./data/authoid.txt", encoding='utf-8') as authname:
                IDs = auth.readlines()
                Names = authname.readlines()
                for i in range(len(IDs)):
                    msg_str = msg_str + Names[i].replace(
                        '\n', '') + "(" + IDs[i].replace('\n', '') + ")"
                    if i < len(IDs) - 1:
                        msg_str += '\n'
        await app.sendGroupMessage(group,
                                   MessageChain.create([Plain(msg_str)]))

    if message.asDisplay() == "开始直播" or message.asDisplay(
    ) == "关闭直播" or message.asDisplay().startswith("修改直播标题"):
        IDs = ['0']
        with open("./data/authority.txt", encoding='utf-8') as auth:
            IDs = auth.readlines()
            for i in range(len(IDs)):
                IDs[i] = IDs[i].replace('\n', '')

        if not str(member.id) in IDs:
            await app.sendGroupMessage(
                group,
                MessageChain.create(
                    [At(member.id),
                     Plain("  你无权开启直播,请向管理员申请添加白名单^-^")]))
            return
        else:
            if message.asDisplay() == "开始直播":
                Localpath = './data/live.json'
                live_info = {}
                fr = open(Localpath, encoding='utf-8')
                live_info = json.load(fr)
                fr.close()
                if live_info['live_status'] == 1:
                    await app.sendGroupMessage(
                        group,
                        MessageChain.create([
                            At(member.id),
                            Plain("  已经有人在使用直播间了"),
                            Face(faceId=111)
                        ]))
                    return
                else:
                    live_info['member_id'] = member.id
                    live_info['group_id'] = group.id
                    live_info['live_status'] = 1
                    with open(Localpath, "w") as fw:
                        jsObj = json.dumps(live_info)
                        fw.write(jsObj)
                        fw.close()
                    await getlive(app, group, member)

            if message.asDisplay() == "关闭直播" or message.asDisplay() == "停止直播":
                Localpath = './data/live.json'
                live_info = {}
                fr = open(Localpath, encoding='utf-8')
                live_info = json.load(fr)
                fr.close()
                if live_info['member_id'] != member.id or live_info[
                        'group_id'] != group.id:
                    await app.sendGroupMessage(
                        group,
                        MessageChain.create([
                            At(member.id),
                            Plain("  你无权关闭直播"),
                            Face(faceId=111)
                        ]))
                    return
                elif live_info['live_status'] == 0:
                    await app.sendGroupMessage(
                        group,
                        MessageChain.create([
                            At(member.id),
                            Plain("  直播间现在是关闭状态哦~请发送'开始直播'来申请直播!")
                        ]))
                else:
                    live_info['live_status'] = 0
                    with open(Localpath, "w") as fw:
                        jsObj = json.dumps(live_info)
                        fw.write(jsObj)
                        fw.close()
                    await liveend(app, group)

            if message.asDisplay().startswith("修改直播标题"):
                await livechange(app, group, message.asDisplay())
Example #59
0
 async def send_msg(self, msg: str) -> str:
     msgs = {'msgtype': 'text', 'text': {'content': msg}}
     async with aiohttp.request('POST', self.send_api, json=msgs) as resp:
         return await resp.json()
Example #60
0
async def latest_comic_num():
    with async_timeout.timeout(6.5):
        async with aiohttp.request('GET', 'http://xkcd.com/info.0.json') as r:
            xkcd = await r.text()
    return jloads(xkcd)['num']