Exemplo n.º 1
0
 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)
Exemplo n.º 2
0
 def GET(self):
     q = web.input().get('q', '')
     html_header()
     search_panel(q)
     if q:
         service = DoubanService(api_key=API_KEY, secret=SECRET)
         feed = service.SearchMovie(q)
         print '<div class="obss" style="margin-top:20px">'
         for movie in feed.entry:
             print '<dl class="obs"><dt>'
             print '<div class="gact"><a href="%s/collection?sid=%s">想看</a></div>' % (
                 PREFIX, movie.id.text)
             print '<a href="%s" title="%s"><img src="%s" class="m_sub_img"/></a>' % (
                 movie.GetAlternateLink().href, movie.title.text,
                 ((len(movie.link) >= 3) and movie.link[2].href) or '')
             print '</dt><dd>'
             print '<a href="%s">%s</a>' % (movie.GetAlternateLink().href,
                                            movie.title.text)
             print '</dd>'
             print '</dl>'
         print '</div>'
     html_footer()
Exemplo n.º 3
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
Exemplo n.º 4
0
# coding: utf-8

import web
from douban.service import DoubanService
from setting import setting

render = setting.render
API_KEY = '0516527552aeabd50f42f75ff80c037f'
SECRET = '438ad54fabc47fea'

client = DoubanService(API_KEY, SECRET)


class Main:
    def GET(self):
        return render.home('hello')
Exemplo n.º 5
0
        request_key = web.input().get('oauth_token', False)
        session = Session()
        if request_key:
            try:
                token = session.query(Token).filter_by(key=request_key).one()
            except exc.NoResultFound, e: 
                return "request token: %s not found" %request_key
        else:
            return web.internalerror()
        client = OAuthClient(key=config.API_KEY, secret=config.API_SECRET)
        access_key, access_secret = client.get_access_token(request_key, token.secret)
        if not access_key or not access_secret:
            return "Error: access_key: %s access_token: %s" %(access_key, access_secret)
        
	    print "access_key %s, access_secret %s" %(access_key, access_secret)
        service = DoubanService(api_key=config.API_KEY, secret=config.API_SECRET)
        
        if not service.ProgrammaticLogin(access_key, access_secret):
            return "service login failed"

        try:
            people = service.GetAuthorizedUID('/people/@me')
        except:
            return web.internalerror()
        try:
            authen = session.query(Authen).filter_by(hash=token.hash).one()
        except:
            return web.internalerror()
        if not authen.jid:
            return "jid not found"
Exemplo n.º 6
0
    def GET(self):
        sid = web.input().get('sid', '')
        if not sid:
            print 'no sid'
            return

        client = OAuthClient(key=API_KEY, secret=SECRET)
        cookies = web.cookies()
        access_key = cookies.get('access_key')
        access_secret = access_tokens.get(access_key)

        if not access_key or not access_secret:
            request_key = web.input().get('oauth_token', '')
            request_secret = request_tokens.get(request_key)
            if request_key and request_secret:
                try:
                    access_key, access_secret = \
                        client.get_access_token(request_key, request_secret)
                    if access_key and access_secret:
                        # store user access key in cookie,
                        # not accessable by other people
                        web.setcookie('access_key', access_key)
                        access_tokens[access_key] = access_secret
                except Exception:
                    access_token = None
                    print '获取用户授权失败'
                    return
            else:
                client = OAuthClient(key=API_KEY, secret=SECRET)
                key, secret = client.get_request_token()
                if key and secret:
                    request_tokens[key] = secret
                    url = client.get_authorization_url(
                        key,
                        secret,
                        callback=HOST + PREFIX + '/collection?sid=' + sid)
                    web.tempredirect(url)
                    return
                else:
                    print '获取 Request Token 失败'
                    return

        service = DoubanService(api_key=API_KEY, secret=SECRET)
        movie = service.GetMovie(sid)
        html_header()
        search_panel()
        print '<h2>你希望收藏电影: %s</h2>' % (movie.title.text)
        print '<div class="obss" style="margin-top:20px">'
        print '<dl class="obs"><dt>'
        print '<a href="%s" title="%s"><img src="%s" class="m_sub_img"/></a>' % (
            movie.GetAlternateLink().href, movie.title.text,
            movie.link[2].href)
        print '</dt><dd>'
        print '<a href="%s">%s</a>' % (movie.GetAlternateLink().href,
                                       movie.title.text)
        print '</dd>'
        print '</dl>'
        if access_key and access_secret:
            if service.ProgrammaticLogin(access_key, access_secret):
                try:
                    entry = service.AddCollection('wish', movie, tag=['test'])
                    if entry:
                        print '<span>已添加到你的收藏</span>'
                    else:
                        print '<span>添加收藏失败</span>'
                except Exception:
                    print '<span>添加收藏失败, 授权失效</span>'
                    del access_tokens[access_key]
                    web.setcookie('access_key', '', 0)
        else:
            print '<span>无法添加收藏,可能你因为你没有授权这个应用访问你在豆瓣的数据</span>'
        print '</div>'
        html_footer()
Exemplo n.º 7
0
# -*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import json
from urlparse import urlparse

from gdata.service import RequestError
from douban.service import DoubanService
from diandou.settings import DOUBAN_API_KEY, DOUBAN_SECRET

client = DoubanService(api_key=DOUBAN_API_KEY, secret=DOUBAN_SECRET)


def _get_attribute(attributes, name):
    return [att.text for att in attributes if att.name == name]


def get_douban_movie(douban_id):
    uri = "/movie/subject/{0}".format(douban_id)

    try:
        return client.GetMovie(uri)
    except RequestError as e:
        err = json.loads(json.dumps(e.message))
        return None


def douban_search(text_query, start_index=0, max_results=10):
    feed = client.SearchMovie(text_query,
                              start_index=start_index,