-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanage_book.py
executable file
·319 lines (265 loc) · 11.4 KB
/
manage_book.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# coding=utf-8
from flask_script import Manager
from wsgi_api import app
import time
import random
import string
import requests
import xmltodict
from models import db
from models import Book
from models import BookShelf
from models import BookChapters
from models import BookChapterContent
from models import ChannelType
from models import ChannelData
manager = Manager(app)
@manager.command
def book():
# 逻辑导入部分开始
from script import book
book.start()
@manager.option('-c', '--channel', dest='channel', default='')
def update_book(channel):
import datetime
print 'Start:', datetime.datetime.now()
from script.base_book import BookUpdater
channel = channel.lower()
channel_class = getattr(getattr(__import__('script.%s_book' % channel), '%s_book' % channel), '%sBookSpider' % channel.capitalize())
if not channel_class:
print 'book channel not exist!'
return
updater = BookUpdater(channel_class)
updater.pull_book()
print 'End:', datetime.datetime.now()
@manager.command
def update_sina_book():
from script import update_book
update_book.start()
@manager.command
def update_kaixing_book():
from script import kaixing_book
import datetime
print 'Start:', datetime.datetime.now()
kaixing_book.pull_book()
print 'End:', datetime.datetime.now()
@manager.command
def update_anzhi_book():
from script import anzhi_book
import datetime
print 'Start:', datetime.datetime.now()
anzhi_book.pull_book()
@manager.command
def update_jingyu_book():
import datetime
print 'Start:', datetime.datetime.now()
from script import jingyu_book
jingyu_book.pull_book()
print 'End:', datetime.datetime.now()
@manager.command
def update_category_book():
import datetime
print 'Start:', datetime.datetime.now()
from script import update_category
update_category.update_book_category()
print 'End:', datetime.datetime.now()
@manager.command
def update_category():
import datetime
print 'Start:', datetime.datetime.now()
from script import update_category
update_category.update_category()
print 'End:', datetime.datetime.now()
@manager.command
def update_book_cover():
"""更新书籍封面为七牛地址"""
from models import db
from lib.utils import upload_img_by_url
query = db.session.execute('select cover, book_id from book').fetchall()
update_list = []
for book in query:
if book.cover.startswith('http://ov2eyt2uw.bkt.clouddn.com'):
continue
new_cover = upload_img_by_url('book_cover_%s' % book.book_id, book.cover)
print book, new_cover
if new_cover != book.cover:
update_list.append({'book_id': book.book_id, 'cover': new_cover})
db.session.execute('update book set cover=:cover where book_id=:book_id', update_list)
db.session.commit()
@manager.command
def fix_book_cover():
"""更新书籍封面为七牛地址"""
from models import db
import requests
from lib.utils import upload_img_by_url
query = db.session.execute('select cover, book_id from book where book_id>5483665').fetchall()
update_list = []
for book in query:
if not book.cover:
continue
try:
resp = requests.head(book.cover, timeout=1)
if resp.headers.get('content-type') == 'text/html':
print resp.headers.get('content-type'), book.cover
update_list.append({'book_id': book.book_id, 'cover': ''})
else:
if book.cover.startswith('http://ov2eyt2uw.bkt.clouddn.com'):
continue
new_cover = upload_img_by_url('book_cover_%s' % book.book_id, book.cover)
print book, new_cover
if new_cover != book.cover:
update_list.append({'book_id': book.book_id, 'cover': new_cover})
except Exception, e:
print "timeout", book.cover, e
continue
if len(update_list) >= 100:
db.session.execute('update book set cover=:cover where book_id=:book_id', update_list)
db.session.commit()
update_list = []
if update_list:
db.session.execute('update book set cover=:cover where book_id=:book_id', update_list)
db.session.commit()
@manager.command
def fix_dup_book():
"""修复重复书籍"""
from models import db
book_group = db.session.execute('select book_id, book_name, author_name from book where free_collect=1').fetchall()
book_dict = {}
delete_books = []
update_books = []
for book in book_group:
book_key = book.book_name + book.author_name
if book_key not in book_dict:
book_dict[book_key] = book
continue
old_book = book_dict[book_key]
if book.book_name == old_book.book_name and book.author_name == old_book.author_name:
delete_books.append({'book_id': book.book_id})
update_books.append({'book_id': book.book_id, 'new_book_id': old_book.book_id})
db.session.execute('update free_book set book_id=:new_book_id where book_id=:book_id', update_books)
db.session.execute('update book_chapters set book_id=:new_book_id where book_id=:book_id', update_books)
db.session.execute('delete from book where book_id=:book_id', delete_books)
db.session.commit()
@manager.command
def remove_dup_free_book():
"""下架正版已有的免费书籍"""
from models import db
book_group = db.session.execute(
'select book_name, author_name from book where free_collect=0').fetchall()
book_set = {book.book_name + book.author_name for book in book_group}
delete_books = []
free_book_group = db.session.execute(
'select book_id, book_name, author_name from book where free_collect=1').fetchall()
for book in free_book_group:
book_key = book.book_name + book.author_name
if book_key not in book_set:
continue
delete_books.append({'book_id': book.book_id})
print book.book_name, book.author_name, book.book_id
db.session.execute('update book set showed=0 where book_id=:book_id', delete_books)
# db.session.execute('delete from book where book_id=:book_id', delete_books)
# db.session.execute('delete from free_book where book_id=:book_id', delete_books)
# db.session.execute('delete from book_chapters where book_id=:book_id', delete_books)
db.session.commit()
print 'num:', len(delete_books)
@manager.option('-c', '--channel', dest='channel', default='')
def delete_chapters_from_source(channel):
from models import db
sql = 'select book_id from book where source="%s"' %(channel)
books = db.session.execute(sql).fetchall()
for book in books:
sql = 'delete from book_volume where book_id=%s' %book.book_id
db.session.execute(sql)
sql = 'delete from book_chapters where book_id=%s' %book.book_id
db.session.execute(sql)
sql = 'delete from book_chapter_content where book_id=%s' %book.book_id
db.session.execute(sql)
db.session.commit()
@manager.command
def send_group_message():
from lib import redis_utils
key = 'send_group_message'
value = random_str(25)
redis_utils.set_cache(key, value, 10)
url = 'http://localhost:6794/user/send_group_message'
data = dict(value=value)
print requests.get(url, data).text, value
def random_str(length):
return ''.join(random.sample(string.letters + '1234567890',length))
def calc_word_count_from_chapters(book_id):
sql = 'select sum(word_count) from book_chapters where book_id=%s' %(book_id)
return db.session.execute(sql).scalar() or 0
@manager.option('-c', '--channel', dest='channel', default='')
def count_words(channel):
books = Book.query.filter_by(word_count=0, is_comic=0, source=channel).all()
for b in books:
print 'before', b.book_name, b.book_id, b.word_count
word_count = calc_word_count_from_chapters(b.book_id)
if word_count:
b.word_count = word_count
print 'after', b.book_name, b.book_id, b.word_count
db.session.add(b)
db.session.commit()
@manager.command
def update_book_shelf():
names = ['hot', 'new', 'finish']
for name in names:
book_shelfs = BookShelf.query.filter_by(name=name).all()
for i in book_shelfs:
book_data = i.to_admin_dict()['book']
if book_data:
if book_data['channel_type'] == 1: # man
m_name = '%s%s' %(name, '-m')
if not BookShelf.query.filter_by(book_id=i.book_id, name=m_name).first():
db.session.add(BookShelf(i.book_id, m_name, i.user_id, i.ranking, i.rate, i.showed, 1))
elif book_data['channel_type'] == 2: # woman
f_name = '%s%s' %(name, '-f')
if not BookShelf.query.filter_by(book_id=i.book_id, name=f_name).first():
db.session.add(BookShelf(i.book_id, f_name, i.user_id, i.ranking, i.rate, i.showed, 2))
db.session.commit()
# 非外链小说按男女分类导入精选小说(good-m,good-f)专题
books = Book.query.filter_by(free_collect=0).all()
for b in books:
if b.channel_type == 1:
if not BookShelf.query.filter_by(book_id=b.book_id, name='good-m').first():
db.session.add(BookShelf(b.book_id, 'good-m', 0, 0, 0, 1, 1))
if b.channel_type == 2:
if not BookShelf.query.filter_by(book_id=b.book_id, name='good-f').first():
db.session.add(BookShelf(b.book_id, 'good-f', 0, 0, 0, 1, 2))
db.session.commit()
# 将外链小说按男女分类导入男生偏爱(free-m)专题和女生偏爱(free-f)专题
books = Book.query.filter_by(free_collect=1).all()
for b in books:
if b.channel_type == 1:
if not BookShelf.query.filter_by(book_id=b.book_id, name='free-m').first():
db.session.add(BookShelf(b.book_id, 'free-m', 0, 0, 0, 1, 1))
if b.channel_type == 2:
if not BookShelf.query.filter_by(book_id=b.book_id, name='free-f').first():
db.session.add(BookShelf(b.book_id, 'free-f', 0, 0, 0, 1, 2))
db.session.commit()
def add_ct_cd(i, platform):
ct = ChannelType.query.filter_by(platform=platform, name=i.name).first()
if not ct:
data = i.to_admin_dict()
data['platform'] = platform
ct = ChannelType(data)
db.session.add(ct)
db.session.commit()
cds = ChannelData.query.filter_by(channel_code=i.id).all()
for cd in cds:
t_cd = ChannelData.query.filter_by(
class_id=cd.class_id, channel_code=ct.id,
class_name=cd.class_name).first()
if not t_cd:
t_cd = ChannelData(class_id=cd.class_id,
channel_code=ct.id, class_name=cd.class_name)
db.session.add(t_cd)
db.session.commit()
@manager.command
def add_ios_applet_channel_type():
cts = ChannelType.query.filter_by(platform='android').all()
for i in cts:
add_ct_cd(i, 'ios')
add_ct_cd(i, 'applet')
if __name__ == "__main__":
manager.run()