Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue #17 #18

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Logger:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

def heading(self , text: str):
print(self.HEADER + text + self.ENDC)

def info(self , text: str):
print(self.OKCYAN + text + self.ENDC)

def warn(self , text: str):
print(self.WARNING + text + self.ENDC)

def error(self , text: str):
print(self.FAIL + text + self.ENDC)

def success(self , text: str):
print(self.OKGREEN + text + self.ENDC)

90 changes: 48 additions & 42 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
from datetime import datetime
from distutils.util import strtobool
import pandas as pd
from logger import Logger

from dotenv import load_dotenv

logger = Logger()

# Load settings
load_dotenv()
INCLUDE_GBIF_SEARCH = strtobool(os.getenv('INCLUDE_GBIF_SEARCH'))
Expand Down Expand Up @@ -89,52 +92,55 @@ def readArgs():
inputfile = arg
elif opt in ('-o', '--ofile'):
outputfile = arg
print('Input file:', inputfile)
print('Output file:', outputfile)
# print(bcolors.OKGREEN + 'Input file:', inputfile)
logger.success(f"Input file: {inputfile}")
logger.success(f"Output file: {outputfile}")
return inputfile, outputfile

if __name__ == '__main__':
# Setup File
inputfile, outputfile = readArgs()

# Read Input
scientific_names = []
if('.txt' in inputfile):
with open(inputfile, 'r') as filehandle:
scientific_names = [name.rstrip() for name in filehandle.readlines()]
elif('.csv' in inputfile):
scientific_names = pd.read_csv(inputfile)['Names'].tolist()
elif('.xlsx' in inputfile):
scientific_names = pd.read_excel(inputfile)['Names'].tolist()
try:
# Setup File
inputfile, outputfile = readArgs()

# Read Input
scientific_names = []
if('.txt' in inputfile):
with open(inputfile, 'r') as filehandle:
scientific_names = [name.rstrip() for name in filehandle.readlines()]
elif('.csv' in inputfile):
scientific_names = pd.read_csv(inputfile)['Names'].tolist()
elif('.xlsx' in inputfile):
scientific_names = pd.read_excel(inputfile)['Names'].tolist()

# Fetch and Write Output
f = open(outputfile, 'a')
print('Starting, it may take a while, please wait...')
for name in scientific_names:
# Non-scientific search tag enabled
if name[-2:] == '-n':
name = getScientificName(name[:-2])

print('Looking up:', name)

# Get Description
description = getDescription(name)
# Get GBIF Data from name
gbif_data = getGBIFData(name)
# Get GBIF Data from similar name
if gbif_data == 'Not Found' and AUTO_SEARCH_SIMILAR_SPECIES and description != 'Not Found':
similar_name = re.search(name.split()[0] + ' .+', description)
gbif_data = getGBIFData(similar_name)
# Get GBIF Data from first word
if gbif_data == 'Not Found' and name.split()[0] != name:
gbif_data = getGBIFData(name.split()[0])
f.write('### ' + name)
f.write('\n')
f.write(description or 'Not Found')
f.write('\n')
f.write(gbif_data or 'Not Found')
f.write('\n')
f.write('\n')
print('Done! :D')
# Fetch and Write Output
f = open(outputfile, 'a')
logger.warn('Starting, it may take a while, please wait...')
for name in scientific_names:
# Non-scientific search tag enabled
if name[-2:] == '-n':
name = getScientificName(name[:-2])

logger.info(f"Looking up: {name}")

# Get Description
description = getDescription(name)
# Get GBIF Data from name
gbif_data = getGBIFData(name)
# Get GBIF Data from similar name
if gbif_data == 'Not Found' and AUTO_SEARCH_SIMILAR_SPECIES and description != 'Not Found':
similar_name = re.search(name.split()[0] + ' .+', description)
gbif_data = getGBIFData(similar_name)
# Get GBIF Data from first word
if gbif_data == 'Not Found' and name.split()[0] != name:
gbif_data = getGBIFData(name.split()[0])
f.write('### ' + name)
f.write('\n')
f.write(description or 'Not Found')
f.write('\n')
f.write(gbif_data or 'Not Found')
f.write('\n')
f.write('\n')
logger.success("\nDone! :D")
except KeyboardInterrupt:
logger.error("Exiting...")