Skip to content

Instantly share code, notes, and snippets.

@socrateslee
socrateslee / wsl-proxychains.sh
Created December 23, 2023 11:47
Use proxychains in wsl
#!/bin/bash
# wsl's host ip address may change every time it reboots,
# this is a problem if you map your host address as proxy.
# Use the script to generate a new config of proxychains
# each time proxychains runs.
# put an entry
# http wsl-host 3128
# in your /etc/proxychains.conf
# Copy the script as ./local/bin/wsl-proxychains.sh
# run your program with proxychains like
@socrateslee
socrateslee / robotparserpatched.py
Last active September 29, 2023 13:16
A monkey patch for urllib.robotparser to support * and $ in robots.txt
'''
A monkey patch for urllib.robotparser to support * and $ in robots.txt.
'''
import re
import urllib.parse
from urllib.robotparser import RobotFileParser, Entry, RuleLine
def get_robots_pattern(path):
ending = '.*?'
@socrateslee
socrateslee / get_word_list.py
Created May 21, 2020 04:17
Get a word list from local man file
import re
import gzip
def get_word_list(f, pattern=None):
pattern = pattern if pattern else r'[a-zA-Z0-9-_]+'
l = re.findall(pattern, gzip.decompress(open(f, 'rb').read()).decode('utf-8'))
return list(set(l))
# Example
@socrateslee
socrateslee / jd_union_api.py
Last active November 1, 2023 03:31
京东联盟开放平台API的一个通用的Client封装
'''
京东联盟开放平台API的一个通用的Client封装。
京东联盟开放平台的文档详见 https://union.jd.com/openplatform
涉及到签名方法的文档见 https://union.jd.com/helpcenter/13246-13247-46301
Example:
client = JdApiClient("<YOUR_APP_KEY>", "<YOUR_SECRET_KEY>")
resp = client.call("jd.union.open.goods.promotiongoodsinfo.query",
{'skuIds':'12072066'})
print(resp.json())
@socrateslee
socrateslee / wider-yuque.js
Last active January 9, 2020 11:23
Wider Yuque
// ==UserScript==
// @name Wider Yuque
// @namespace https://gist.github.com/socrateslee/751c6670c59d5b9cdbd8428fab493375
// @version 0.1
// @description Let yuque a bit wider on browser screen.
// @author github.com/socrateslee
// @include https://*.yuque.com/*
// @grant none
// ==/UserScript==
@socrateslee
socrateslee / traced_obj.py
Created May 23, 2019 07:20
An object stores the change history of its attributes, the object can be use to store the hierarchies of different levels of configurations.
'''
An object stores the change history of its attributes,
the object can be use to store the hierarchies of different
levels of configurations.
Example
-------
# Set up config object
config = TracedObj()
@socrateslee
socrateslee / script.js
Last active May 6, 2019 14:53
A Tampermonkey userscripts for better display of data.stats.gov.cn
// ==UserScript==
// @name better data.stats.gov.cn
// @namespace https://gist.github.com/socrateslee/69169c58be9cf2e0e3019b09b680461b
// @version 0.1
// @description Inject css for better display of data.stats.gov.cn
// @author https://github.com/socrateslee
// @include http://data.stats.gov.cn/easyquery.htm*
// @grant none
// ==/UserScript==
@socrateslee
socrateslee / compact_json_dumps.py
Last active September 15, 2017 14:32
pretty and compact json dumps for python
import re
import json
def compact_json_dumps(*sub, **kw):
'''
A wrapper of json.dumps support an optional compact_length parameter.
compact_length take effects when indent > 0, will return a more compact
indented result, i.e., pretty and compact json dumps.
'''
@socrateslee
socrateslee / static-serve.go
Created June 18, 2014 14:56
Serving static files in current directory with golang. Simply a "python -m SimpleHTTPServer [PORT]" replacement written in golang. Use it as "static-serve [PORT]"
package main
import (
"net/http"
"os"
"fmt")
func main() {
port := "8080"
if len(os.Args) >= 2 {
@socrateslee
socrateslee / functor.py
Created May 28, 2014 06:23
A simple wrapper make a class become a functor.
'''
A simple wrapper make a class become a functor,
the class need a __call__ function. The (*sub,
**kwargs) will be passed for the __init__ function,
and the name of class will be the name of function
object.
Example
-------
@functor(*sub, **kwargs)