used a trickky way to temp slove import douban books issue

This commit is contained in:
2014-06-22 10:32:52 +08:00
parent 26e64ce560
commit 8f1d2e58d8
8 changed files with 53 additions and 19 deletions
+1 -1
View File
@@ -58,7 +58,7 @@ class Api::V1::BooksController < ApplicationController
def import_douban_books
user = User.find_by_email params[:user_email]
user.douban_user params[:name]
render json: {status: 'success', msg: '成功连接豆瓣用户'}
render json: {status: 'success', msg: '成功连接豆瓣用户,稍后您将可以图书列表看到你的图书'}
end
end
+4 -2
View File
@@ -1,6 +1,8 @@
class ThirdPartyUserData < ActiveRecord::Base
belongs_to :user
attr_accessible :douban_user_name, :user_id
attr_accessible :douban_user_name, :user_id, :import_douban_done
def import_douban_user_done
update_attributes import_douban_done: true
end
end
+19 -2
View File
@@ -134,8 +134,21 @@ class User < ActiveRecord::Base
else
third_party_user_datas.create douban_user_name: name
end
# Temp disabled
DoubanBooksWorker.perform_async id, name
begin
DoubanBooksWorker.perform_async id, name
import_double_user_done
rescue RuntimeError => error
logger.error "绑定豆瓣用户图书失败:#{error}"
true
rescue TypeError => type_error
logger.error "绑定豆瓣用户图书失败,类型错误:\n#{type_error}"
true
rescue RestClient::Forbidden
logger.error "豆瓣禁止访问API了"
true
rescue Exception => ex
logger.error "出错了:#{ex.to_s}"
end
end
def get_douban_user
@@ -146,4 +159,8 @@ class User < ActiveRecord::Base
end
end
def import_double_user_done
third_party_user_datas.first.import_douban_user_done
end
end
+9 -9
View File
@@ -8,9 +8,10 @@ class DoubanBooksWorker
total = 0
start = 0
while (start == 0) || (total-start > count)
result = RestClient.get "http://api.douban.com/v2/book/user/#{name}/collections?start=#{start}&count=#{count}"
url = "http://api.douban.com/v2/book/user/#{name}/collections?start=#{start}&count=#{count}"
logger.info "url============#{url}"
result = RestClient.get url
json = JSON.parse(result)
logger.info "get books from douban user:==== #{json}"
storeBooks(json, user)
total = json['total']
start += count
@@ -20,15 +21,14 @@ class DoubanBooksWorker
def storeBooks(json, user)
collections = json['collections']
logger.error "--------------------#{collections}"
collections.each { |collection|
logger.warn "!!!!!!!!!!!! #{collection}\n"
collections.each { |collection, index|
book = collection['book']
Book.create_book_by_isbn book unless Book.find_by_isbn book['isbn13']
user.create_book_instance book['isbn13']
if book['isbn13'] != nil
Book.create_book_by_isbn book unless Book.find_by_isbn book['isbn13']
user.create_book_instance book['isbn13']
end
}
end
end
+2 -2
View File
@@ -16,7 +16,7 @@ development:
adapter: postgresql
encoding: unicode
database: libr_development
pool: 5
pool: 20
host: localhost
user: postgres
password:
@@ -52,7 +52,7 @@ production:
adapter: postgresql
encoding: unicode
database: libr_production
pool: 5
pool: 20
host: localhost
user: postgres
password:
@@ -0,0 +1,6 @@
class AddImportDoubanBookStatusToThirdPartyData < ActiveRecord::Migration
def change
add_column :third_party_user_data, :import_douban_done, :boolean, default: false
end
end
+2 -1
View File
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20140615114344) do
ActiveRecord::Schema.define(version: 20140622005440) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -135,6 +135,7 @@ ActiveRecord::Schema.define(version: 20140615114344) do
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
t.boolean "import_douban_done", default: false
end
create_table "user_to_books", force: true do |t|
+10 -2
View File
@@ -11,7 +11,7 @@ describe :User do
it 'ensure user can borrow a existed book' do
instance = BookInstance.create book_id: @book.id, user_id: @user.id
@user.borrow instance
@user.borrowed_and_not_returned_books.empty?.should == false
@user.borrowed_and_not_returned_books.empty?.should == false
@user.borrowed_and_not_returned_books.count.should == 1
end
it 'ensure when there are two books,users can borrow these two books.' do
@@ -21,7 +21,7 @@ describe :User do
instance_2 = BookInstance.create book_id: book_1.id, user_id: user_1.id
user_1.borrowed_and_not_returned_books.count.should == 0
user_1.borrow instance
user_1.borrowed_and_not_returned_books.empty?.should == false
user_1.borrowed_and_not_returned_books.empty?.should == false
user_1.borrowed_and_not_returned_books.count.should == 1
end
@@ -106,4 +106,12 @@ describe :User do
# end
#
#end
it 'should update user third party import douban user status' do
@user.douban_user 'test'
@user.third_party_user_datas.first.import_douban_done.should == false
@user.import_double_user_done
@user.third_party_user_datas.first.import_douban_done.should == true
end
end