-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
223 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
""" | ||
This code creates database and tables to each satellite with their md5 hash as table name. | ||
There are 2 codes for database: init_database.py and scraper.py | ||
Important: Run init_database.py before scraper.py | ||
Note: Password field is empty during database connection. Insert password for | ||
mysql authentication. | ||
""" | ||
|
||
import hashlib | ||
import MySQLdb | ||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
def create_database(): | ||
""" | ||
Creating database named "cubesat" | ||
Args: | ||
NIL | ||
Return: | ||
db : database object | ||
d : connection flag (0: success) | ||
""" | ||
|
||
db = MySQLdb.connect(host="localhost", user="root", passwd="mysql") | ||
cursor = db.cursor() | ||
sql = 'CREATE DATABASE cubesat;' | ||
cursor.execute(sql) | ||
# print('Database created') | ||
d = cursor.execute('use cubesat') | ||
# print('Database selected') | ||
# print(d) | ||
return db, d | ||
|
||
def string_to_hash(tle): | ||
""" | ||
Converts satellite name to its corresponding md5 hexadecimal hash | ||
Args: | ||
tle : satellite name | ||
Return: | ||
sat_hash : md5 hexadecimal hash | ||
""" | ||
|
||
md5_hash = hashlib.md5(tle.encode()) | ||
sat_hash = md5_hash.hexdigest() | ||
return sat_hash | ||
|
||
def create_tables(db): | ||
""" | ||
Creating tables in the database | ||
Args: | ||
db : database object | ||
Return: | ||
NIL | ||
""" | ||
page = requests.get("https://www.celestrak.com/NORAD/elements/cubesat.txt") | ||
soup = BeautifulSoup(page.content, 'html.parser') | ||
tle = list(soup.children) | ||
tle = tle[0].splitlines() | ||
cursor = db.cursor() | ||
|
||
sql = 'CREATE TABLE mapping ' + '\ | ||
(sat_name varchar(50), md5_hash varchar(50));' | ||
cursor.execute(sql) | ||
|
||
success = 0 | ||
error = 0 | ||
for i in range(0, len(tle), 3): | ||
sat_hash = string_to_hash(tle[i]) | ||
|
||
try: | ||
sql = 'CREATE TABLE ' + str(sat_hash) + '\ | ||
(time varchar(30), line1 varchar(70), line2 varchar(70));' | ||
cursor.execute(sql) | ||
except Exception: | ||
error = error + 1 | ||
print(tle[i] + ' - ' + sat_hash) | ||
else: | ||
sql = 'INSERT INTO mapping values(\'%s,\', \'%s\');\ | ||
' %(str(tle[i]), str(sat_hash)) | ||
cursor.execute(sql) | ||
db.commit() | ||
success = success + 1 | ||
|
||
print('Tables created : ' + str(success)) | ||
# print('Error/Total : ' + str(error) + '/' + str(error+success)) | ||
db.close() | ||
|
||
if __name__ == "__main__": | ||
db,_ = create_database() | ||
create_tables(db) |
This file was deleted.
Oops, something went wrong.
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,27 @@ | ||
import sys | ||
import os.path | ||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) | ||
|
||
import pytest | ||
import MySQLdb | ||
from numpy.testing import assert_array_equal | ||
from database.init_database import create_database, string_to_hash | ||
|
||
def test_create(): | ||
_,flag = create_database() | ||
assert(flag == 0) | ||
|
||
""" | ||
The satellite name in the TLE is max 24 character in length. Hence, the below | ||
function is taking it with the rest whitespaces | ||
""" | ||
def test_hash(): | ||
md5 = string_to_hash("SWISSCUBE ") | ||
assert(md5 == "3ca5eaf04b8badf4564ef8f753c56e12") | ||
|
||
md5 = string_to_hash("EXOCUBE ") | ||
assert(md5 == "8559e7a3093811263cd8b251f7bac5b7") | ||
|
||
if __name__ == "__main__": | ||
test_create() | ||
test_hash() |
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,27 @@ | ||
import sys | ||
import os.path | ||
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir))) | ||
|
||
import pytest | ||
import MySQLdb | ||
from numpy.testing import assert_array_equal | ||
from database.scraper import database_connect, string_to_hash | ||
|
||
def test_connection(): | ||
_,connect = database_connect() | ||
assert(connect == 0) | ||
|
||
""" | ||
The satellite name in the TLE is max 24 character in length. Hence, the below | ||
function is taking it with the rest whitespaces | ||
""" | ||
def test_hash(): | ||
md5 = string_to_hash("SWISSCUBE ") | ||
assert(md5 == "3ca5eaf04b8badf4564ef8f753c56e12") | ||
|
||
md5 = string_to_hash("EXOCUBE ") | ||
assert(md5 == "8559e7a3093811263cd8b251f7bac5b7") | ||
|
||
if __name__ == "__main__": | ||
test_connection() | ||
test_hash() |