commit d6fa621e8e0791d650002945c8d4fb6d20f9f3c3 Author: lina Date: Fri May 24 16:12:15 2024 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d75edea --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3f5c155 --- /dev/null +++ b/README.md @@ -0,0 +1,12 @@ +# Projet API + +### Description + +Ce projet a été réaliser dans le cadre du cours de python en formation DEVOPS au CNAM. + +### Prérequis + +- Python 3.11.2 +- flask : 3.0.2 +- requests : 2.31.0 +- bootstrap : 4.3.1 diff --git a/app.py b/app.py new file mode 100644 index 0000000..f14f39d --- /dev/null +++ b/app.py @@ -0,0 +1,18 @@ +import requests +from flask import Flask, render_template +from fonctions import * + + +app = Flask(__name__) + +@app.route("/") +def index(): + return render_template("index.html") + +@app.route("/livres", methods=("GET", "POST")) +def livres(): + datas = get_books() + return render_template("livres.html", datas=datas) + +if __name__ == " __main__": + app.run() \ No newline at end of file diff --git a/fonctions.py b/fonctions.py new file mode 100644 index 0000000..031f40b --- /dev/null +++ b/fonctions.py @@ -0,0 +1,55 @@ +import requests, json, ipaddress, random +from flask import request + +def get_headers(): + + ip = [] + + for i in range(0, 4): + ip.append(random.randint(0, 254)) + + random_ip = '' + for i in range(0, 3): + random_ip += str(ip[i]) + '.' + + random_ip += str(ip[3]) + + headers = {"X-Forwarded-For": random_ip} + + return headers + +class Livre: + def __init__(self, title, author, publish, isbn, img_url): + self.title = title + self.author = author + self.publish = publish + self.isbn = isbn + self.img_url = img_url + +def get_books(): + + livres = [] + headers = get_headers() + + # La condition if permet de n'afficher du contenue de l'API que lorsqu'il y a une requête qui est effectué. + if request.method == "POST": + choix = request.form["choix"] + + # Concaténations a l'url de l'API du choix de l'utilisateur reçu via request.form et on choisi les informations que nous souhaitons récuperer. + url = "https://openlibrary.org/search.json?q=" + choix + "&fields=title,author_name,first_publish_year,isbn" + response = requests.get(url, headers) + + # Transformation des données reçu de l'API en objet JSON pour les avoir sous forme clé/valeur. + datas = json.loads(response.text) + + for e in datas["docs"]: + # A l'aide du constructeur de la classe Livre on affine les données reçu et on concaténe isbn a l'url images. + livre = Livre(e["title"], e["author_name"][0], e["first_publish_year"], e["isbn"][0], "https://covers.openlibrary.org/b/isbn/" + e["isbn"][0] + "-L.jpg") + + # Informations ajouté au dictionnaire livres definit plus haut. + livres.append(livre) + else: + print("Pas de requête effectué") + livres.append("No datas") + + return livres \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..3862f5c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +gunicorn==22.0.0 +packaging==24.0 +requests==2.31.0 +flask==3.0.2 \ No newline at end of file diff --git a/templates/base.html b/templates/base.html new file mode 100644 index 0000000..2cec8ea --- /dev/null +++ b/templates/base.html @@ -0,0 +1,49 @@ + + + + + + Projet API + + + + + + + + +
+ {% block content %} + {% endblock %} +
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..10ea4db --- /dev/null +++ b/templates/index.html @@ -0,0 +1,34 @@ +{% extends "base.html" %} + +{% block content %} +
+
+
+

💻 Welcome!

+
+
+

💡 Find your new favorite book among several provided categories. 💡

+
+ +
+
+
    +
  • +
  • ★ Horror
  • +
  • ★ Thriller
  • +
  • ★ Science Fisction
  • +
+
+
+
    +
  • ★ Fiction
  • +
  • ★ Romance
  • +
  • ★ Fantasy
  • +
+
+ +
+
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/livres.html b/templates/livres.html new file mode 100644 index 0000000..b6d4bc6 --- /dev/null +++ b/templates/livres.html @@ -0,0 +1,47 @@ +{% extends "base.html" %} +{% block content %} +
+
+

Books 📚

+

Choose a category and see our selection

+
+ +
+ + + + + + + + + +
+ + {% if datas[0] != "No datas" %} +
+
+ + {% for elem in datas %} +
+
+
+
+ There is no available image for this book! +
+
+
+
{{ elem.title }}
+

{{ elem.author }}

+

{{ elem.publish }}

+
+
+
+
+
+ {% endfor %} +
+
+ {% endif %} +
+{% endblock %} \ No newline at end of file