usr bin env python coding utf-8 prog OPTIONS --help Crawles ftp and gi

  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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""%prog [OPTIONS] [--help]
Crawles ftp and give XML output"""
__ver__ = "%prog 0.2alfa" +\
"\n Authors by:" +\
"\n\t Imposeren <imposeren@gmail.com>" +\
"\n\t krava <krave@bete.tv>" +\
"\n Home:" +\
"\n\t http://look.bete.tv"
from multiprocessing import Pool
from ftplib import FTP
from socket import getfqdn
from sys import argv,exit,stdout,stderr,stdin
from optparse import OptionParser
from ftped import crawl
from parse import toXML
from solr import handler
def optioner(argarr=argv[1:]):
parser = OptionParser(usage=__doc__,version=__ver__)
parser.add_option('-p','--parallel',
action="store", dest='parallel',type='int', default=4,
help='Number of parallel crawls')
parser.add_option('-n','--numlines',
action="store", dest='number',type='int', default=8192,
help='Number of docs per chunk')
parser.add_option('-s','--source',
action="store", dest='source', default='selfTest',
help='source of targets, if source is args, then supply ' +\
'targets in this format:\n' +\
'-s args host1 enc1 host2 enc2 ...')
parser.add_option('-c','--config',
action="store", dest='config',
default='/etc/CPTD/crawl.conf',
help='config file location')
return parser.parse_args(argarr)
def worker(host='localhost',
enca='utf-8',
target=stdout,
N=8192,
user='anonymous',
passwd='robot@look.bete.tv',
):
if target =='file':
target = open(host+'.xml','w')
global n
n = 0
def outPutter(res,item,error=False):
global n
if not error:
res,n = toXML.splitter(item[0],n,N)
target.write(res.encode('utf-8'))
try:
ftp = FTP(host)
crawl.connect(ftp,user,passwd)
host = getfqdn(host)
print 'host: ',host
crawl.walker(ftp,host,'/',enca,toXML.xmlize,outPutter,retVal=False)
if n<=N:
target.write(toXML.ender)
target.close()
return 1
except (KeyboardInterrupt, SystemExit) as Ex:
return Ex
except Exception as Ex:
stderr.write('\nError: ' + str(Ex))
def cleanup(all=True,w=None):
global p
if all:
p.terminate()
p = Pool()
def daemon(argarr=argv[1:]):
try:
(o,targets) = optioner(argarr)
except Exception,Ex:
stderr.write('\nError: '+str(Ex))
return 0
hosts=[]
encs=[]
if o.source=='selfTest':
hosts = ['10.115.44.146']
encs = ['utf-8','utf-8']
elif o.source=='args':
for target in targets:
if target in ['cp1251','koi8-r','utf-8']:
encs.append(target)
else:
hosts.append(target)
if len(hosts) != len(encs):
raise ValueError('Something wrong with targets')
else:
solr="http://bete.tv:8889/solr"
print solr.handler.actions(solr,'get')
global p
p = Pool(processes=o.parallel)
jobs=[]
for i in xrange(len(hosts)):
jobs.append(p.apply_async(worker,[hosts[i],encs[i],'file',o.number]))
p.close()
for job in jobs:
job.get()
def main():
try:
daemon()
except (KeyboardInterrupt, SystemExit):
stderr.write('\nTerminated\n')
cleanup()
return 0
except Exception as Ex:
stderr.write('\nUnhandled exception!'+str(Ex)+'\n')
raise
return 0
return 1
if __name__ == '__main__':
exit(main())