Skip to content

Commit

Permalink
Add the -r flag, letting you grab a path relative to the match
Browse files Browse the repository at this point in the history
  • Loading branch information
cortesi committed Nov 23, 2015
1 parent fc2e7c4 commit 9b7909c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions ghrabber.py
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
import argparse import argparse
import os.path import os.path
import urllib


import BeautifulSoup import BeautifulSoup
import requests import requests
Expand Down Expand Up @@ -40,7 +41,7 @@ def make_fname(p):
return parts[0] + "." + parts[1] return parts[0] + "." + parts[1]




def get(query, outdir, listonly=False): def get(query, relative, outdir, listonly=False):
page = 1 page = 1
while 1: while 1:
params = dict( params = dict(
Expand All @@ -54,6 +55,8 @@ def get(query, outdir, listonly=False):
break break
for u in extract(r.content): for u in extract(r.content):
ru = raw_url(u) ru = raw_url(u)
if relative:
ru = urllib.basejoin(ru, relative)
if listonly: if listonly:
print ru print ru
else: else:
Expand Down Expand Up @@ -83,11 +86,15 @@ def get(query, outdir, listonly=False):
"-o", type=str, default=".", "-o", type=str, default=".",
help="Output directory. Created if it doesn't exist." help="Output directory. Created if it doesn't exist."
) )
parser.add_argument(
"-r", type=str, default=None,
help="Grab a path relative to the match"
)
parser.add_argument("query", type=str, help="Github Code Search query") parser.add_argument("query", type=str, help="Github Code Search query")
args = parser.parse_args() args = parser.parse_args()
if not os.path.exists(args.o): if not os.path.exists(args.o):
os.makedirs(args.o) os.makedirs(args.o)
try: try:
get(args.query, args.o, listonly=args.l) get(args.query, args.r, args.o, listonly=args.l)
except KeyboardInterrupt: except KeyboardInterrupt:
pass pass

0 comments on commit 9b7909c

Please sign in to comment.