-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhubcrawl
More file actions
executable file
·44 lines (38 loc) · 1.25 KB
/
hubcrawl
File metadata and controls
executable file
·44 lines (38 loc) · 1.25 KB
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
#!/usr/bin/env python
# crawl along a hub channel, next or previous, for an arbitrary number of steps
import os
import sys
import urllib2
def hubcrawl(url, direction, num_steps):
for i in range(num_steps):
print(url)
request = urllib2.Request("%s/%s" % (url, direction[1:]))
response = urllib2.urlopen(request)
url = response.geturl()
def validate(url, direction, num_steps):
if direction != "-n" and direction != "-p":
print("%s should be either -n or -p" % direction)
sys.exit(1)
import re
r = re.compile('^[0-9]+$')
if not(r.match(num_steps)):
print("%s should be an integer" % num_steps)
sys.exit(2)
def main():
if len(sys.argv) == 4:
_, url, direction, num_steps = sys.argv
validate(url, direction, num_steps)
hubcrawl(url, direction, int(num_steps))
else:
print("Usage: ")
print(" hubcrawl http://hub/foo/2018/05/23/01/01/01/01/cxhDs -n 10")
print " or "
print(" hubcrawl http://hub/foo/2018/05/23/01/01/01/01/cxhDs -p 10")
if __name__ == '__main__':
try:
main()
except urllib2.HTTPError, KeyboardInterrupt:
try:
sys.exit(0)
except SystemExit:
os._exit(0)