4
4
import codecs
5
5
import os
6
6
import time
7
-
7
+ import sys
8
8
def getInfo (ip ):
9
9
resp = requests .get ('http://ip-api.com/json/' + ip )
10
10
if resp .status_code != 200 :
@@ -15,12 +15,15 @@ def getInfo(ip):
15
15
data_formated = {"ip" : ip , "lon" : data ["lon" ], "lat" : data ["lat" ], "country" : data ["country" ], "isp" : data ["isp" ],"org" : data ["org" ] }
16
16
return data_formated
17
17
18
- def getIps ():
19
- f = open ('ips.txt' ,'r' )
18
+ def readIpFile (filename ):
19
+ script_dir = os .getcwd ()
20
+ s = "input/"
21
+ s += filename
22
+ filepath = os .path .join (script_dir , s )
23
+ f = open (filepath ,'r' )
20
24
ips = []
21
25
for line in f :
22
26
ips .append (line .rstrip ("\n " ))
23
- #print(line.rstrip("\n"))
24
27
return ips
25
28
26
29
def generatePayload (ips ):
@@ -33,7 +36,7 @@ def batchRequest(ips):
33
36
payload = generatePayload (ips )
34
37
resp = requests .post ('http://ip-api.com/batch' , data = json .dumps (payload ))
35
38
if resp .status_code != 200 :
36
- data_formated = "Error while getting Data for " , ip
39
+ data_formated = "Error while getting Data"
37
40
if resp .status_code == 422 :
38
41
data_formated = "Too much IPs in request"
39
42
else :
@@ -42,25 +45,25 @@ def batchRequest(ips):
42
45
43
46
44
47
def main ():
45
- ips = getIps ()
46
- ip_arry = []
47
- # Cut array in chunks because of the api limitation
48
- chunks = [ ips [ x : x + 100 ] for x in range ( 0 , len ( ips ), 100 )]
49
- for c , l in enumerate ( chunks ):
50
-
51
- data = batchRequest ( l )
52
- time . sleep ( 1 )
53
- script_dir = os . path . dirname ( r'C:\Users\admin\Documents\git\pyAuthLog\data' )
54
- s = "data \\ "
55
- s += str ( c )
56
- s += "_output.json"
57
- absPath = os . path . join ( script_dir , s )
58
- print ( "Chunk Number " , str ( c ) + " Path: " + str (absPath ) )
59
- with open ( absPath , "wb" ) as outfile :
60
- json . dump ( data , codecs . getwriter ( 'utf-8' )( outfile ), ensure_ascii = False )
61
-
62
-
63
-
48
+ if len ( sys . argv ) < 2 :
49
+ print ( "Usage: analyse.py <Filename in /input>" )
50
+ sys . exit ()
51
+ else :
52
+ ips = readIpFile ( sys . argv [ 1 ])
53
+ # Cut array in chunks because of the api limitation
54
+ chunks = [ ips [ x : x + 100 ] for x in range ( 0 , len ( ips ), 100 )]
55
+ for c , l in enumerate ( chunks ):
56
+ data = batchRequest ( l )
57
+ # More than 150 Requests per Second will get you blocked
58
+ time . sleep ( 1 )
59
+ script_dir = os . getcwd ()
60
+ s = "data/"
61
+ s += str (c )
62
+ s += "_output.json"
63
+ absPath = os . path . join ( script_dir , s )
64
+ print ( "Chunk Number " , str ( c ) + " Path: " + str ( absPath ) )
65
+ with open ( absPath , "wb" ) as outfile :
66
+ json . dump ( data , codecs . getwriter ( 'utf-8' )( outfile ), ensure_ascii = False )
64
67
65
68
66
69
0 commit comments