Skip to content

Instantly share code, notes, and snippets.

View nitobuendia's full-sized avatar

Nito Buendia nitobuendia

View GitHub Profile
@nitobuendia
nitobuendia / trailing_slash_middleware.py
Last active November 9, 2017 17:02
Django middleware that removes additional erroneous backslashes from your requests.
"""Middleware that removes additional meaningless backslashes.
This transforms URLs as follows:
- yourwebsite.com// to yourwebsite.com
- yourwebsite.com/section/// to yourwebsite.com/section
Ideally, this middleware should be placed before Commonmiddleware to avoid
conflicts with APPEND_SLASH. However, this code already appends final slash
if APPEND_SLASH is set to TRUE trying to reduce one additional redirect.
@nitobuendia
nitobuendia / cache_control.py
Created October 26, 2017 13:38
Django middleware that allows to set up a global cache limit for all your requests.
"""Control MAX age for all Django pages."""
from django.conf import settings
from django.utils.deprecation import MiddlewareMixin
class MaxAgeMiddleware(MiddlewareMixin):
"""Set up Cache-Control header for all Django-handled requests."""
def process_response(self, request, response):
"""Adds Cache-Control header to response.
@nitobuendia
nitobuendia / index.js
Last active May 27, 2018 14:19
[My Motivational Coach] - A DialogFlow-powered add-on for Google Home Assistant that replies with a randomly generated motivational quote.
/**
* @fileoverview Handles a DialogFlow request and returns a motivational quote.
*
* In order to deploy, run:
* gcloud beta functions deploy getMotivationQuote --trigger-http
*/
const /** boolean */ EQUAL_PROBABILITY = true;
/**
@nitobuendia
nitobuendia / google_fit.py
Last active August 22, 2021 15:17
Custom sensor for Home Assistant to integrate Google Fit data.
"""Creates a Google Fit sensors.
At the moment, provides two measurements:
- weight: in KG.
- last_updated: entry of the current weight point.
Sensor is designed to be flexible and allow customization to add new Google Fit
dimensions with minimal effort with relative knowledge of Python and Fitness
Rest API.
@nitobuendia
nitobuendia / get_all_price_extensions.py
Last active August 14, 2018 09:46
Retrieves price extensions details at different levels from a Google Ads account.
"""Retrieves price extensions details (at any level, on the data feeds) from a Google Ads account.
If you never installed Google Ads API before, you may need to install it first:
https://github.com/googleads/googleads-python-lib/blob/master/README.md#getting-started
$ pip install setuptools
$ pip install googleads
$ pip install google-auth-oauthlib
In order to use this script, some pre-configuration is needed:
@nitobuendia
nitobuendia / citc1.py
Created September 8, 2018 10:13
Code in the Community application exercises.
"""Asks users for input to generate a list of random numbers and print data."""
import random
def _generate_print_message(number_list):
"""Generates desired print message based on the sequence list of numbers.
Args:
number_list: List[int], List of numbers.
@nitobuendia
nitobuendia / console_code.js
Created May 24, 2019 09:01
Colors of the Wind - Kube-Kube AutoWin
/**
* Clicks the different colored tile.
* Wins the game on https://kuku-kube.com/
* To use, simply start the game and paste this code on
* the JavaScript console.
*/
const clickBoxColor = () => {
let lastColor;
const boxes = document.querySelectorAll('#box span');
for (let i = 0; i < boxes.length; i++) {
@nitobuendia
nitobuendia / google_docs_image_resizer.js
Created June 7, 2019 11:10
Resizes all images in a Google Docs document to a desired width, while limited to a max size (sizes are in cm).
/**
* @fileoverview Resizes all images in a document to a desired width
* while limited to a max size. Sizes are in cm.
*
* This code is meant to be implemented as a script on a Google Docs
* document: Tools > Script editor.
*
* Once open, paste the code, adjust your desired sizes and run:
* `resizeToDesiredWidth`.
*
@nitobuendia
nitobuendia / hangman.py
Created July 15, 2019 11:38
Hangman game - implemented in Python
"""Runs a Hangman game that can be played via terminal.
Prerequisites:
1. Download the code as hangman.py into any folder.
2. Get a .txt file called 'words.txt' with all the words that you want to include in the game.
>> Example: https://github.com/dwyl/english-words
To play:
- Run `python hangman`
@nitobuendia
nitobuendia / app.py
Last active July 21, 2019 15:22
API Rest Server for Enviro+ (and sensor config for Home Assistant)
import flask
import reader
app = flask.Flask(__name__)
enviro_reader = reader.EnviroReader()
@app.route("/api/all")
def get_all_readings():