Day 37 - requests & Habit Tracker


Posted by pei_______ on 2022-05-18

learning from 100 Days of Code: The Complete Python Pro Bootcamp for 2022


Miss two days' posts cause it's some technique issues that I can't totally finish on SMS lessons :<


Habit tracker API

Pixela API
Pixela API Document
Pixela API github


request-documentation

requests.get()
requests.post()
requests.put()
requests.delete()

strftime() Method

today = datetime.today()
yesterday = datetime(year=2022, month=5, day=17)

today.strftime("%Y%m%d")
yesterday.strftime("%Y%m%d")

Constant

import requests
from datetime import datetime

USERNAME = YOUR OWN USERNAME
TOKEN = YOUR OWN TOKEN
GRAPH_ID = "graph1"

headers = {
    "X-USER-TOKEN": TOKEN
}


# The day you want to add info #
today = datetime.today()

Create a new account requests.post()

pixela_endpoint = "https://pixe.la/v1/users"

user_params = {
    "token": TOKEN,
    "username": USERNAME,
    "agreeTermsOfService": "yes",
    "notMinor": "yes"
}

response = requests.post(url=pixela_endpoint, json=user_params)
print(response.text)

Create a new graph requests.post()

graph_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs"

graph_config = {
    "id": GRAPH_ID,
    "name": "Programming Graph",
    "unit": "hour",
    "type": "int",
    "color": "ajisai",
    "timezone": "Asia/Taipei",
}

response = requests.post(url=graph_endpoint, json=graph_config, headers=headers)
print(response.text)

Add a new pixel requests.post()

pixel_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}"


pixel_config = {
    "date": today.strftime("%Y%m%d"),
    "quantity": "5"
}

response = requests.post(url=pixel_endpoint, json=pixel_config, headers=headers)
print(response.text)

update a pixel requests.put()

update_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

update_config = {
    "quantity": "3"
}

response = requests.put(url=update_endpoint, json=update_config, headers=headers)
print(response.text)

delete a pixel requests.delete()

delete_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{today.strftime('%Y%m%d')}"

response = requests.delete(url=delete_endpoint, headers=headers)
print(response.text)

#Python #課堂筆記 #100 Days of Code







Related Posts

關於這個技術共筆部落格

關於這個技術共筆部落格

學 JavaScript 的那些筆記 3 -- ES6

學 JavaScript 的那些筆記 3 -- ES6

MTR04_0703

MTR04_0703


Comments