def douban_cli(): from douban_client import DoubanClient client = DoubanClient(API_KEY, API_SECRET, redirect_uri, SCOPE) if not douban_token: print client.authorize_url # manually get auth code client.auth_with_code(douban_token) return client
class MyDouban: def __init__(self): self.clent = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) self.clent.auth_with_token(TOKEN) def search_movie(self, keyword): q = "" for kw in keyword: q += kw + "" return self.clent.movie.search(q)
class ClientHelper(): # Initiate the Douban API v2.0 client with OAuth. def __init__( self ): key = '022e300c79c18fc7068a90256d44af55' secret = '11c8bcbac80e8085' callback = 'http://www.douban.com' scope = 'douban_basic_common,community_basic_user' self.client = DoubanClient( key, secret, callback, scope ) self.client.auth_with_token( '23f8580710490aad6be9081530db2007' )
class MyDouban: def __init__(self): self.clent = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) self.clent.auth_with_token(TOKEN) def search_movie(self, keyword): q = '' for kw in keyword: q += kw + '' return self.clent.movie.search(q)
def get(self,code): douban = DoubanClient(API_KEY, SECRET, 'http://book.douban.com',SCOPE) douban.auth_with_code(code) token = douban.access_token.token print token duser = douban.user.me print duser user = connection.User() user['id']=int(duser['uid']) user['password'] = token user.save() return token
def douban_cli(): from douban_client import DoubanClient API_KEY = '09e7e2ad76917ac81db7a80863b47058' API_SECRET = '42764a7149130882' redirect_uri = 'http://54.250.166.126/spam/auth' SCOPE = 'douban_basic_common,movie_basic_r' client = DoubanClient(API_KEY, API_SECRET, redirect_uri, SCOPE) if not douban_token: print client.authorize_url # manually get auth code client.auth_with_code(douban_token) return client
def get_client(): from douban_client import DoubanClient API_KEY = os.environ.get('DOUBAN_API_KEY') API_SECRET = os.environ.get('DOUBAN_API_SECRET') your_redirect_uri = '' SCOPE = 'bouban_basic_common, community_basic_user, shuo_basic_r, shuo_basic_w' TOKEN_CODE = os.environ.get('DOUBAN_TOKEN_CODE') client = DoubanClient(API_KEY, API_SECRET, your_redirect_uri, SCOPE) client.auth_with_token(TOKEN_CODE) return client
def get_client(): client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) token = TOKEN if token: client.auth_with_token(token) else: print 'Go to the following link in your browser:' print client.authorize_url code = raw_input('Enter the verification code and hit ENTER when you\'re done:') client.auth_with_code(code) print client.client.token return client
def get_client(): client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) token = TOKEN if token: client.auth_with_token(token) else: print "Go to the following link in your browser:" print client.authorize_url code = raw_input("Enter the verification code and hit ENTER when you're done:") client.auth_with_code(code) print "token code:", client.token_code print "refresh token code:", client.refresh_token_code return client
def main(): key = '022e300c79c18fc7068a90256d44af55' secret = '11c8bcbac80e8085' callback = 'http://www.douban.com' scope = 'douban_basic_common,community_basic_user,book_basic_r' client = DoubanClient( key, secret, callback, scope ) # Following is to get the token at first time print 'Go to the following link in your browser:' print client.authorize_url code = raw_input( 'Enter the verification code:' ) client.auth_with_code( code ) print client.access_token.token # Following is to auth with the token already got # client.auth_with_token( '2090de6f59fea80a61b29b4e0dfbf81e' ) print client.user.me
def get_douban_client(app_config): scope = 'douban_basic_common,movie_basic_r,movie_advance_r' client = DoubanClient( app_config['douban_app_key'], app_config['douban_app_secret'], app_config['douban_redirect_url'], scope) return client
def login(self, email=None, password=None, token=None): if not self.client: self.client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) if token: self.client.auth_with_token(token) self.client.auth_with_token(token) else: self.client.auth_with_password(email, password) self.user.refresh_token = self.client.refresh_token_code self.user.uid = self.client.user.me['uid'] self.is_login = True
def new(): client = DoubanClient(API_KEY, API_SECRET, CALLBACK, SCOPE) code = request.form.get('code') image = request.files.get('image') comment = request.form.get('comment', '') hash1 = request.form.get('hash[new1]', '') hash2 = request.form.get('hash[new2]', '') hash3 = request.form.get('hash[new3]', '') if image: fname = '/tmp/1.jpg' image.save(fname) hash_text = hash_name([hash1, hash2, hash3]) text = "%s %s" % (comment, hash_text) text = text.encode("utf-8") #try: client.auth_with_code(code) client.miniblog.new(text, image=open(fname)) #except DoubanError: # return redirect('/login') return "Upload OK" else: return "Error"
def register(self, auth_code): """douban client for note4st user_json_file - solved on register """ self.client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) self.client.auth_with_code(auth_code) self.user.registered( self.client.user.me, self.client.user.access_token.token, self.client.user.access_token.refresh_token, self.client.user.access_token.expires_in, self.client.user.access_token.expires_at, ) self.user.save2file(self.user.name) self.is_login = True
def get_client_v2(): client = DoubanClient(key, secret, callback, SCOPE) if expires_at: expires_time = int(expires_at) if token: now_time = int(time.time()) remaining_seconds = expires_time - now_time #print 'remains: ' + str(remaining_seconds) if remaining_seconds > 3600: client.auth_with_token(token) else: client.refresh_token(refresh_token) config.set('INFO', 'token', client.token_code) config.set('INFO', 'refresh_token', client.refresh_token_code) config.set('INFO', 'expires_at', client.expires_at) with open('test_config.cfg', 'wb') as configfile: config.write(configfile) else: print_('Go to the following link in your browser:') print_(client.authorize_url) code = input( 'Enter the verification code and hit ENTER when you\'re done:') client.auth_with_code(code) print_('token code:', client.token_code) print_('refresh token code:', client.refresh_token_code) print_('expires at:', client.expires_at) print type(client.expires_at) config.set('INFO', 'token', client.token_code) config.set('INFO', 'refresh_token', client.refresh_token_code) config.set('INFO', 'expires_at', client.expires_at) with open('test_config.cfg', 'wb') as configfile: config.write(configfile) #pdb.set_trace() return client
def get_client_v2(): client = DoubanClient(key, secret, callback, SCOPE) if expires_at: expires_time = int(expires_at) if token: now_time = int(time.time()) remaining_seconds = expires_time - now_time #print 'remains: ' + str(remaining_seconds) if remaining_seconds > 3600: client.auth_with_token(token) else: client.refresh_token(refresh_token) config.set('INFO', 'token', client.token_code) config.set('INFO', 'refresh_token', client.refresh_token_code) config.set('INFO', 'expires_at', client.expires_at) with open('test_config.cfg', 'wb') as configfile: config.write(configfile) else: print_('Go to the following link in your browser:') print_(client.authorize_url) code = input('Enter the verification code and hit ENTER when you\'re done:') client.auth_with_code(code) print_('token code:', client.token_code) print_('refresh token code:', client.refresh_token_code) print_('expires at:', client.expires_at) print type(client.expires_at) config.set('INFO', 'token', client.token_code) config.set('INFO', 'refresh_token', client.refresh_token_code) config.set('INFO', 'expires_at', client.expires_at) with open('test_config.cfg', 'wb') as configfile: config.write(configfile) #pdb.set_trace() return client
class Note4stDouban: def __init__(self, user_json_file=None): """douban client for note4st user_json_file - solved on register """ self.client = None self.user = DoubanUser() self.is_login = False if user_json_file: self.user.load(open(user_json_file)) def register(self, auth_code): """douban client for note4st user_json_file - solved on register """ self.client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) self.client.auth_with_code(auth_code) self.user.registered( self.client.user.me, self.client.user.access_token.token, self.client.user.access_token.refresh_token, self.client.user.access_token.expires_in, self.client.user.access_token.expires_at, ) self.user.save2file(self.user.name) self.is_login = True def login(self, email=None, password=None, token=None): if not self.client: self.client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) if token: self.client.auth_with_token(token) self.client.auth_with_token(token) else: self.client.auth_with_password(email, password) self.user.refresh_token = self.client.refresh_token_code self.user.uid = self.client.user.me['uid'] self.is_login = True
#!/usr/bin/env python3 from douban_client import DoubanClient API_KEY = '0c39dde0317db0f024ad99a0947912d0' API_SEC = 'e30f500ed75a3a4f' REDIRECT_URL = 'http://blog.unionx.me/callback' SCOPE = 'book_basic_r, book_basic_w, shuo_basic_r, douban_basic_common' client = DoubanClient(API_KEY, API_SEC, REDIRECT_URL, SCOPE) print('Go to the following link in your browser:') print(client.authorize_url) code = input('Enter the verification code:\n') client.auth_with_code(code) # 2. 如果有之前有 token,则可以 # client.auth_with_token(token) # Token Code token_code = client.token_code print(token_code) book = client.book.get(26372794) print(book) # Refresh Token # 请注意:`refresh_token_code` 值仅可在授权完成时获取(即在 `auth_with_code`, `auth_with_password` 之后) refresh_token_code = client.refresh_token_code client.refresh_token(refresh_token_code) # refresh token
from douban_client import DoubanClient KEY = '' SECRET = '' CALLBACK = '' TOKEN = 'your token' SCOPE = 'douban_basic_common,community_basic_user' client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) client.auth_with_token(TOKEN) print client.user.me
# # #url = 'https://www.douban.com/service/auth2/auth?' #url = url + client_id + redirect_uri + scope + response_type # #print url,'\n' # ##webbrowser.open(url,2) # #post = '&' + urllib.urlencode(secret) # #turl = 'https://www.douban.com/service/auth2/token?' #post = client_id + redirect_uri + grant_type + post # #print turl #print post # #f = urllib2.urlopen(turl,post) #print f.readlines() key = '09b8939a3413aad21d29bace9c3b0076' secret = '9859991d44544cb1' redirect = 'http://bearzk.wordpress.com/' token = '80dce6eac862de188296b4c059289a48' client = DoubanClient(key,secret,redirect) client.auth_with_token(token) g = client.book.getter() print g
# coding: utf-8 """ 使用 hist magic 保存 ipython 命令, 便于命令行调试 %hist """ import time from datetime import datetime from douban_client import DoubanClient from douban_mongo import StorageClient import logging log = logging.getLogger(__name__) level = logging.WARNING level = logging.DEBUG logging.basicConfig(format='%(name)s \t\t%(levelname)-8s %(filename)8s %(funcName)8s() [%(message)-60s] %(module)s', level=level) douban_client = DoubanClient() mongo_client = StorageClient() content, error = douban_client.movie_in_theaters() type(content) print("将这些代码复制到 ipython 中,便于调试 ..")
#!/usr/bin/env python # encoding: utf-8 from douban_client import DoubanClient from ruamel import yaml import logging import threading API_KEY = '' API_SECRET = '' SCOPE = 'douban_basic_common,shuo_basic_r,shuo_basic_w,book_basic_r' client = DoubanClient(API_KEY, API_SECRET, '', SCOPE) client.auth_with_password('', '') logger = logging.getLogger(__file__) con = threading.Condition() sdk_count = 0 def getClient(): return client def getBookByIsbn(isbn): return client.book.isbn(isbn) def getBookById(id): logger
def __init__(self): self.clent = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) self.clent.auth_with_token(TOKEN)
#encoding:utf-8 """ auth with password 注意:auth_with_password 需要先申请 xAuth 权限 关于 xAuth 权限申请可咨询: api-master[at]douban.com 或者到 http://www.douban.com/group/dbapi/ 寻求帮助 """ from douban_client import DoubanClient KEY = '' SECRET = '' CALLBACK = '' SCOPE = 'douban_basic_common,community_basic_user' client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) client.auth_with_password('user_email', 'user_password') print client.user.me
# -*- coding:utf-8 -*- from douban_client import DoubanClient API_KEY = '020a9c5869bcf26b061fd5093d36d741' API_SECRET = '2e6e4d4785a8c438' REDIRECT_URI = 'http://freesz.sinaapp.com/douban' # 在 OAuth 2.0 中, # 获取权限需要指定相应的 scope,请注意!! # scope 权限可以在申请应用的 "API 权限" 查看。 SCOPE = 'douban_basic_common,book_basic_r,book_basic_w' client = DoubanClient(API_KEY, API_SECRET, REDIRECT_URI, SCOPE) print client.authorize_url
from douban_client import DoubanClient KEY = '022e300c79c18fc7068a90256d44af55' SECRET = '11c8bcbac80e8085' CALLBACK = 'http://www.douban.com' SCOPE = 'douban_basic_common,movie_basic,movie_basic_r,community_basic_note,community_basic_user,community_basic_photo,book_basic_r,music_basic_r,shuo_basic_r' client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) print client.authorize_url
Replace this with more appropriate tests for your application. """ from django.test import TestCase class SimpleTest(TestCase): def test_basic_addition(self): """ Tests that 1 + 1 always equals 2. """ self.assertEqual(1 + 1, 2) if __name__ == "__main__": from douban_client import DoubanClient API_KEY = '074b071d3b024f8315e11557b106ee89' API_SECRET = '35e9df8c4c8a7435' SCOPE = 'shuo_basic_r,shuo_basic_w' your_redirect_uri = 'http://hobo.sinaapp.com/' client = DoubanClient(API_KEY, API_SECRET, your_redirect_uri, SCOPE) print 'Go to the following link in your browser:' print client.authorize_url code = raw_input('Enter the verification code:') client.auth_with_code(code)
def autopraise(id0): #需要申请一个豆瓣API KEY, 在这里申请 http://developers.douban.com/apikey/apply API_KEY = '' API_SECRET = '' # 在 OAuth 2.0 中, # 获取权限需要指定相应的 scope,请注意!! SCOPE = 'shuo_basic_r,shuo_basic_w,miniblog_basic_r,miniblog_basic_w' callback = "http://www.XXXX.com/callback" #填你申请时候的回调地址,有没有网站都无所谓 client = DoubanClient(API_KEY, API_SECRET, callback, SCOPE) # 存储授权地址 authurl = client.authorize_url # print authurl f = open("c:/authurl.txt", "w") # # 读取页面源代码 br = mechanize.Browser() # Cookie Jar cj = cookielib.LWPCookieJar() br.set_cookiejar(cj) # Browser options br.set_handle_equiv(True) br.set_handle_gzip(True) br.set_handle_redirect(True) br.set_handle_referer(True) br.set_handle_robots(False) br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) #模拟浏览器行为 br.addheaders = [( 'User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/4.0.0' )] r2 = br.open(authurl) br.select_form(nr=1) br['user_email'] = '' #你的豆瓣用户名和密码 br['user_passwd'] = '' r3 = br.submit() c = r3.geturl() f.write(c) #从请求页面回调地址中查找code m = re.findall("(?<=code\=).*", c) code = m[0] print code # 用code授权client client.auth_with_code(code) #获取最近广播数据 r4 = br.open("http://api.douban.com/shuo/v2/statuses/home_timeline?count=5" ) #Count这里可以修改获取的广播数量,5比较多 caststr = r4.read() id1 = re.findall("(?<=id\"\:)\d+", caststr) #友邻白名单 def whitelist(): r1 = br.open( "http://api.douban.com/shuo/v2/statuses/user_timeline/XXXX" ) #XXXX处 填写你不想赞的友邻用户名 cast1 = r1.read() id1 = re.findall("(?<=id\"\:)\d+", cast1) id = id1 return id f.write(caststr) #存储广播列表 for id in id1: if id in id0: break if id in whitelist(): break client.miniblog.like(id) #赞 print "liked" if hour > 2 and hour < 7: client.miniblog.comment.new(id, "Yo,watch body young man") #提醒友邻睡觉啦 pass return id1
# id = ids[0] for id in ids: content, error = douban_client.movie_subject(id) if content != None: mongo_client.save_movie_subject(id, content) # mongo_client.collect_movie_subject(id, content) else: print content, error # break log.warn("got date success %s %s " % (now.strftime("%Y-%m-%d %H:%M:%S"), now.strftime("%s"))) if __name__ == "__main__": level = logging.WARNING level = logging.DEBUG logging.basicConfig(format='%(name)s \t\t%(levelname)-8s %(filename)8s %(funcName)8s() [%(message)-60s] %(module)s', level=level) START_TIME = datetime.now() douban_client = DoubanClient() mongo_client = StorageClient() while True: content, error = douban_client.movie_in_theaters() # 测试抓取, if content == None and error.code == 1998: print "我们抓取的太快了, 休息一下 ..." time.sleep(BREAK_INTERVEL_SECONDS) # 如果抓取太快, 等候下一轮抓取 task_movie_list() time.sleep(BREAK_INTERVEL_SECONDS) task_movie_subject_recent() #break # debug
# -*- coding:utf-8 -*- from douban_client import DoubanClient from xsettings import XCFG API_KEY = XCFG.WECHAT_API_KEY API_SECRET = XCFG.WECHAT_API_SECRET REDIRECT_URI = XCFG.WECHAT_REDIRECT_URI SCOPE = XCFG.WECHAT_SCOPE client_wechat = DoubanClient(API_KEY, API_SECRET, REDIRECT_URI, SCOPE) LOGIN_API_KEY = XCFG.LOGIN_API_KEY LOGIN_API_SECRET = XCFG.LOGIN_API_SECRET LOGIN_REDIRECT_URI = XCFG.LOGIN_REDIRECT_URI LOGIN_SCOPE = XCFG.LOGIN_SCOPE client_login = DoubanClient(LOGIN_API_KEY, LOGIN_API_SECRET, LOGIN_REDIRECT_URI, LOGIN_SCOPE)
from douban_client import DoubanClient import requests API_KEY = '01674b2cf3fb7e9d0435e186dbf27bd7' API_SECRET = '5fc3e181565091c7' SCOPE = 'douban_basic_common,shuo_basic_r,shuo_basic_w' your_redirect_uri = 'http://bbs.csdn.net/topics/370247707.com' client = DoubanClient(API_KEY, API_SECRET, your_redirect_uri, SCOPE) # print 'Go to the following link in your browser:' print client.authorize_url r = requests.post(client.authorize_url) code = r.json()['code'] client.auth_with_code(110) print client.user.me
from douban_client import DoubanClient API_KEY = '0e5eae024adb9ce12b1c0a06e092d6a3' API_SECRET = '9f7ef068498fc361' # 在 OAuth 2.0 中, # 获取权限需要指定相应的 scope,请注意!! # scope 权限可以在申请应用的 "API 权限" 查看。 SCOPE = 'douban_basic_common,shuo_basic_r,shuo_basic_w' client = DoubanClient(API_KEY, API_SECRET, "http://opac.whsw.tk", SCOPE) # 以下方式 2 选 1: # 1. 引导用户授权 print('Go to the following link in your browser:') print(client.authorize_url) code = input('Enter the verification code:') client.auth_with_code(code) # 2. 如果有之前有 token,则可以 #client.auth_with_token(token) # Token Code token_code = client.token_code # Refresh Token # 请注意:`refresh_token_code` 值仅可在授权完成时获取(即在 `auth_with_code`, `auth_with_password` 之后) refresh_token_code = client.refresh_token_code client.refresh_token(refresh_token_code) # refresh token
from douban_client import DoubanClient import memcache DOUBAN_API_KEY = '0beb137ce048a99623c13a76a0d9fcc2' DOUBAN_API_SECRET = '0c4e47816f6a0a93' DOUBAN_SCOPE = 'book_basic_r,book_basic_w,community_basic_note,community_basic_online,community_basic_photo,community_advanced_photo,community_basic_user,douban_basic_common,event_basic_r,event_basic_w,movie_basic_r,movie_basic_w,music_artist_r,music_basic_r,music_basic_w,shuo_basic_r,shuo_basic_w' DOUBAN_REDIRECT_URI = 'http://book.douban.com:8080/login' client = DoubanClient(DOUBAN_API_KEY, DOUBAN_API_SECRET, DOUBAN_REDIRECT_URI, DOUBAN_SCOPE) mc = memcache.Client(['127.0.0.1:12000'], debug=True)
from douban_client import DoubanClient KEY = '' SECRET = '' CALLBACK = '' SCOPE = 'douban_basic_common,community_basic_user' client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) client.auth_with_password('user_email', 'user_password') print client.user.me
when you run "manage.py test". Replace this with more appropriate tests for your application. """ from django.test import TestCase class SimpleTest(TestCase): def test_basic_addition(self): """ Tests that 1 + 1 always equals 2. """ self.assertEqual(1 + 1, 2) if __name__ == "__main__": from douban_client import DoubanClient API_KEY = '074b071d3b024f8315e11557b106ee89' API_SECRET = '35e9df8c4c8a7435' SCOPE = 'shuo_basic_r,shuo_basic_w' your_redirect_uri = 'http://hobo.sinaapp.com/' client = DoubanClient(API_KEY, API_SECRET, your_redirect_uri, SCOPE) print 'Go to the following link in your browser:' print client.authorize_url code = raw_input('Enter the verification code:') client.auth_with_code(code)
def get(self, *args): KEY = "08e0dfb50c90b0991aa29f1f25b211cb" SECRET = "378ed0dadf96de47" CALLBACK = conf["url"] + "/dback" SCOPE = "douban_basic_common,community_basic_user,community_basic_note" client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) code = self.get_argument("code") if code: client.auth_with_code(code) auth = client.user.me if auth: accounts = conf["db"].accounts account = { "_id": Kid.kid(), "id": auth.get("id"), "name": auth.get("name"), "avatar": auth.get("avatar"), "loc_name": auth.get("loc_name"), "desc": auth.get("desc"), } try: exist = Account.get_by_id(auth.get("id")) if exist is None: accounts.save(account) else: accounts.update({"id": auth.get("id")}, {"$set": account}) except Exception as e: print str(e) diary = client.note.list(auth.get("id"), 0, 20) diaries = conf["db"].diaries diary_notes = diary.get("notes") for i in diary_notes: # re-summary content from douban summary = i.get("content")[0:80] + "..." diary = { "_id": Kid.kid(), "update_time": i.get("update_time"), "publish_time": i.get("publish_time"), "photos": i.get("photos"), "comments_count": i.get("comments_count"), "liked_count": i.get("liked_count"), "recs_count": i.get("recs_count"), "id": i.get("id"), "alt": i.get("alt"), "can_reply": i.get("can_reply"), "title": i.get("title"), "privacy": i.get("privacy"), "summary": summary, "content": client.note.get(i.get("id"), format="html_full").get("content"), } try: exist = Diary.get_by_id(i.get("id")) if exist is None: diaries.save(diary) else: diaries.update({"id": i.get("id")}, {"$set": diary}) except Exception as e: print str(e) self.redirect("/") else: print "douban_callback code missing"
from six.moves import input from douban_client import DoubanClient KEY = '' SECRET = '' CALLBACK = '' SCOPE = 'douban_basic_common,community_basic_user' client = DoubanClient(KEY, SECRET, CALLBACK, SCOPE) print client.authorize_url code = input('Enter the verification code:') client.auth_with_code(code) print client.user.me
def autopraise(id0): # 需要申请一个豆瓣API KEY, 在这里申请 http://developers.douban.com/apikey/apply API_KEY = "" API_SECRET = "" # 在 OAuth 2.0 中, # 获取权限需要指定相应的 scope,请注意!! SCOPE = "shuo_basic_r,shuo_basic_w,miniblog_basic_r,miniblog_basic_w" callback = "http://www.XXXX.com/callback" # 填你申请时候的回调地址,有没有网站都无所谓 client = DoubanClient(API_KEY, API_SECRET, callback, SCOPE) # 存储授权地址 authurl = client.authorize_url # print authurl f = open("c:/authurl.txt", "w") # # 读取页面源代码 br = mechanize.Browser() # Cookie Jar cj = cookielib.LWPCookieJar() br.set_cookiejar(cj) # Browser options br.set_handle_equiv(True) br.set_handle_gzip(True) br.set_handle_redirect(True) br.set_handle_referer(True) br.set_handle_robots(False) br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(), max_time=1) # 模拟浏览器行为 br.addheaders = [ ( "User-agent", "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/4.0.0", ) ] r2 = br.open(authurl) br.select_form(nr=1) br["user_email"] = "" # 你的豆瓣用户名和密码 br["user_passwd"] = "" r3 = br.submit() c = r3.geturl() f.write(c) # 从请求页面回调地址中查找code m = re.findall("(?<=code\=).*", c) code = m[0] print code # 用code授权client client.auth_with_code(code) # 获取最近广播数据 r4 = br.open("http://api.douban.com/shuo/v2/statuses/home_timeline?count=5") # Count这里可以修改获取的广播数量,5比较多 caststr = r4.read() id1 = re.findall('(?<=id"\:)\d+', caststr) # 友邻白名单 def whitelist(): r1 = br.open("http://api.douban.com/shuo/v2/statuses/user_timeline/XXXX") # XXXX处 填写你不想赞的友邻用户名 cast1 = r1.read() id1 = re.findall('(?<=id"\:)\d+', cast1) id = id1 return id f.write(caststr) # 存储广播列表 for id in id1: if id in id0: break if id in whitelist(): break client.miniblog.like(id) # 赞 print "liked" if hour > 2 and hour < 7: client.miniblog.comment.new(id, "Yo,watch body young man") # 提醒友邻睡觉啦 pass return id1
from douban_client import DoubanClient from util import mongocli import json API_KEY = '0d0690ea685259262713b2bf38f70b72' API_SECRET = 'bf6e552666db43a0' SCOPE = 'douban_basic_common,community_basic_user' client = DoubanClient(API_KEY, API_SECRET, 'http://localhost:5000', SCOPE) def get_douban_eventlist(date): #find solidify event solidify_event = mongocli.find_douban_eventlist(date) if solidify_event is None: event = client.event.list(108296, 'day', 'all', date, 15) mongocli.save_douban_eventlist({"date": date, "event": event}) return event else: return solidify_event
#encoding:utf-8 from douban_client import DoubanClient from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth import authenticate, login as auth_login from django.contrib.auth.models import User from models import UserProfile from models import Link from accounts.signals import new_user_register import settings API_KEY = settings.DOUBAN_API_KEY API_SECRET = settings.DOUBAN_API_SECRET SCOPE = settings.DOUBAN_SCOPE CALLBACK_URL = settings.DOUBAN_CALLBACK_URL client = DoubanClient(API_KEY, API_SECRET, CALLBACK_URL) def index(request): """ 跳转到验证页面 """ auth_url = client.authorize_url return HttpResponseRedirect(auth_url) def callback(request): """ 用户授权后的回调 """ code = request.GET.get(u'code')
def login(): client = DoubanClient(API_KEY, API_SECRET, CALLBACK, SCOPE) auth_url = client.authorize_url return st('login.html', **locals())