diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 7aa21da6..00000000 --- a/.gitignore +++ /dev/null @@ -1,28 +0,0 @@ -##Β Open-Runtime specific rules -code.tar.gz - -##Β General rules, taken from https://gist.github.com/octocat/9257657 - -# Compiled source # -################### -*.com -*.dll -*.exe -*.o -*.so - -# Logs and databases # -###################### -*.log -*.sql -*.sqlite - -# OS generated files # -###################### -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -ehthumbs.db -Thumbs.db \ No newline at end of file diff --git a/compress-image-sample/README.md b/compress-image-sample/README.md new file mode 100644 index 00000000..507803a4 --- /dev/null +++ b/compress-image-sample/README.md @@ -0,0 +1,61 @@ +# πŸ“± Validate phone number and get it's country information + +A Python Cloud Function that figures out country in which a phone number is registered. + +_Example input:_ + +```json +{ + "provider":"tinypng", + "image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg==" +} +``` + +> Image to compress should be received as payload in base64 format . + + +_Example output:_ + + +```json +{ + "phoneNumber": "+421957215740", + "phonePrefix": "+421", + "countryCode": "SK", + "countryName": "Slovakia" +} +``` + +## πŸ“ Environment Variables + +List of environment variables used by this cloud function: + +- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of your Appwrite server +- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key +- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically. + +## πŸš€ Deployment + +1. Clone this repository, and enter this function folder: + +``` +$ git clone https://github.com/open-runtimes/examples.git && cd examples +$ cd python/convert-phone-number-to-country-name +``` + +2. Enter this function folder and build the code: +``` +docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/python:v2-3.10 sh /usr/local/src/build.sh +``` +As a result, a `code.tar.gz` file will be generated. + +3. Start the Open Runtime: +``` +docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=main.py --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/python:v2-3.10 sh /usr/local/src/start.sh +``` + +Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Python runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/python-3.10). + +## πŸ“ Notes + - This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). + - This example is compatible with Python 3.10. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file diff --git a/compress-image-sample/main.py b/compress-image-sample/main.py new file mode 100644 index 00000000..99201cfb --- /dev/null +++ b/compress-image-sample/main.py @@ -0,0 +1,40 @@ +import base64 +import tinify +import json +import os +import tempfile + + + +def decode(encoded_value): + decoded = base64.b64decode(encoded_value) + return decoded + +def encode(plaintext): + encoded = base64.b64encode(plaintext) + return encoded + +def errorMessage(res, message): + return res.json({ + "success": False, + "message": "fail", + }) + +def main(req, res): + # Get the payload then grab the provider and image + payload = req.payload + provider = payload['provider'] + image = payload['image'] + + # Get the variables (usually the api key) + variables = req.variables + + # Grab the api key + secret_key = variables['TINYPNG_API_KEY'] + # Set the tinify key as the api key + tinify.key = secret_key + + # Decode the image data from base64 + decoded = base64.b64decode(image) + + return None \ No newline at end of file diff --git a/compress-image-sample/requirements.txt b/compress-image-sample/requirements.txt new file mode 100644 index 00000000..c65c821b --- /dev/null +++ b/compress-image-sample/requirements.txt @@ -0,0 +1 @@ +tinify==1.6.0 \ No newline at end of file diff --git a/python/compress-image/README.md b/python/compress-image/README.md new file mode 100644 index 00000000..507803a4 --- /dev/null +++ b/python/compress-image/README.md @@ -0,0 +1,61 @@ +# πŸ“± Validate phone number and get it's country information + +A Python Cloud Function that figures out country in which a phone number is registered. + +_Example input:_ + +```json +{ + "provider":"tinypng", + "image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg==" +} +``` + +> Image to compress should be received as payload in base64 format . + + +_Example output:_ + + +```json +{ + "phoneNumber": "+421957215740", + "phonePrefix": "+421", + "countryCode": "SK", + "countryName": "Slovakia" +} +``` + +## πŸ“ Environment Variables + +List of environment variables used by this cloud function: + +- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of your Appwrite server +- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key +- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically. + +## πŸš€ Deployment + +1. Clone this repository, and enter this function folder: + +``` +$ git clone https://github.com/open-runtimes/examples.git && cd examples +$ cd python/convert-phone-number-to-country-name +``` + +2. Enter this function folder and build the code: +``` +docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/python:v2-3.10 sh /usr/local/src/build.sh +``` +As a result, a `code.tar.gz` file will be generated. + +3. Start the Open Runtime: +``` +docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=main.py --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/python:v2-3.10 sh /usr/local/src/start.sh +``` + +Your function is now listening on port `3000`, and you can execute it by sending `POST` request with appropriate authorization headers. To learn more about runtime, you can visit Python runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/python-3.10). + +## πŸ“ Notes + - This function is designed for use with Appwrite Cloud Functions. You can learn more about it in [Appwrite docs](https://appwrite.io/docs/functions). + - This example is compatible with Python 3.10. Other versions may work but are not guaranteed to work as they haven't been tested. \ No newline at end of file diff --git a/python/compress-image/main.py b/python/compress-image/main.py new file mode 100644 index 00000000..a5826bc2 --- /dev/null +++ b/python/compress-image/main.py @@ -0,0 +1,104 @@ +import base64 +import tinify +import json +import os +import tempfile +# Example Function Payload +{ + "provider":"tinypng", + "image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg==" +} +# Successful Function Response +{ + "success":True, + "image":"iVBORw0KGgoAAAANSUhE...o6Ie+UAAAAASU5CYII=" +} +# Error Function Response +{ + "success":False, + "message":"Input file is not an image." +} + + +def decode(encoded_value): + decoded = base64.b64decode(encoded_value) + return decoded + +def encode(plaintext): + encoded = base64.b64encode(plaintext) + return encoded + +def errorMessage(res, message): + return res.json({ + "success": False, + "message": "fail", + }) + +''' +Scraps + + Read data from external websites + # data = encodeExternal("python/compress-image/base64.txt") + # print(data) + + +How to encode a local image + # Encode the image + # with open("python/compress-image/dogs.jpeg", "rb") as image_file: + # encoded_string = encode(image_file.read()) +''' + + +# Potential Curl Command +# curl http://localhost:3000/ +# -H "X-Internal-Challenge: secret-key" +# -H "Content-Type: application/json" +# -d '{"payload": {"provider":"tinypng","image":"iVBORw0KGgoAAAANSUhEUgAAAaQAAALiCAY...QoH9hbkTPQAAAABJRU5ErkJggg=="}, "variables": {"TINYPNG_API_KEY": ""}}' + +def main(req, res): + # Get the payload then grab the provider and image + payload = req.payload + provider = payload['provider'] + image = payload['image'] + + # Get the variables (usually the api key) + variables = req.variables + + # Grab the api key + secret_key = variables['TINYPNG_API_KEY'] + # Set the tinify key as the api key + tinify.key = secret_key + + # Decode the image data from base64 + decoded = base64.b64decode(image) + + # Create a temp directory inside of the current working directory. Prefix it as "temp" + temp_dir = tempfile.mkdtemp(prefix='temp', dir=os.getcwd()) + + # Generate a copy of the decoded image data as non_optimized.jpg + file_path = os.path.join(temp_dir, "non_optimized.jpg") + # I used with open, to write media file with the decoded base64 contents + with open(file_path, "wb") as f: + f.write(decoded) + + # Compress the non_optimized image + source = tinify.from_file(file_path) + output_file_path = os.path.join(temp_dir, "optimized.jpg") + source.to_file(output_file_path) + + # Package the optimized image and encode it + with open(output_file_path, "rb") as image_file: + encoded = base64.b64encode(image_file.read()) + return res.json( + { + "success:" : True, + "image": str(encode) + } + ) +# request = {"payload": {"provider":"tinypng","image":"iVBORw0KGgoAAAANSUhEUgAAABEAAAAOCAMAAAD+MweGAAADAFBMVEUAAAAAAFUAA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WxzNj+gAgAGfvvDdCQIHoSnGYcGDE2nH92DoRqTYJ2bTcsKgqhIi47VdgAWNmwFSFA1UAAT2sSFcnq8a3x/zkkJrhaHT3N+hD3aH7ZuabGHX7bsSMhxwTJLr3evf1e0nBVcwmqcTZuatKoJaB7dSHjTZdM0G1HBTWefly//q2EB7/BEvk5vmzeQaJ7/xKPImpzv8/s4grhAxHl0DsqGUAAAAASUVORK5CYII="}, "variables": {"TINYPNG_API_KEY": "R4nM3B54NbHNcHblC0XXl0LZyV82PBgZ"}} +# response = None +# print( compressImage(request, response) ) + + +# curl http://localhost:3000/ -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -d '{"payload": {"provider": "tinypng", "image":"iVBORw0KGgoAAAANSUhEUgAAABEAAAAOCAMAAAD+MweGAAADAFBMVEUAAAAAAFUAAKoAAP8AJAAAJFUAJKoAJP8ASQAASVUASaoASf8AbQAAbVUAbaoAbf8AkgAAklUAkqoAkv8AtgAAtlUAtqoAtv8A2wAA21UA26oA2/8A/wAA/1UA/6oA//8kAAAkAFUkAKokAP8kJAAkJFUkJKokJP8kSQAkSVUkSaokSf8kbQAkbVUkbaokbf8kkgAkklUkkqokkv8ktgAktlUktqoktv8k2wAk21Uk26ok2/8k/wAk/1Uk/6ok//9JAABJAFVJAKpJAP9JJABJJFVJJKpJJP9JSQBJSVVJSapJSf9JbQBJbVVJbapJbf9JkgBJklVJkqpJkv9JtgBJtlVJtqpJtv9J2wBJ21VJ26pJ2/9J/wBJ/1VJ/6pJ//9tAABtAFVtAKptAP9tJABtJFVtJKptJP9tSQBtSVVtSaptSf9tbQBtbVVtbaptbf9tkgBtklVtkqptkv9ttgBttlVttqpttv9t2wBt21Vt26pt2/9t/wBt/1Vt/6pt//+SAACSAFWSAKqSAP+SJACSJFWSJKqSJP+SSQCSSVWSSaqSSf+SbQCSbVWSbaqSbf+SkgCSklWSkqqSkv+StgCStlWStqqStv+S2wCS21WS26qS2/+S/wCS/1WS/6qS//+2AAC2AFW2AKq2AP+2JAC2JFW2JKq2JP+2SQC2SVW2Saq2Sf+2bQC2bVW2baq2bf+2kgC2klW2kqq2kv+2tgC2tlW2tqq2tv+22wC221W226q22/+2/wC2/1W2/6q2///bAADbAFXbAKrbAP/bJADbJFXbJKrbJP/bSQDbSVXbSarbSf/bbQDbbVXbbarbbf/bkgDbklXbkqrbkv/btgDbtlXbtqrbtv/b2wDb21Xb26rb2//b/wDb/1Xb/6rb////AAD/AFX/AKr/AP//JAD/JFX/JKr/JP//SQD/SVX/Sar/Sf//bQD/bVX/bar/bf//kgD/klX/kqr/kv//tgD/tlX/tqr/tv//2wD/21X/26r/2////wD//1X//6r////qm24uAAAA1ElEQVR42h1PMW4CQQwc73mlFJGCQChFIp0Rh0RBGV5AFUXKC/KPfCFdqryEgoJ8IX0KEF64q0PPnow3jT2WxzNj+gAgAGfvvDdCQIHoSnGYcGDE2nH92DoRqTYJ2bTcsKgqhIi47VdgAWNmwFSFA1UAAT2sSFcnq8a3x/zkkJrhaHT3N+hD3aH7ZuabGHX7bsSMhxwTJLr3evf1e0nBVcwmqcTZuatKoJaB7dSHjTZdM0G1HBTWefly//q2EB7/BEvk5vmzeQaJ7/xKPImpzv8/s4grhAxHl0DsqGUAAAAASUVORK5CYII="}, "variables": {"TINYPNG_API_KEY": "R4nM3B54NbHNcHblC0XXl0LZyV82PBgZ"}}' +# curl http://localhost:3000/ -H "X-Internal-Challenge: secret-key" -H "Content-Type: application/json" -d '{"variables": {},"payload": "{}","headers": {}}' \ No newline at end of file diff --git a/python/compress-image/requirements.txt b/python/compress-image/requirements.txt new file mode 100644 index 00000000..c65c821b --- /dev/null +++ b/python/compress-image/requirements.txt @@ -0,0 +1 @@ +tinify==1.6.0 \ No newline at end of file