Skip to content

Instantly share code, notes, and snippets.

@thevickypedia
thevickypedia / proxy.py
Last active April 5, 2024 22:16
Basic proxy service using FastAPI (synchronous)
import logging
from http import HTTPStatus
import httpx
import uvicorn.logging
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.routing import APIRoute
TARGET_URL = "http://127.0.0.1:8080" # Change this to the URL you want to proxy requests to
CLIENT = httpx.Client()
@thevickypedia
thevickypedia / proxy.py
Last active April 5, 2024 22:16
Basic proxy service using FastAPI (asynchronous)
import logging
from http import HTTPStatus
import httpx
import uvicorn.logging
from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.routing import APIRoute
TARGET_URL = "http://127.0.0.1:8080" # Change this to the URL you want to proxy requests to
@thevickypedia
thevickypedia / nginx_static.md
Last active March 23, 2024 01:37
Error page for Nginx

Preferred File Location

/var/www/html

Assign www-data as owner

sudo chown www-data:www-data /var/www/html/error.html
@thevickypedia
thevickypedia / linux-service.md
Last active March 23, 2024 13:24
Run python script as a daemon service in Linux

Create a service

/etc/systemd/system/pyfilebrowser.service
Contents
@thevickypedia
thevickypedia / downloader.py
Created January 18, 2024 12:10
File downloader using multi-threading with progress bar
import math
import os
import time
from threading import Thread
from typing import Union
import inflect
import requests
import tqdm
@thevickypedia
thevickypedia / timed_cache.py
Created December 4, 2023 20:43
Timed caching using LRU algorithm
import functools
import time
def timed_cache(max_age, maxsize=128, typed=False):
"""Least-recently-used cache decorator with time-based cache invalidation.
See Also:
- ``lru_cache`` takes all params of the function and creates a key
- If even one key is changed, it will map to new entry thus refreshed.
@thevickypedia
thevickypedia / requirements.py
Created December 2, 2023 18:29
Update requirements.txt to what's currently available in pip freeze
import os
import shutil
import subprocess
import time
with open('requirements.txt') as file:
requirements = file.readlines()
tmp_name = f'requirements_{int(time.time())}.txt'
os.rename('requirements.txt', tmp_name)
@thevickypedia
thevickypedia / sound.py
Created November 29, 2023 15:04
Record and play audio using pyaudio module
import logging
import math
import pyaudio
_logger = logging.getLogger(__name__)
_logger.addHandler(logging.StreamHandler())
_logger.setLevel(logging.DEBUG)
@thevickypedia
thevickypedia / workflows.py
Last active October 23, 2023 18:46
Delete GitHub workflows using API
import os
from typing import List
import requests
SESSION = requests.Session()
headers = {
'Accept': 'application/vnd.github+json',
'Authorization': f"Bearer {os.getenv('GIT_TOKEN')}",
@thevickypedia
thevickypedia / async_telegram_polling.py
Last active September 11, 2023 07:03
Asynchronous calls for long polling Telegram API - A better broken state
import asyncio
from typing import Dict
from urllib.parse import urljoin
import aiohttp
from pydantic import HttpUrl
from pydantic_settings import BaseSettings
class Settings(BaseSettings):