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

Feature db #75

Merged
merged 8 commits into from
Aug 5, 2018
Merged
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
4 changes: 4 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
URL="YOUR_URL"
DATABASE_NAME="YOUR_DATABASE_NAME"
DATABASE_USERNAME="YOUR_DATABASE_USERNAME"
DATABASE_PASSWORD="YOUR_DATABASE_PASSWORD"
Binary file added __pycache__/settings.cpython-36.pyc
Binary file not shown.
51 changes: 51 additions & 0 deletions modules/savedb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import MySQLdb


def saveToDatabase(database, user, password, links):
"""
Connects to a MYSQL DB
Create MYSQL Table
Add link to the DB

Args:
data = data that is being stored in the database
user = username to login into MYSQL
password = password of MYSQL
link = URLs from the crawler
"""
if not database and not user and not password:
print("Wrong DB Credentials")
exit()

#Debug
#print("Database:", database, "\nuser:",user, "\npass:",password)
try:
db = MySQLdb.connect(host="localhost", # your host
user=user, # your username
passwd=password, # your password
db=database) # name of the data base

cur = db.cursor()

except Exception as e:
print "Error '{0}' occurred. Arguments {1}.".format(e.message, e.args):

try:
query = """ CREATE TABLE IF NOT EXISTS `tor_url` (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
link VARCHAR(30) NOT NULL UNIQUE,
reg_date TIMESTAMP)"""
cur.execute(query)
for link in links:
query = "INSERT IGNORE INTO `tor_url` (link) VALUES ('{0}')".format(link)
cur.execute(query)
#print(query)
db.commit()

except (MySQLdb.Error, MySQLdb.Warning, TypeError, ValueError) as e:
print(e)
return None
finally:
cur.close()
db.close()

4 changes: 4 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from dotenv import load_dotenv
from pathlib import Path # python3 only
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
23 changes: 20 additions & 3 deletions torBot.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import argparse
import socket
import socks
import os
import settings
from modules import (bcolors, getemails, pagereader, getweblinks, updater,
info, savefile)
info, savefile, savedb)

# GLOBAL CONSTS
LOCALHOST = "127.0.0.1"
Expand Down Expand Up @@ -148,7 +150,10 @@ def get_args():
action="store_true",
help=' '.join(("Info displays basic info of the",
"scanned site, (very slow)")))
return parser.parse_args()
parser.add_argument("-db", "--database",
action="store_true",
help="Specify a database to connect.")
args = parser.parse_args()


def main(conn=False):
Expand All @@ -164,6 +169,16 @@ def main(conn=False):
if args.update:
updater.updateTor()
exit()
if args.database:
if 'DATABASE_NAME' and 'DATABASE_USERNAME' and 'DATABASE_PASSWORD' in os.environ:
DATABASE_PASSWORD = os.getenv("DATABASE_PASSWORD")
DATABASE_USERNAME = os.getenv("DATABASE_USERNAME")
DATABASE_NAME = os.getenv("DATABASE_NAME")
#print("DB - ", DATABASE_NAME, " - ", DATABASE_USERNAME, " - ", DATABASE_PASSWORD)
else:
print("Wrong Database Configurations")
exit()

if not args.quiet:
header()
# If url flag is set then check for accompanying flag set. Only one
Expand All @@ -189,9 +204,11 @@ def main(conn=False):
ext=args.extension)
if args.save:
savefile.saveJson("Links", links)
if(args.database):
savedb.saveToDatabase(DATABASE_NAME, DATABASE_USERNAME, DATABASE_PASSWORD, links)
else:
print("usage: torBot.py [-h] [-v] [--update] [-q] [-u URL] [-s] [-m]",
"[-e EXTENSION] [-l] [-i]")
"[-e EXTENSION] [-l] [-i] [-db]")

print("\n\n")

Expand Down