This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from 18F/gather-abc
Abstract base class approach for gatherers
- Loading branch information
Showing
5 changed files
with
149 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from abc import ABCMeta, abstractmethod | ||
import os | ||
from typing import List | ||
|
||
|
||
class Gatherer(metaclass=ABCMeta): | ||
|
||
def __init__(self, suffixes: List[str], options: dict, extra: dict={}): | ||
self.suffixes = suffixes | ||
self.options = options | ||
self.extra = extra | ||
self.report_dir = self.options.get("output", "./") | ||
self.cache_dir = os.path.join(self.report_dir, "cache") | ||
self.results_dir = os.path.join(self.report_dir, "results") | ||
|
||
@abstractmethod | ||
def gather(self): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,39 @@ | ||
import os | ||
import requests | ||
import logging | ||
|
||
from utils import utils | ||
|
||
# Gathers hostnames from a CSV at a given URL. | ||
# | ||
# --url: The URL to download. Can also be a local path. | ||
# Will be parsed as a CSV. | ||
import requests | ||
|
||
from gatherers.gathererabc import Gatherer | ||
from utils import utils | ||
|
||
def gather(suffixes, options, extra={}, cache_dir="./cache"): | ||
# Defaults to --url, but can be overridden. | ||
name = extra.get("name", "url") | ||
url = options.get(name) | ||
|
||
if url is None: | ||
logging.warn("A --url is required. (Can be a local path.)") | ||
exit(1) | ||
class Gatherer(Gatherer): | ||
|
||
# remote URL | ||
if url.startswith("http:") or url.startswith("https:"): | ||
# Though it's saved in cache/, it will be downloaded every time. | ||
remote_path = os.path.join(cache_dir, "url.csv") | ||
def gather(self): | ||
# Defaults to --url, but can be overridden. | ||
name = self.extra.get("name", "url") | ||
url = self.options.get(name) | ||
|
||
try: | ||
response = requests.get(url) | ||
utils.write(response.text, remote_path) | ||
except: | ||
logging.error("Remote URL not downloaded successfully.") | ||
print(utils.format_last_exception()) | ||
if url is None: | ||
logging.warn("A --url is required. (Can be a local path.)") | ||
exit(1) | ||
|
||
# local path | ||
else: | ||
remote_path = url | ||
|
||
for domain in utils.load_domains(remote_path): | ||
yield domain | ||
# remote URL | ||
if url.startswith("http:") or url.startswith("https:"): | ||
# Though it's saved in cache/, it will be downloaded every time. | ||
remote_path = os.path.join(self.cache_dir, "url.csv") | ||
|
||
try: | ||
response = requests.get(url) | ||
utils.write(response.text, remote_path) | ||
except: | ||
logging.error("Remote URL not downloaded successfully.") | ||
print(utils.format_last_exception()) | ||
exit(1) | ||
|
||
# local path | ||
else: | ||
remote_path = url | ||
|
||
for domain in utils.load_domains(remote_path): | ||
yield domain |