forked from leodas/ZhihuHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.py
50 lines (40 loc) · 1.64 KB
/
answer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import zhihu
from session import ZhihuSession
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
parser.add_option('-u', '--user', dest='username')
parser.add_option('-p', '--password', dest='passwd')
parser.add_option('-s', '--dest', dest='dest_user')
parser.add_option('-b', '--backup', action='store_true', dest='backup')
parser.add_option('-d', '--delete', action='store_true', dest='delete')
options, args = parser.parse_args()
# TEST BEGIN ======================================================>
session = ZhihuSession()
if not session.login(options.username, options.passwd):
print 'Login Failed, exit'
exit(1)
if options.dest_user:
answers, answers_size = session.getAnswers(options.dest_user)
else:
answers, answers_size = session.getAnswers()
print '%d answers found' % answers_size
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
if options.backup:
f = open(session.me.link_name + '-answers', 'w+')
for answer in answers:
f.write(u'\n==================================================================\n')
f.write(answer.question.title + u'\n')
f.write(answer.question.url + u'\n')
f.write(u'\n')
f.write(answer.text + u'\n')
f.close()
print '%d answers has saved in %s' % (answers_size, session.me.link_name + '-answers')
if options.delete and not options.dest_user:
for answer in answers:
session.delAnswer(answer)
print '%d answers has removed'