Skip to content

Instantly share code, notes, and snippets.

@polikeiji
polikeiji / gist:8e3145aaac9b67fd3456
Last active August 29, 2015 14:02
Stringで[1..4]とかできるようにするextension
extension String {
subscript(range:Range<Int>) -> String {
return self[advance(self.startIndex, range.startIndex)..advance(self.startIndex, range.endIndex)]
}
}
var s = "abcdefghij"
s[1..4] // print "bcd"
s[1...4] // print "bcde"
@polikeiji
polikeiji / gist:c82e0cb021394dd8432a
Created July 10, 2014 09:41
サムネイル作成用のPHPクラス。GDベース。サムネイル画像をバイナリーデータとして返してくれる。
<?php
class ThumbnailUtility {
const TYPE_KEEP_RATIO = 0;
const TYPE_TRIM = 1;
public static function Resize(
$file_path_or_url,
$thumbnail_width = null,
@polikeiji
polikeiji / gist:8fcbdc88bb213257da9f
Last active August 29, 2015 14:03
S3用のrsync的なPythonスクリプト。botoが必要。excludeパターンの指定可。
# -*- coding: utf-8 -*-
"""
File Name: upload_s3.py
Usage: python upload_s3.py
"""
import sys
import os
import os.path
import fnmatch
@polikeiji
polikeiji / gist:1fd44508f7efff2e5074
Last active August 29, 2015 14:03
InstagramでOAuthする為に作ったクラス。結構昔だから、今はちゃんとしたSDKあるかも。
<?php
class InstagramOAuth {
const CLIENT_ID = Configuration::INSTAGRAM_CLIENT_ID;
const CLIENT_SECRET = Configuration::INSTAGRAM_CLIENT_SECRET;
const CALLBACK_URL = Configuration::INSTAGRAM_CALLBACK_URL;
public static function getAuthorizationURL($state = null, $scope = InstagramScope::BASIC, $is_implicit_authentication = false) {
$scope = $scope == null ? InstagramScope::BASIC : $scope;
$response_type = !$is_implicit_authentication ? 'code' : 'token';
@polikeiji
polikeiji / gist:92b9b99a5263c73c7874
Created July 12, 2014 19:37
Twitterのフォロワー一覧のプロフ画像の解像度をあげるブックマークレット
javascript: $(".ProfileCard-avatarImage").each(function() { $avatarImg = $(this); $avatarImg.attr("src", $avatarImg.attr("src").replace("_bigger", "_400x400")); });
@polikeiji
polikeiji / gist:2b6056610d6fc0bc55a7
Created July 28, 2014 08:23
gunicornのリバースプロキシ付きのnginx.conf
user www;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
@polikeiji
polikeiji / gist:dc39630eebf169333026
Created July 28, 2014 08:21
gunicorn起動用のupstartのinitスクリプト
description "[APP NAME] API Server"
start on runlevel [2345]
stop on runlevel [016]
respawn
script
su - www
source /home/www/[APP NAME]/virtualenv/bin/activate
@polikeiji
polikeiji / gist:f0f5b2b80610accb9665
Created August 27, 2014 03:58
knockout.jsでdropzone.jsを使う為のカスタムバインディング。
ko.bindingHandlers.dropzone =
init: (element, value_accessor) ->
passed_value = ko.unwrap value_accessor()
send_data = passed_value.data
$target = $(element).dropzone
url: passed_value.url
sending: (file, xhr, form_data) ->
for name, value of send_data
if typeof(value) == "function"
form_data.append name, value()
@polikeiji
polikeiji / override_print.py
Last active August 29, 2015 14:09
コンソールに上書き出力するPython用関数。
def override_print(message, line_length = 100):
if len(message) > line_length:
message = message[:line_length - 3] + "..."
sys.stdout.write((u"\r{0:<" + str(line_length) + "}").format(message))
sys.stdout.flush()
@polikeiji
polikeiji / gist:a00455c83fdbcdb8f110
Last active August 29, 2015 14:23
エクセルをJSONに直すPythonスクリプト
import xlrd
from collections import OrderedDict
import simplejson as json
excel_path = raw_input('input path> ')
json_path = raw_input('output json path> ')
wb = xlrd.open_workbook(excel_path)
sheets = OrderedDict()