Hi! 😊

My First API images based on Flask

My First API images based on Flask

HAPPY New Year 2019 🙂

For the past few weeks, I was working on Flask to get URL of Images from the internet and publish it as JSON format to be used by other applications (Desktop background changer/ Telegram/ WhatsApp/ Nodered….)

Current API URL: https://apiimages.kushal.net
GitLab: https://gitlab.com/appadudevraj7/apiimages/

In the process, I’ve tried to implement the application on Docker and using GitLAB CI/CD to push to the APP server, for CI/CD i’m still experimenting 🙂

The API is based on Python Flask and for the api documentation I have used the ApiDocJs (http://apidocjs.com/)


APIDOCJs is amazing as it reads annotations directly from the source codes and apidoc scans the codes when building the whole docker images and it generates html on document root path which is then read by nginx.

""" 
  @api {get} /lexpresshumor/latest Get Latest Image from LexpressHumor
  @apiVersion 0.0.1
  @apiName getlexpresshumorlatest
  @apiGroup LexpressHumor
 
  @apiExample Example usage:
  curl -i https://apiimages.kushal.net/api/v1.0/lexpress/latest
 
  @apiSuccess {String} url The URL of image.
  @apiSuccess {String} source The source of URL of image.
 
  @apiError Google_it_I_dont_have_it -  No image is found with current keyword
 
"""

The docker-compose is splitted into 3 containers,

  • APIDOC to generate the documentation api from my source code (this container is closed automatically when the executions is done)
  • it mounts the 3 volumes from my drive (template, sourcecode and web document root)
  • Nginx to host the html documents – Container listening on internal port 80
  • API image Application on Flask which is listening on internal port 5000 (the nginx will be able to communicate to it)

I have Traefik as my LoadBalancer (Port 80 and 443) for all my containers and it will distribute traffic and create SSL certificate automatically with first launch to my containers based on the URL.

The docker-compose.yml looks like this 

version: '3'
services:
        apidoc:
              container_name: apidoc_test
              build: 
                     context: .
                     dockerfile: apiDockerFile 
              volumes:
                   - ./API_images/routes:/api:ro
                   - ./Web:/web:rw
                   - ./apidoc_template:/template:ro
              command: apidoc -i /api -o /web -t /template

        web:
                container_name: APIImages_nginx
                build: .
                command: nginx -g "daemon off";
                restart: always
                volumes:
                       - ./Web:/www:ro
                depends_on:      
                        - apidoc
                        - apiimages
                        
                labels:
                        - traefik.backend=$web_traefik_name
                        - traefik.frontend.rule=Host:$web_traefik_host 
                        - traefik.docker.network=proxy
                        - traefik.port=80
                networks:
                        - traefik_proxy

        apiimages:
                build: 
                        context: .
                        dockerfile: API_images/Dockerfile
                command: gunicorn -w 1 app:app -b :5000 --log-level debug --reload
                restart: always
                labels:
                        - traefik.backend=$apiimages_traefik_name
                        - traefik.docker.network=proxy
                        - traefik.port=5000
                networks:
                        - traefik_proxy

networks:
        traefik_proxy:
                external: true

The codes are on gitlab https://gitlab.com/appadudevraj7/apiimages/

The Components:-

ApiDoc Container
Once the docker-compose up is launched, the ApiDoc will mount the 3 volumes and compile a Dockerfile
./api_template -> contain the template web html pages from APIDOCJS
./Web -> its the document root for my nginx
./API_images/routes -> contains all the source codes for different image searches

The Dockfile will download the apidoc from npm library and will run the following command:
command: apidoc -i /api -o /web -t /template
-i -> takes as Input a directory with the data (source code with annotations)
-o -> it outputs to a directory which is then used to display on web
-t -> it takes a template

Nginx Container
The homepage of the Nginx will be the Documentation API created by the APIDoc, the /web is mounted on both containers
All requests which has /api is redirect automatically to the flask application which is listening on port 5000.

APIImages Flask Application
The application is developed on flask and I have created different routes for each website and all the files are imported on the main app (app.py)

The APP basically makes a requests to a website (lexpresshumor for instance) and scans for html tags or json for images then gets an array as output where it picks one at random 🙂

Current Implementations:

  • Background changer on my linux machine whenever I open a new tab on my terminal
  • Automatically sends me a pictures on WhatsApp and Telegram
  • Easy to integrate with NodeRed http_requests nodes to get images and do something cool ^^

I have also implemented some source website like (reddit/xxxpictures/bonjourmadame..) to get beautifull/sexy pictures )

Current API images Source List

  • Instagram tags
  • Reddit Searches
  • Nasa picture of the day
  • Lexpress Humor from Mauritius
  • Bing picture of the day
  • xxxpictures
  • BonjourMadame

More API to come and more posts for this year!!!

Made with ♡ ♥💕❤ from Mauritius