def curl_response(method, params): curl_cnn = curl_connection() response = StringIO() body_as_dict = {"jsonrpc": "2.0", "id": "curl", "method": method, "params": params} body_as_json_string = json.dumps(body_as_dict) # dict to json curl_cnn.setopt(curl_cnn.WRITEFUNCTION, response.write) curl_cnn.setopt(curl_cnn.POSTFIELDS, body_as_json_string) curl_cnn.perform() status_code = curl_cnn.getinfo(curl_cnn.RESPONSE_CODE) response = json.loads(response.getvalue()) curl_cnn.close() out = response.get('result') if out: out = json.dumps(out) err = response.get('error') if err: err = json.dumps(err) return out, err, status_code
def handler(ctx, data: io.BytesIO = None): try: body = json.loads(data.getvalue()) data = body.get("data", {}) additional_details = data.get("additionalDetails", {}) namespace = additional_details.get("namespace") bucket = additional_details.get("bucketName") obj = data.get("resourceName") eventtime = body.get("eventTime") source = "Oracle Cloud" #adding a source name. service = "OCI Logs" #adding a servicen name. datafile = request_one_object(namespace, bucket, obj) data = str(datafile, 'utf-8') datadoghost = os.environ['DATADOG_HOST'] datadogtoken = os.environ['DATADOG_TOKEN'] for lines in data.splitlines(): logging.getLogger().info("lines " + lines) payload = {} payload.update({"host": obj}) payload.update({"time": eventtime}) payload.update({"ddsource": source}) payload.update({"service": service}) payload.update({"event": lines}) headers = { 'Content-type': 'application/json', 'DD-API-KEY': datadogtoken } x = requests.post(datadoghost, data=json.dumps(payload), headers=headers) logging.getLogger().info(x.text) print(x.text) except (Exception, ValueError) as ex: # print(str(ex)) logging.getLogger().info(str(ex)) return
def handler(ctx, data: io.BytesIO = None): name = "World" try: body = json.loads(data.getvalue()) data = body.get("data") #バックアップするblock volumeのIDを取得する resourceId = data.get("resourceId") logging.info("--------resourceId:" + resourceId + ":--------------") except (Exception, ValueError) as ex: logging.info(str(ex)) #バックアップするblock volumeのID source_backup_id = resourceId #バックアップターゲットリージョン destination_region = "eu-frankfurt-1" #OCI configファイルの置く場所 source_config = oci.config.from_file("/function/key/config", "tao-oci-profile") destination_config = source_config.copy() destination_config["region"] = destination_region source_blockstorage_client = oci.core.BlockstorageClient(source_config) destination_blockstorage_client = oci.core.BlockstorageClient( destination_config) # load config and create clients (one for the source region and one for the destination region). # print('Copying backup with ID {} from {} to {} using new display name: {} and kms key id: {} \n'.format( # source_backup_id, source_config["region"], destination_region)) result = source_blockstorage_client.copy_volume_backup( source_backup_id, oci.core.models.CopyVolumeBackupDetails( destination_region=destination_region # display_name=display_name, # kms_key_id=kms_key_id )) print('Copy backup response status: {}, copied backup: {}\n'.format( result.status, result.data)) print('Waiting for the copied backup to be in available state...') # query the destination region for the copied' backup's status and wait for it to be available. copied_backup = oci.wait_until( destination_blockstorage_client, destination_blockstorage_client.get_volume_backup(result.data.id), 'lifecycle_state', 'AVAILABLE').data print('Backup successfully copied: {}'.format(copied_backup)) print('Example script done') return response.Response(ctx, response_data=json.dumps( {"message": "Hello {0}".format(name)}), headers={"Content-Type": "application/json"})
async def handler(ctx, data: io.BytesIO=None, loop=None): if data is not None or len(data) != 0: data = ujson.loads(data.getvalue()) sys.stderr.write("payload to post-to-slack: {0}\n".format(ujson.dumps(data))) msg = data.get("msg") if not msg: msg = "Muy Guapo!" if not data.get("image_url"): res = await post_msg_to_slack(msg) else: res = await post_image_to_slack(msg, data.get("image_url")) return response.Response( ctx, status_code=200, response_data=json.dumps( {"ok": True} ), headers={"Content-Type": "application/json"} ) #return res
async def reverse_image_search(_, message: Message): if not message.reply_to_message: return await message.reply_text( "Reply to a message to reverse search it.") reply = message.reply_to_message if (not reply.document and not reply.photo and not reply.sticker and not reply.animation and not reply.video): return await message.reply_text( "Reply to an image/document/sticker/animation to reverse search it." ) m = await message.reply_text("Searching...") file_id = get_file_id_from_message(reply) if not file_id: return await m.edit("Can't reverse that") image = await app.download_media(file_id, f"{randint(1000, 10000)}.jpg") async with aiofiles.open(image, "rb") as f: if image: search_url = "http://www.google.com/searchbyimage/upload" multipart = { "encoded_image": (image, await f.read()), "image_content": "", } def post_non_blocking(): return requests.post(search_url, files=multipart, allow_redirects=False) loop = get_running_loop() response = await loop.run_in_executor(None, post_non_blocking) location = response.headers.get("Location") os.remove(image) else: return await m.edit("Something wrong happened.") headers = { "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:58.0) Gecko/20100101 Firefox/58.0" } try: soup = await get_soup(location, headers=headers) div = soup.find_all("div", {"class": "r5a77d"})[0] text = div.find("a").text text = f"**Result**: [{text}]({location})" except Exception: return await m.edit( f"**Result**: [Link]({location})", disable_web_page_preview=True, ) # Pass if no images detected try: url = "https://google.com" + soup.find_all( "a", {"class": "ekf0x hSQtef"})[0].get("href") soup = await get_soup(url, headers=headers) media = [] for img in soup.find_all("img"): if len(media) == 2: break if img.get("src"): img = img.get("src") if "image/gif" in img: continue img = BytesIO(b64decode(img)) img.name = "img.png" media.append(img) elif img.get("data-src"): img = img.get("data-src") media.append(img) # Cache images, so we can use file_ids tasks = [app.send_photo(MESSAGE_DUMP_CHAT, img) for img in media] messages = await gather(*tasks) await message.reply_media_group([ InputMediaPhoto( i.photo.file_id, caption=text, ) for i in messages ]) except Exception: pass await m.edit( text, disable_web_page_preview=True, )