forked from KMhook/istweb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py.bak
72 lines (64 loc) · 2.53 KB
/
views.py.bak
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
62
63
64
65
66
67
68
69
70
71
import sys
# Create your views here.
from annoying.decorators import render_to
from django.template import Context
from contacts.models import Contact
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
try:
from douban.service import DoubanService
from douban.client import OAuthClient
except ImportError, e:
print e
print 'please install douban-python'
sys.exit(0)
class DoubanBooks(object):
def __init__(self):
self.HOST = 'http://www.douban.com'
self.API_KEY = "0ff1b8ce70b305ab2fd52a6b52191101"
self.SECRET = "47c2cb706c4ec51d"
self.reco_books = []
self.subjects = []
self.reco = []
self.href = []
self.service = DoubanService(self.API_KEY, self.SECRET)
def get_reco(self):
member = Contact.objects.all()
for tmp in member:
feed_read = self.service.GetMyCollection('/people/%s/collection' % tmp.douban_id, 'book', 'IST','read')
self.get_book(feed_read, tmp.fullname)
feed_reading = self.service.GetMyCollection('/people/%s/collection' % tmp.douban_id, 'book', 'IST','reading')
self.get_book(feed_reading, tmp.fullname)
feed_wish = self.service.GetMyCollection('/people/%s/collection' % tmp.douban_id, 'book', 'IST','wish')
self.get_book(feed_wish, tmp.fullname)
def get_book(self, feed, fullname):
for entry in feed.entry:
book_href = entry.link[1].href
self.href.append(book_href[21:])
subject_id = book_href[35:]
if subject_id not in self.subjects:
book_href = book_href[21:]
bookfeed = self.service.GetBook(book_href)
self.subjects.append(subject_id)
self.reco.append({subject_id: (bookfeed, [fullname])})
continue
else:
for tmp in self.reco:
if tmp.has_key(subject_id):
if fullname not in tmp[subject_id][1]:
tmp[subject_id][1].append(fullname)
continue
@render_to('recommentation/index.html')
def index(request):
page = request.GET.get('page')
reco_books = DoubanBooks()
reco_books.get_reco()
if not page:
page = 1
paginator = Paginator(reco_books.reco, 5)
try:
pages = paginator.page(page)
except PageNotAnInteger:
pages = paginator.page(1)
except EmptyPage:
pages = paginator.page(paginator.num_pages)
return {'books': pages.object_list, 'page': pages}