Skip to content

Instantly share code, notes, and snippets.

View lepture's full-sized avatar
💭
I'm working on Typlog.com now

Hsiaoming Yang lepture

💭
I'm working on Typlog.com now
View GitHub Profile
# coding: utf-8
from wand.image import Image
def resize(blob, min=300, max=1200):
image = Image(blob=blob)
width = image.width
height = image.height
if width < min:
@lepture
lepture / qq_download_ip
Last active December 22, 2015 06:58
deny QQ Download
# "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; QQDownload 713; .NET CLR 2.0.50727; InfoPath.2)"
# sudo iptables -I INPUT -j DROP -s [ip]
# row 2
180.96.74.17
182.140.168.14
119.188.77.8
111.30.135.16
113.142.38.15
115.236.140.15
SUBSTITUTE=`ps -A -o pid,rss,command | grep substitute | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
SUPERVISOR=`ps -A -o pid,rss,command | grep supervisord | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
GUNICORN=`ps -A -o pid,rss,command | grep gunicorn | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
MYSQL=`ps -A -o pid,rss,command | grep mysql | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
NGINX=`ps -A -o pid,rss,command | grep nginx | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
OTHER=`ps -A -o pid,rss,command | grep -v nginx | grep -v substitute | grep -v gunicorn | grep -v mysql | grep -v supervisord | grep -v grep | awk '{total+=$2}END{printf("%d", total/1024)}'`
printf "%-12s %3s MB\n" "Supervisor:" $SUPERVISOR
printf "%-12s %3s MB\n" "Substitute:" $SUBSTITUTE
printf "%-12s %3s MB\n" "Gunicorn:" $GUNICORN
printf "%-12s %3s MB\n" "Nginx:" $NGINX
from flask import Flask as _Flask
from flask.sessions import SecureCookieSessionInterface
class _SecureCookieSessionInterface(SecureCookieSessionInterface):
def save_session(self, app, session, response):
if not session.modified:
return
super(_SecureCookieSessionInterface, self).save_session(
app, session, response
@lepture
lepture / server.js
Last active December 20, 2015 05:09
var fs = require('fs');
var path = require('path');
var http = require('http');
var bind = 9090;
var basedir = '';
var server = http.createServer(function(req, res) {
if (req.method === 'POST') {
var d = new Date();
@lepture
lepture / sanitilize_link.py
Created July 15, 2013 06:34
Clean utm parameters on link.
# https://github.com/mozilla/unicode-slugify/blob/master/slugify/__init__.py
import re
import unicodedata
from django.utils.encoding import smart_unicode
# Extra characters outside of alphanumerics that we'll allow.
SLUG_OK = '-_~'
var md5 = require('md5');
const SUBSTITUTE_BASE = 'https://example.com/';
const SUBSTITUTE_SECRET = 'a secret';
html = html.replace(/<img[^>]*src=('|")(.*?)\1[^>]*>/g, function(img) {
var src = RegExp.$2;
var digest = md5.hmac(SUBSTITUTE_SECRET, src);
var newSrc = SUBSTITUTE_BASE + digest + '/' + encodeURIComponent(src);
return img.replace(src, newSrc);
});
@lepture
lepture / fix_orientation.py
Created July 1, 2013 10:56
Rotate it right for browser displaying images.
# coding: utf-8
def fix_orientation(img):
orientation = img.metadata.get('exif:Orientation', 1)
if orientation == 1:
# do nothing
return img
elif orientation == 2:
# vertical mirror
@lepture
lepture / manager.py
Last active December 17, 2015 10:39
upgrade python china
@manager.command
def sync():
from june.models import db, Account, Node, Topic, NodeStatus
users = Account.query.all()
nodes = Node.query.all()
for user in users:
print('save %i %s' % (user.id, user))
if not user.username:
username = user.email.split('@')[0]