7

, , . ? , , . , .

? ?

- e , , StartCake .. , API Geekflare - Site Up?

API .

Python , Gmail, . API Is Site Up?


API Is Site Up?

API requests, Python API. Python. . , , , API .

, , Python, :

pip install requests 

( , Python) .

API Geekflare.

API Geekflare

API, Is Site Up?. API Geekflare API, RapidAPI.

GET API KEY, RapidAPI.

GET API KEY

RapidAPI :

RapidAPI

API . RapidAPI, .

RapidAPI

API Geekflare API Is Site Up?, . , . API. Code Snippets Python -> Requests.

Code Snippets

Python, .

API Is Site Up?. , . Python.

import requests
API_URL = "https://geekflare.p.rapidapi.com/up"
def make_api_request():
    headers = {
        'content-type': "application/json",
        'x-rapidapi-host': "geekflare.p.rapidapi.com",
        'x-rapidapi-key': "YOUR_API_KEY"
    }
    payload = r'{"url": "https://www.geekflare.com"}'
    response = requests.request("POST", API_URL, data=payload, headers=headers)
    return response.json()   
if __name__ == '__main__':
    data = make_api_request()
    print(data)
    

API_KEY API RapidAPI . . RapidAPI :

API_KEY

API , .

API_KEY

(-, ). . - () . URL- :

{
    "locations": [
        "uk",
        "us",
        "sg"
    ],
    "url": "geekflare.com"
}
    

.

, API, , . , , .


, Gmail Python , yagmail Gmail.

Gmail . . :

def send_mail():
    gmail = yagmail.SMTP("gmail", "password")
    receiver = "receiver@domain.com"
    subject = "Testing Subject"
    body = "This is a testing mail"
    gmail.send(
        to=receiver,
        subject=subject,
        contents=body,
    )    

yagmail .

API . send_mail , .

, , ? API Is Site Up?, Python :

Python

message. . :

  • Site is up
  • Site is down

, , Site is down. :

import requests
import yagmail
API_URL = "https://geekflare.p.rapidapi.com/up"
def make_api_request():
    headers = {
        'content-type': "application/json",
        'x-rapidapi-host': "geekflare.p.rapidapi.com",
        'x-rapidapi-key': "API_KEY"
    }
    payload = r'{"url": "https://www.abcd.com"}'
    response = requests.request("POST", API_URL, data=payload, headers=headers)
    return response.json()
def send_mail(content):
    gmail = yagmail.SMTP("gmail", "password")
    receiver = "email@domain.com"
    subject = "Your Site is Down"
    gmail.send(
        to=receiver,
        subject=subject,
        contents=content,
    )
if __name__ == '__main__':
    data = make_api_request()
    
    message = data['message']
    ## seding the mail
    if message == 'Site is down.':
        ## extracting the errors from different locations
        locations_data = data['data']
        mail_content = "Your site is down due to unexpected error. See the useful data to resolve errors below.\n\n"
        for location in locations_data:
            mail_content += f"{location['city']}, {location['country']} - {location['error']}\n"
        mail_content += "\nCheck the error and resolve them as soon as possible."
        send_mail(mail_content)
    

. , . , .

, .

, , . . , .

, . . , . : cron . , .


Cron

cron UNIX.

  • .
  • crontab -e, crontab .
  • i .
  • cron, Python , :
0 * * * * /usr/bin/python3 /home/sample.py
cron .

, . , cron.

, . , .


. cron - , , .


50% Merion Academy