Cum sociis natoque
July 3, 2014
Cum sociis natoque
July 3, 2014

The aim of this project is to build a habit tracking system using the Pixela API.

💡 Technologies Used :

  • Python

  • Requests Library

  • REST API

  • HTTP Methods (POST, PUT, DELETE)

  • JSON

  • Datetime module

				
					import requests
import datetime as dt

#####--------------constants--------------##### :

PIXELA_URL = "https://pixe.la/v1/users"
USER_NAME = " YOUR USERNAME HERE! "
PIXELA_TOKEN = " YOUR PIXELA TOKEN HERE! "
PIXELA_PARAMS ={
    "token" : PIXELA_TOKEN,
    "username" : USER_NAME,
    "agreeTermsOfService" : "yes",
    "notMinor" : "yes"
}
 
#####--------------create a user--------------##### : 

user_creation = requests.post(url=PIXELA_URL,json=PIXELA_PARAMS)
print(user_creation.text)
				
			

Step 1:

In this first step of the project :

  • Import the required modules (requests and datetime) to handle API communication and date formatting
  • Define the necessary constants such as the Pixela endpoint, username, token, and authentication parameters
  • Create a new user by sending a POST request to the Pixela API using Python
				
					#####--------------create a graph--------------##### :

GRAPH_URL = f"https://pixe.la/v1/users/{USER_NAME}/graphs"
GRAPH_PARAMS ={
    "id" : " YOUR PIXELA GRAPH ID ",
    "name" : " YOUR PIXELA GRAPH NAME ",
    "unit" : " YOUR PIXELA GRAPH MEASUREMENT ",
    "type" : "float",
    "color" : "shibafu",
}
HEADERS = {
    "X-USER-TOKEN" : PIXELA_TOKEN
}
graph_creation = requests.post(url=GRAPH_URL,json=GRAPH_PARAMS,headers=HEADERS)
print(graph_creation.text)
				
			

Step 2:

In this second step of the project :

  • Define the graph endpoint URL using the username to target the correct Pixela account
  • Set the graph parameters including the graph ID, name, unit of measurement, data type, and color
  • Authenticate using the user token in the headers and send a POST request to create the graph using the Pixela API
 
				
					#####--------------post a pixel--------------##### :

POST_URL = "https://pixe.la/v1/users/testamir/graphs/codegraphforamir"
POST_PARAMS = {
    "date" : dt.datetime.now().strftime("%Y%m%d"),
    "quantity" : input("How many hours have you exercised on your code? ")
}
post_creation = requests.post(url=POST_URL,json=POST_PARAMS,headers=HEADERS)
print(post_creation.text)

				
			

Step 3:

In this third step of the project :

  • Define the endpoint URL to target a specific graph for posting data
  • Generate today’s date automatically and collect user input for the number of coding hours
  • Send a POST request to add a new pixel (data entry) to the graph using the Pixela API
				
					#####--------------edit a pixel--------------##### :

CHANGE_DATE = " DATE YOU WANT TO DELETE "
CHANGE_URL = f"https://pixe.la/v1/users/testamir/graphs/codegraphforamir/{CHANGE_DATE}"
CHANGE_PARAMS = {
    "quantity" : "8.5"
}
post_change = requests.put(url=CHANGE_URL,json=CHANGE_PARAMS,headers=HEADERS)
print(post_change.text)

				
			

Step 4:

In this fourth step of the project :

  • Specify the date of the pixel that needs to be updated
  • Define the endpoint URL for that specific date inside the graph
  • Send a PUT request to modify the quantity value and update the existing pixel using the Pixela API
				
					#####--------------delete a pixel--------------##### :

DELETE_DATE = " DATE YOU WANT TO DELETE "
DELETE_URL = f"https://pixe.la/v1/users/testamir/graphs/codegraphforamir/{DELETE_DATE}"

post_delete = requests.delete(url=DELETE_URL,headers=HEADERS)
print(post_delete.text)
				
			

Step 5:

In this final step of the project :

  • Specify the date of the pixel that needs to be deleted
  • Define the endpoint URL for that specific date inside the graph
  • Send a DELETE request to remove the selected pixel from the graph using the Pixela API
Â