From d5373e39729706347387d2b6f930f7a72cd8d263 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Tue, 30 Apr 2024 21:37:11 +0300 Subject: [PATCH 1/7] add: docker config file --- Dockerfile | 18 ++++++++++++++++++ docker-compose.yml | 0 entrypoint.sh | 0 test.py | 13 +++++++++++++ 4 files changed, 31 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml create mode 100644 entrypoint.sh create mode 100644 test.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0c4bfa6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,18 @@ + +FROM python:3.12.3-alpine + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +WORKDIR /app + +COPY . /app/ +COPY requirements.txt /app/ + +RUN pip install --upgrade pip +RUN pip install -r /app/requirements.txt + +COPY ./entrypoint.sh /app/ +RUN chmod +x /app/entrypoint.sh + +ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..e69de29 diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..e69de29 diff --git a/test.py b/test.py new file mode 100644 index 0000000..a16c990 --- /dev/null +++ b/test.py @@ -0,0 +1,13 @@ + +from producthunt import ProductHunt +api_key = 'wtpXwVAiQ5gH2ahM1e9ziWe0bCuzOaqahVZfsZRaccI' +ph = ProductHunt(api_key) + + +daily_products = ph.get_daily() +for product in daily_products: + print(f"ID: {product['ID']}") + print(f"Name: {product['Name']}") + print(f"Tagline: {product['Tagline']}") + + From b1ac77d0194acc0e54bfc71bfd5adb2b606c6930 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Tue, 30 Apr 2024 21:53:16 +0300 Subject: [PATCH 2/7] add: compose file --- docker-compose.yml | 16 ++++++++++++++++ entrypoint.sh | 3 +++ requirements.txt | 16 ++++++++++++++++ server.py | 17 +++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 requirements.txt create mode 100644 server.py diff --git a/docker-compose.yml b/docker-compose.yml index e69de29..90df317 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -0,0 +1,16 @@ +version: '3' + +services: + producthunt: + build: + context: . + dockerfile: Dockerfile + container_name: producthunt + env_file: + - .env + ports: + - "8000:8000" + volumes: + - .:/app + restart: unless-stopped + diff --git a/entrypoint.sh b/entrypoint.sh index e69de29..380f99f 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 server.py diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..6f474d6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,16 @@ +aniso8601==9.0.1 +blinker==1.8.1 +certifi==2024.2.2 +charset-normalizer==3.3.2 +click==8.1.7 +Flask==3.0.3 +graphene==3.3 +graphql-core==3.2.3 +graphql-relay==3.2.0 +idna==3.7 +itsdangerous==2.2.0 +Jinja2==3.1.3 +MarkupSafe==2.1.5 +requests==2.31.0 +urllib3==2.2.1 +Werkzeug==3.0.2 diff --git a/server.py b/server.py new file mode 100644 index 0000000..8fc9b84 --- /dev/null +++ b/server.py @@ -0,0 +1,17 @@ +from flask import Flask +from flask import jsonify +app = Flask(__name__) + + + +@app.route('/change//') +def changeroute(dollar, cents): + print(f"Make Change for {dollar}.{cents}") + amount = f"{dollar}.{cents}" + result = change(float(amount)) + return jsonify(result) + + + +if __name__ == '__main__': + app.run(host='0.0.0.0', port=8000, debug=True) \ No newline at end of file From b6bb4fb08f24b439c19734156ef00265c01f5dd9 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Tue, 30 Apr 2024 22:23:07 +0300 Subject: [PATCH 3/7] add: flask server for hosting api based responses --- server.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/server.py b/server.py index 8fc9b84..6c69751 100644 --- a/server.py +++ b/server.py @@ -1,15 +1,16 @@ from flask import Flask from flask import jsonify -app = Flask(__name__) +from producthunt import ProductHunt +api_key = 'wtpXwVAiQ5gH2ahM1e9ziWe0bCuzOaqahVZfsZRaccI' +ph = ProductHunt(api_key) +app = Flask(__name__) -@app.route('/change//') -def changeroute(dollar, cents): - print(f"Make Change for {dollar}.{cents}") - amount = f"{dollar}.{cents}" - result = change(float(amount)) - return jsonify(result) +@app.route('/product-details') +def get_product_details(): + data = ph.get_product_details(slug='product') + return jsonify(data) From e4ba18428673c062fe58a5a1f02ba9a90c60e7f6 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Wed, 1 May 2024 00:06:10 +0300 Subject: [PATCH 4/7] add: fix docker config and permissions --- docker-compose.yml | 2 -- entrypoint.sh | 0 2 files changed, 2 deletions(-) mode change 100644 => 100755 entrypoint.sh diff --git a/docker-compose.yml b/docker-compose.yml index 90df317..440d907 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,8 +6,6 @@ services: context: . dockerfile: Dockerfile container_name: producthunt - env_file: - - .env ports: - "8000:8000" volumes: diff --git a/entrypoint.sh b/entrypoint.sh old mode 100644 new mode 100755 From 713a591a87af65e61206c9dd44e8a4232f7a5533 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Wed, 1 May 2024 00:08:26 +0300 Subject: [PATCH 5/7] add: fix docker config and permissions --- server.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 6c69751..84e7c7a 100644 --- a/server.py +++ b/server.py @@ -2,7 +2,7 @@ from flask import jsonify from producthunt import ProductHunt -api_key = 'wtpXwVAiQ5gH2ahM1e9ziWe0bCuzOaqahVZfsZRaccI' +api_key = '' ph = ProductHunt(api_key) app = Flask(__name__) @@ -13,6 +13,12 @@ def get_product_details(): return jsonify(data) +@app.route('/get-daily') +def get_daily(): + data = ph.get_daily() + return jsonify(data) + + if __name__ == '__main__': app.run(host='0.0.0.0', port=8000, debug=True) \ No newline at end of file From 3f279874ce067fea100d5a7b53510e15b6bb55c0 Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Wed, 1 May 2024 00:10:00 +0300 Subject: [PATCH 6/7] done: api endpoints --- server.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/server.py b/server.py index 84e7c7a..8bd0d57 100644 --- a/server.py +++ b/server.py @@ -12,12 +12,16 @@ def get_product_details(): data = ph.get_product_details(slug='product') return jsonify(data) - @app.route('/get-daily') def get_daily(): data = ph.get_daily() return jsonify(data) +@app.route('/get-posts-by-topic') +def get_posts_by_topic(): + data = ph.get_posts_by_topic() + return jsonify(data) + if __name__ == '__main__': From 6a7b17d879882cf16ee79765d49540c1fe82740f Mon Sep 17 00:00:00 2001 From: maen08 <2001stany@gmail.com> Date: Wed, 1 May 2024 00:29:24 +0300 Subject: [PATCH 7/7] add: readme file for docker implementation --- README.md | 37 +++++++++++++++++++++++++++++++++++++ server.py | 2 +- test.py | 13 ------------- 3 files changed, 38 insertions(+), 14 deletions(-) delete mode 100644 test.py diff --git a/README.md b/README.md index d1c2219..dcb6dfd 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,43 @@ for product in products: print(f"Tagline: {product['Tagline']}") ``` + + +## Run it on Docker +- You can also clone the project and run it with docker exposing the function calls implemented as REST API endpoints. To do that follow these commands: + +``` +git clone https://github.com/dariubs/producthunt.py.git + +``` + +- Add your API token that you get from ProductHunt +``` +cd producthunt.py + +open a file server.py then add it => api_key = 'product-hunt-token' + +``` + +- Then build and run your docker container + +``` +cd producthunt.py + +docker-compose up --build + +``` + +- It will build and run the container on `http://localhost:8000`. Endpoints are named based on the current implemented funtions: + +``` +/get-daily +/product-details +/get-posts-by-topic + +``` + + ## License This project is licensed under the terms of the MIT license. diff --git a/server.py b/server.py index 8bd0d57..669e925 100644 --- a/server.py +++ b/server.py @@ -2,7 +2,7 @@ from flask import jsonify from producthunt import ProductHunt -api_key = '' +api_key = 'product-hunt-token' ph = ProductHunt(api_key) app = Flask(__name__) diff --git a/test.py b/test.py deleted file mode 100644 index a16c990..0000000 --- a/test.py +++ /dev/null @@ -1,13 +0,0 @@ - -from producthunt import ProductHunt -api_key = 'wtpXwVAiQ5gH2ahM1e9ziWe0bCuzOaqahVZfsZRaccI' -ph = ProductHunt(api_key) - - -daily_products = ph.get_daily() -for product in daily_products: - print(f"ID: {product['ID']}") - print(f"Name: {product['Name']}") - print(f"Tagline: {product['Tagline']}") - -