forked from jackMort/hamster-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
251 lines (214 loc) · 9.5 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2011 [email protected]
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import sys
import time
from optparse import OptionParser, OptionGroup
from chomikuj import Chomik, CaptchNeededException
def sleep( timeout ):
timeout = int( timeout )
print " -- going sleep for %d secs." % timeout
time.sleep( timeout )
def user_done( type, username, user ):
try:
return user in [ u.strip() for u in open( "db/%s_%s.dat" % ( username, type ), 'r' ) ]
except Exception:
return False
def add_user_to_done( type, username, user ):
if not os.path.isdir( 'db' ):
os.mkdir( 'db' )
f = open( "db/%s_%s.dat" % ( username, type ), 'a' )
f.write( '%s\n' % user )
f.close()
def query_number( question, default=0 ):
prompt = " [%s] " % default
while 1:
sys.stdout.write( question + prompt )
choice = raw_input().lower()
if default is not None and choice == '':
return default
elif choice:
return int( choice )
else:
sys.stdout.write( "Please select the number.\n" )
def query_yes_no_quit(question, default="yes"):
"""Ask a yes/no/quit question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no", "quit" or None (meaning
an answer is required of the user).
The "answer" return value is one of "yes", "no" or "quit".
"""
valid = {"yes":"yes", "y":"yes", "ye":"yes",
"no":"no", "n":"no",
"quit":"quit", "qui":"quit", "qu":"quit", "q":"quit"}
if default == None:
prompt = " [y/n/q] "
elif default == "yes":
prompt = " [Y/n/q] "
elif default == "no":
prompt = " [y/N/q] "
elif default == "quit":
prompt = " [y/n/Q] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while 1:
sys.stdout.write(question + prompt)
choice = raw_input().lower()
if default is not None and choice == '':
return default
elif choice in valid.keys():
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes', 'no' or 'quit'.\n")
parser = OptionParser( usage="usage: %prog [OPTIONS]" )
parser.add_option( "-c", "--copy", dest="copy", action="store_true", help="recursivly copy directories" )
parser.add_option( "-m", "--mkdir", dest="mkdir", help="mkdir" )
parser.add_option( "-r", "--rmdir", dest="rmdir", help="remove directory" )
parser.add_option( "-s", "--stats", dest="stats", action="store_true", help="print user stats" )
parser.add_option( "-i", "--invite", dest="invite", action="store_true", help="invite users" )
parser.add_option( "-g", "--generate", dest="generate_list", help="generate users list" )
parser.add_option( "-x", "--send-message", dest="send_message", help="send chat message" )
parser.add_option( "-d", "--download", dest="download", help="download file from given directory" )
parser.add_option( "-u", "--username", dest="username", help="chomik username" )
parser.add_option( "-p", "--password", dest="password", help="chomik password" )
parser.add_option( "-f", "--file", dest="file", help="users file" )
parser.add_option( "-b", "--users", dest="users", help="list of users" )
parser.add_option( "-t", "--timeout", dest="timeout", help="timeout in secs" )
if __name__ == "__main__":
( options, args ) = parser.parse_args()
chomik = Chomik( options.username, options.password )
if options.copy:
if chomik.connect():
users = []
if options.users:
users = options.users
else:
users = [ u.strip() for u in open( options.file, 'r' ) ]
for user in users:
print chomik.copy_directory_tree( user )
if options.timeout:
sleep( options.timeout )
if options.invite:
if chomik.connect():
users = []
if options.users:
users = options.users
else:
users = [ u.strip() for u in open( options.file, 'r' ) ]
for user in users:
if not user_done( 'invite', options.username, user ):
chomik.invite( user )
add_user_to_done( 'invite', options.username, user )
if options.timeout:
sleep( options.timeout )
else:
print " -- skipping user %s, already processed" % user
if options.send_message:
if chomik.connect():
users = []
if options.users:
users = options.users
else:
users = [ u.strip() for u in open( options.file, 'r' ) ]
for user in users:
do_this = True
while do_this:
try:
if not user_done( 'send_message', options.username, user ):
message = open( options.send_message, 'r' ).read()
chomik.send_chat_message( user, message )
add_user_to_done( 'send_message', options.username, user )
if options.timeout:
sleep( options.timeout )
else:
print " -- skipping user %s, already processed" % user
do_this = False
except CaptchNeededException:
if query_yes_no_quit( " -- WRITE CAPTCHA AND TRY AGAIN, continue ?" ) <> 'yes':
sys.exit( 1 )
if options.download:
if chomik.connect():
directory = options.download
do_this = True
while do_this:
print " -- Reading directory %s" % directory
do_this = False
result = chomik.read_directory( directory )
len_folders = len( result['folders'] )
len_files = len( result['files'] )
if len_folders + len_files == 0:
print "directory contains 0 folders and 0 files ..."
else:
i = 0
tmp_dict = {}
print " :: %d folders" % len_folders
for item in result['folders']:
i += 1
tmp_dict[i] = {
'type': 'folder',
'href': item[1]
}
print "[%d] + %s ( %s )" % ( i, item[0], item[1] )
print " :: %d files" % len_files
for item in result['files']:
i += 1
tmp_dict[i] = {
'type': 'file',
'id': item[2],
'name': item[0]
}
print "[%d] + %s ( %s )" % ( i, item[0], item[2] )
result = query_number( "Select a number of item ( 0 to download all files )" )
if result == 0:
print " -- downloading all files %d" % len_files
for key, item in tmp_dict.items():
if item['type'] == 'file':
print " -- downloading file %s" % item['name']
chomik.download_file_by_id( item['id'] )
elif tmp_dict.has_key( result ):
item = tmp_dict[result]
if item['type'] == 'file':
print " -- downloading file %s" % item['name']
chomik.download_file_by_id( item['id'] )
else:
do_this = True
directory = item['href']
else:
print "Bad choice ..."
if options.stats:
if chomik.connect():
stats = chomik.get_stats()
print " points: %s" % stats['points']
print " size : %s" % stats['size']
print " files : %s" % stats['files']
print " - docs : %s" % stats['docs']
print " - images: %s" % stats['images']
print " - video : %s" % stats['video']
print " - music : %s" % stats['music']
print "--------------------------"
else:
print "Not connected ..."
if options.mkdir:
if chomik.connect():
print chomik.create_directory( options.mkdir )
if options.rmdir:
if chomik.connect():
print chomik.remove_directory( options.rmdir )
if options.generate_list:
chomik.generate_list( count=int( options.generate_list ) )
# vim: fdm=marker ts=4 sw=4 sts=4