-
Notifications
You must be signed in to change notification settings - Fork 1
/
hatena_bookmark_manager.py
61 lines (51 loc) · 1.71 KB
/
hatena_bookmark_manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# -*- coding: utf-8 -*-
import sha
import logging
from google.appengine.api import memcache
from google.appengine.api import urlfetch
from hatena_bookmark import HatenaBookmark
class HatenaBookmarkManager:
def __init__(self):
self.username, self.password = self.read_credential()
self.ttl = 60 * 60 * 24 # 1 day
self.ttl_negative = 60 * 5 # 5 minutes
def read_credential(self):
f = open("config/hatena.id")
try:
username = f.readline().strip()
password = f.readline().strip()
return (username, password)
finally:
f.close()
def create_title_key(self, url):
return "hatena_bookmark_title_" + sha.sha(url).hexdigest()
def get_title(self, url):
key = self.create_title_key(url)
value = memcache.get(key)
if value is None:
# MEMO: 1度だけ再試行する
try:
value = HatenaBookmark.get_title(url, self.username, self.password)
except urlfetch.DownloadError:
logging.info("retry download")
value = HatenaBookmark.get_title(url, self.username, self.password)
memcache.add(key, value, self.ttl)
return value
def create_summary_key(self, url):
return "hatena_bookmark_summary_" + sha.sha(url).hexdigest()
def get_summary(self, url):
key = self.create_summary_key(url)
value = memcache.get(key)
if value is None:
# MEMO: 1度だけ再試行する
try:
value = HatenaBookmark.get_summary(url)
except urlfetch.DownloadError:
logging.info("retry download")
value = HatenaBookmark.get_summary(url)
if value == (None, None):
ttl = self.ttl_negative
else:
ttl = self.ttl
memcache.add(key, value, ttl)
return value