def check(self): """ Получить события от сервера один раз :returns: `list` of :class:`Event` """ values = { 'act': 'a_check', 'key': self.key, 'ts': self.ts, 'wait': self.wait, } self.url = f'{self.url}?{urllib.parse.urlencode(values)}' response = json.dumps(requests.get2json(self.url)) if 'failed' not in response: self.ts = response['ts'] return [ self._parse_event(raw_event) for raw_event in response['updates'] ] elif response['failed'] == 1: self.ts = response['ts'] elif response['failed'] == 2: self.update_longpoll_server(update_ts=False) elif response['failed'] == 3: self.update_longpoll_server() return []
def export_chat_invite_link(self, message_id): try: link = json.loads(fast_req.get2json( f'https://api.telegram.org/bot{settings.GROUP}/exportChatInviteLink?chat_id={self.database.chat_id}' ))['result'] except fast_req.NimPyException: self.send_message(self.translate('bot_permission'), reply_to_message_id=message_id) else: self.send_message(self.translate('link', link), reply_to_message_id=message_id)
def handle(self, *args, **options): Items.objects.all().delete() print("loading data from Hacker News api... ") url = 'https://hacker-news.firebaseio.com/v0/maxitem.json?print=pretty' req = request.get(url).json() # removes html characters from data text_maker = html2text.HTML2Text() text_maker.ignore_links = True text_maker.bypass_tables = False text_maker.open_quote = True text_maker.close_quote = True for i in random.sample(range(0, req), 70000): response = requests.get2json( f'https://hacker-news.firebaseio.com/v0/item/{i}.json?print=pretty' ) print(i) data = json.loads(response) if 'id' not in data: # raise ValueError("No target in given data") continue if 'by' not in data: # raise ValueError("No target in given data") continue if 'text' not in data: # raise ValueError("No target in given data") continue if data['type'] == 'comment': print('inserting into database') # print(data, '\n') text_conv = text_maker.handle(data['text']) ''' Checking keys should be easier than this so maybe this can be a function. ''' Items.objects.create( id=data['id'], by=data['by'], text=text_conv, ) else: print('nope')
print(requests.put("http://httpbin.org/put", """{"foo": "bar", "baz": true}""")) # HTTP PUT. print(requests.delete("http://httpbin.org/delete")) # HTTP DELETE. print( requests.patch("http://httpbin.org/patch", """{"foo": "bar", "baz": true}""")) # HTTP PATCH. print(requests.get2str( "http://httpbin.org/get")) # HTTP GET body only to string response. print(requests.get2dict( "http://httpbin.org/get")) # HTTP GET body only to dictionary response. print(requests.get2json( "http://httpbin.org/get")) # HTTP GET body only to JSON response. print( requests.post2str("http://httpbin.org/post", """{"foo": "bar", "baz": true}""") ) # HTTP POST data only to string response. print( requests.post2dict("http://httpbin.org/post", """{"foo": "bar", "baz": true}""") ) # HTTP POST data only to dictionary response. print( requests.post2json( "http://httpbin.org/post", """{"foo": "bar", "baz": true}""")) # HTTP POST data to JSON response.
def test_get2json(self): self.assertTrue(dict(json.loads(faster_than_requests.get2json("http://httpbin.org/get"))))