下記がコードサンプル、create_indexは忘れずに。
#!/usr/bin/env python
import binascii
import struct
import datetime
import hashlib
rom pymongo import Connection
from time import sleep
reconnect_times = 10
reconnect_counter = 0
dbsetting = {'dbhost':'localhost','dbname':'shorturldb'}
def con(**kwargs):
global reconnect_counter
global reconnect_times
if 'host' not in kwargs:
kwargs['host'] = dbsetting['dbhost']
dbname = dbsetting['dbname']
try:
_con = Connection(**kwargs)
return eval('_con.'+dbname)
except Exception, e:
sleep(0.1)
print('Fail to excute %s [%s]' % ( __name__, e))
if reconnect_counter == reconnect_times:
reconnect_counter = reconnect_counter + 1
con(**kwargs)
def save_hash(url, length=5):
base32 = [
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y','z',
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9'
]
hex = hashlib.md5(url).hexdigest()
hexLen = len(hex)
subHexLen = hexLen/8
output = []
mydb = con()
for i in range(0, subHexLen):
subHex = hex[i*8:(i*8)+8]
num = 0x3FFFFFFF & int('0x'+subHex, 0)
hash = '';
for j in range(0,length):
val = 0x0000003D & num
hash = hash + base32[val]
num = num >> length
r = mydb.shorturl.find_one({'i':{'$in':[hash,url]}})
if not r:
insert = {'i':[hash,url],'p':datetime.datetime.utcnow()}
mydb.shorturl.insert(insert)
return hash
elif r and r['i'][1] == url:
return r['i'][0]
return None
def get_hash(url):
mydb = con()
r = mydb.shorturl.find_one({'i':{'$in':[url]}})
if r:
return r['i'][0]
return None
#!/usr/bin/env python
import binascii
import struct
import datetime
import hashlib
rom pymongo import Connection
from time import sleep
reconnect_times = 10
reconnect_counter = 0
dbsetting = {'dbhost':'localhost','dbname':'shorturldb'}
def con(**kwargs):
global reconnect_counter
global reconnect_times
if 'host' not in kwargs:
kwargs['host'] = dbsetting['dbhost']
dbname = dbsetting['dbname']
try:
_con = Connection(**kwargs)
return eval('_con.'+dbname)
except Exception, e:
sleep(0.1)
print('Fail to excute %s [%s]' % ( __name__, e))
if reconnect_counter == reconnect_times:
reconnect_counter = reconnect_counter + 1
con(**kwargs)
def save_hash(url, length=5):
base32 = [
'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o',
'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y','z',
'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y','Z',
'0', '1', '2', '3', '4',
'5', '6', '7', '8', '9'
]
hex = hashlib.md5(url).hexdigest()
hexLen = len(hex)
subHexLen = hexLen/8
output = []
mydb = con()
for i in range(0, subHexLen):
subHex = hex[i*8:(i*8)+8]
num = 0x3FFFFFFF & int('0x'+subHex, 0)
hash = '';
for j in range(0,length):
val = 0x0000003D & num
hash = hash + base32[val]
num = num >> length
r = mydb.shorturl.find_one({'i':{'$in':[hash,url]}})
if not r:
insert = {'i':[hash,url],'p':datetime.datetime.utcnow()}
mydb.shorturl.insert(insert)
return hash
elif r and r['i'][1] == url:
return r['i'][0]
return None
def get_hash(url):
mydb = con()
r = mydb.shorturl.find_one({'i':{'$in':[url]}})
if r:
return r['i'][0]
return None
