Skip to content

Commit

Permalink
Merge branch 'master' into Arya_dev
Browse files Browse the repository at this point in the history
  • Loading branch information
aryadas98 committed Jun 13, 2018
2 parents 6f6a59c + 46c578d commit 70e2e7d
Show file tree
Hide file tree
Showing 5 changed files with 223 additions and 95 deletions.
99 changes: 99 additions & 0 deletions orbitdeterminator/database/initDatabase.py
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)
64 changes: 0 additions & 64 deletions orbitdeterminator/database/init_database.py

This file was deleted.

101 changes: 70 additions & 31 deletions orbitdeterminator/database/scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,78 @@
import requests
from bs4 import BeautifulSoup

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()
ts = time.time()
def database_connect():
"""
Initializes database connection and selects corresponding database
db = MySQLdb.connect(host="localhost", user="root", passwd="*****")
cursor = db.cursor()
cursor.execute('use cubesat;')
print('Database selected')
Args:
NIL
"""
Updating tables with new TLE values.
Return:
db : database object
d : connection flag (0: success)
"""

There are 3 attributes in the tables are: time, line1, line2
"""
success = 0
error = 0
for i in range(0, len(tle), 3):
md5_hash = hashlib.md5(tle[i].encode())
db = MySQLdb.connect(host="localhost", user="root", passwd="mysql")
cursor = db.cursor()
d = cursor.execute('use cubesat')
# print('Database selected')
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 update_tables(db):
"""
Updating tables with new TLE values.
There are 3 attributes in the tables are: time, line1, line2
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()

success = 0
error = 0
ts = time.time()
cursor = db.cursor()
for i in range(0, len(tle), 3):
sat_hash = string_to_hash(tle[i])

try:
sql = 'INSERT INTO %s values(\'%s,\', \'%s,\', \'%s,\');\
' %(str(sat_hash), str(ts), tle[i+1], tle[i+2])
cursor.execute(sql)
d = db.commit()
except Exception:
error = error + 1
# print(tle[i] + ' - Error: Table not found')
else:
success = success + 1

print('Tables updated : ' + str(success))
# print('Error/Total : ' + str(error) + '/' + str(error+success))
db.close()

try:
sql = 'INSERT INTO %s values(\'%s,\', \'%s,\', \'%s,\');\
' %(str(sat_hash), str(ts), tle[i+1], tle[i+2])
cursor.execute(sql)
db.commit()
except Exception:
error = error + 1
# print(tle[i] + ' - Error: Table not found')
else:
success = success + 1

print('Tables updated : ' + str(success))
# print('Error/Total : ' + str(error) + '/' + str(error+success))
db.close()
if __name__ == "__main__":
db,_ = database_connect()
update_tables(db)
27 changes: 27 additions & 0 deletions orbitdeterminator/tests/test_initDatabase.py
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()
27 changes: 27 additions & 0 deletions orbitdeterminator/tests/test_scraper.py
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()

0 comments on commit 70e2e7d

Please sign in to comment.