Skip to content

Instantly share code, notes, and snippets.

@lijameshao
lijameshao / youtube-search.py
Created February 5, 2023 03:00 — forked from stvar/youtube-search.py
Find YouTube channel IDs by custom URLs or user names
#!/usr/bin/python3
# Copyright (C) 2020 Stefan Vargyas
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
@lijameshao
lijameshao / sqlalchemy_session_routing_alternative.py
Last active January 22, 2022 01:30
Route SQLAlchemy session based on engine
# Option 2
def get_session_alternative(read_only=False):
if read_only:
SessionLocal = sessionmaker(bind=engine_ro)
else:
SessionLocal = sessionmaker(bind=engine)
session = SessionLocal()
try:
yield session
finally:
@lijameshao
lijameshao / sqlalchemy_session_routing.py
Last active December 8, 2023 13:26
Route SQLAlchemy session to based on SQL query
import sqlalchemy
from sqlalchemy.sql import Select, Update, Delete, Insert
from sqlalchemy.orm import Session, sessionmaker
from sqlalchemy.ext.declarative import DeclarativeMeta, declarative_base
Base: DeclarativeMeta = declarative_base()
# Hack to get around engines need type DeclarativeBase or MappedClass or Table but we are targeting the same Base
# and we want to route on a query level
ReadBase = Base
@lijameshao
lijameshao / rds_proxy_endpoint.yml
Created January 12, 2022 19:38
CloudFormation for rds proxy endpoint
RDSDBProxyEndpoint:
Type: AWS::RDS::DBProxyEndpoint
DependsOn:
- RDSDBProxy
Properties:
DBProxyEndpointName: ReadEndpoint
DBProxyName: !Ref RDSDBProxy
TargetRole: READ_ONLY
VpcSecurityGroupIds:
- !Ref YourDBClusterSecurityGroup
@lijameshao
lijameshao / rds_db_proxy.yml
Last active January 12, 2022 19:34
CloudFormation example to create a RDS proxy
RDSDBProxyRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Principal:
Service:
- "rds.amazonaws.com"
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Template to create a static S3 website with CloudFront distribution.",
"Parameters" : {
"S3BucketName" : {
"Type" : "String",
"Description" : "The name of your S3 bucket"
},
@lijameshao
lijameshao / medium-web-dev-streaming-chart.html
Created September 30, 2020 05:52
HTML for streaming chart
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/moment@2.27.0/min/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/chart.js@2.9.3"></script>
<script type="text/javascript" src="chartjs-plugin-streaming-min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pusher/4.1.0/pusher.js"></script>
<script defer type="text/javascript" src="coinbase-minimal.js"></script>
</head>
@lijameshao
lijameshao / medium-web-dev-create-chart-with-streaming-plugin.js
Created September 30, 2020 05:49
Create chart with streaming plugin
let chart = new Chart(ctx, {
type: 'line',
data: {
datasets: [{
data: [],
label: 'Bid',
borderColor: 'rgb(0, 255, 0)',
backgroundColor: 'rgba(0, 255, 0, 0.5)',
fill: false,
lineTension: 0
const canvasDiv = document.querySelector('#canvas-div');
let canvas = document.createElement("canvas");
canvasDiv.appendChild(canvas);
let ctx = canvas.getContext('2d');
@lijameshao
lijameshao / medium-web-dev-parse-wss-push-to-buf.js
Last active September 30, 2020 05:43
Parse Coinbase websocket messages
streamer.onmessage = (message) => {
let data = JSON.parse(message.data);
if (data['type'] === 'heartbeat') {
let time = data.time;
console.log('Heartbeat: ' + time);
};