Skip to content

Instantly share code, notes, and snippets.

View quietcricket's full-sized avatar

Shang Liang quietcricket

  • Singapore
View GitHub Profile
@quietcricket
quietcricket / gist:2521037
Created April 28, 2012 18:20
Get IP Address, C/C++
string getIPAddress(){
string ipAddress="Unable to get IP Address";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
@quietcricket
quietcricket / gist:1593632
Created January 11, 2012 07:58
Fuzzy string match objective-c (Levenshtein Distance Algorithm)
-(float)compareString:(NSString *)originalString withString:(NSString *)comparisonString
{
// Normalize strings
[originalString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[comparisonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
originalString = [originalString lowercaseString];
comparisonString = [comparisonString lowercaseString];
// Step 1 (Steps follow description at http://www.merriampark.com/ld.htm)
@quietcricket
quietcricket / Global keyboard hook for OSX
Created January 8, 2014 07:42
OSX global keyboard hook. Requires root privileges.
// alterkeys.c
// http://osxbook.com
//
// Complile using the following command line:
// gcc -Wall -o alterkeys alterkeys.c -framework ApplicationServices
//
// You need superuser privileges to create the event tap, unless accessibility
// is enabled. To do so, select the "Enable access for assistive devices"
// checkbox in the Universal Access system preference pane.
@quietcricket
quietcricket / abspath_pyinstaller.py
Created August 24, 2023 02:27
pyinstaller can package a python app into a single executable file. When this executable file runs, it's first extracted into a temporary location then the python script is called. The usual loading of files with relative path would not work. The following code is found online to work around this issue.
def abspath(filename):
# looking for magic word _MEIPASS
bundle_dir = getattr(sys, '_MEIPASS', os.path.abspath(os.path.dirname(__file__)))
return os.path.abspath(os.path.join(bundle_dir, filename))
@quietcricket
quietcricket / pip_cert_fix.md
Created August 24, 2023 02:21
Fix pip install certificate error

For Windows 💻 only

Because the computer is behind a corporate firewall, with additional firewall software installed, pip install always fails and gives a CERTIFICATE_VERIFY_FAILED error. It's either the access to the to the cert file is blocked or the cert is not recognized.

The best way to solve this problem I found so far is to add a pip.ini file in ~/AppData/Roaming/pip/pip.ini with the following content

[global]
@quietcricket
quietcricket / transfer-domain-from-aws-to-gandi.md
Created October 17, 2022 01:57
Transfer Domain from AWS to Gandi
  • Unlock domain in AWS Route53
  • Copy authentication code
  • Start transfer process in Gandi
  • Paste authentication code
  • AWS will send a approval link to the email address registered for the domain
  • If the approval link is not triggered, domain transfer will fail
  • The last step is very important
@quietcricket
quietcricket / upload_s3_boto3.py
Last active February 23, 2021 02:23
Upload file to aws s3 with boto3
import mimetypes
def upload_s3(key, fileobj, bucket_name, cache=999999):
"""
Lots of problems with new boto3 library. Many functions are not documented or wrongly documented
1. s3.Object.metadata, s3.Object.cache_control, s3.Object.content_type: are read only, gotcha
2. If you set it with `ExtraArgs={'Metadata':{...}}`, wrong again. Cache-Control will become amz-x-cache-control,
not what you wanted
3. If you set it with `ExtraArgs={'Cache-Control':'max-age=999'}`, wrong again! They need too be CacheControl and ContentType
"""
@quietcricket
quietcricket / ffmpeg-commands.sh
Created January 28, 2021 08:45
FFMPEG Commands
# Making gif with PNG files
# Generate palette
ffmpeg -f image2 -i frame%04d.png -vf scale=900:-1:sws_dither=ed,palettegen palette.png
# Join png files using the palette generate
ffmpeg -i frame%04d.png -i palette.png -filter_complex "fps=12,scale=900:-1:flags=lanczos[x];[x][1:v]paletteuse" video.gif
@quietcricket
quietcricket / add_firebase_user.py
Created February 6, 2020 08:36
Add user to firebase's authentication using email and password. Please note that this is not firebase admin script to add user to work on a firebase project
import hashlib
import hmac
import base64
import secrets
import json
import os
print("Create a user account with email and the password is randomly generated.")
email = input("Email: ")
@quietcricket
quietcricket / firebase_livereload.py
Last active December 31, 2019 07:49
Firebase local server with livereload. Requires python_livereload
import json
from livereload import Server
from tornado.web import RedirectHandler, StaticFileHandler
settings = json.load(open('.firebaserc'))
server_url = 'https://%s.web.app/__/' % settings['projects']['default']
class NoCacheHandler(StaticFileHandler):
def set_extra_headers(self, path):