Skip to content

Commit

Permalink
Refactor recommendation
Browse files Browse the repository at this point in the history
  • Loading branch information
KMhook committed Mar 5, 2012
1 parent 7127e0e commit 4214cba
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 127 deletions.
Binary file modified celerybeat-schedule
Binary file not shown.
2 changes: 1 addition & 1 deletion mysite.pid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5060
2999
48 changes: 48 additions & 0 deletions recommendation/doubanbooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from recommendation.models import Book
from contacts.models import Contact
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()
book_states = ['read', 'reading', 'wish']
for tmp_member in member:
for tmp_states in book_states:
feed_read = self.service.GetMyCollection('/people/%s/collection' % tmp_member.douban_id, 'book', 'IST', tmp_states)
self.get_book(feed_read, tmp_member.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((bookfeed, [fullname], subject_id))
continue
else:
for tmp in self.reco:
if tmp[2] == subject_id:
if fullname not in tmp[1]:
tmp[1].append(fullname)
continue

40 changes: 40 additions & 0 deletions recommendation/doubanbooks.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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((bookfeed, [fullname], subject_id))
continue
else:
for tmp in self.reco:
if tmp[2] == subject_id:
if fullname not in tmp[1]:
tmp[1].append(fullname)
continue

89 changes: 19 additions & 70 deletions recommendation/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,35 @@
from celery.task.schedules import crontab

import sys

# Create your views here.
from recommendation.models import Book
from contacts.models import Contact
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)
# Create your views here.

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((bookfeed, [fullname], subject_id))
continue
else:
for tmp in self.reco:
if tmp[2] == subject_id:
if fullname not in tmp[1]:
tmp[1].append(fullname)
continue
from doubanbooks import DoubanBooks

@periodic_task(run_every=crontab(hour="*", minute="0", day_of_week="*"))
def test():
douban_books = DoubanBooks()
douban_books.get_reco()

for reco in douban_books.reco:
bid = reco[0].link[1].href
title = reco[0].title.text
#author = reco[0].author
img_url = reco[0].link[2].href
recommender = ''
author = ''
tmp_book = Book()
tmp_book.book_id = reco[0].link[1].href
tmp_book.book_title = reco[0].title.text
tmp_book.book_img_url = reco[0].link[2].href
#tmp_book.book_recommender = ''
#tmp_book.book_author = ''
for person in reco[0].author:
author += person.name.text + ', '
author = author[0:-2]
tmp_book.book_author += person.name.text + ', '
tmp_book.book_author = tmp_book.book_author[0:-2]
for person in reco[1]:
recommender += ' ' + person
print recommender
books = Book.objects.filter(book_id = bid)
tmp_book.book_recommender += ' ' + person
print tmp_book.book_recommender

books = Book.objects.filter(book_id = tmp_book.book_id)
if not books:
book = Book(book_title = title,
book_author = author,
book_img_url = img_url,
book_id = bid,
book_recommender = recommender)
book.save()
tmp_book.save()
else:
book = books[0]
book.book_recommender = recommender
book.book_author = author
book.save()
book = books[0]
book.book_recommender = tmp_book.book_recommender
book.save()

77 changes: 21 additions & 56 deletions recommendation/tasks.py~
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,37 @@ from celery.task.schedules import crontab
import sys

# Create your views here.
from annoying.decorators import render_to
from django.template import Context
from recommendation.models import Book
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((bookfeed, [fullname]))
continue
else:
for tmp in self.reco:
if tmp.has_key(subject_id):
if fullname not in tmp[1]:
tmp[1].append(fullname)
continue
from doubanbooks import DoubanBooks

@periodic_task(run_every=crontab(hour="*", minute="*", day_of_week="*"))
def test():
douban_books = DoubanBooks()
douban_books.get_reco()
for reco in douban_books.reco:
bid = reco.id.text
title = reco.title.text
author = reco.author
img_url = reco.link[2].href
bid = reco[0].link[1].href
title = reco[0].title.text
#author = reco[0].author
img_url = reco[0].link[2].href
recommender = ''
author = ''
for person in reco[0].author:
author += person.name.text + ', '
author = author[0:-2]
for person in reco[1]:
recommender += ' ' + person
print recommender
books = Book.objects.filter(book_id = bid)
if not books:
book = Book(book_title = title,
book_author = author,
book_img_url = img_url,
book_id = bid)
book_id = bid,
book_recommender = recommender)
book.save()
else:
book = books[0]
book.book_recommender = recommender
book.book_author = author
book.save()

0 comments on commit 4214cba

Please sign in to comment.