35 lines
773 B
Python
35 lines
773 B
Python
|
import logging
|
||
|
import os
|
||
|
|
||
|
import pymysql as pymysql
|
||
|
|
||
|
|
||
|
def get_connection() -> pymysql.Connection:
|
||
|
"""
|
||
|
Get a connection to SQL.
|
||
|
This function is used on the vServer for local testing
|
||
|
@return: pymysql connection object
|
||
|
"""
|
||
|
try:
|
||
|
if os.environ.get('IS_VSERVER') == 'true':
|
||
|
conn = pymysql.connect(
|
||
|
user=os.environ['vServer_SQL_User'],
|
||
|
password=os.environ['vServer_SQL_Password'],
|
||
|
host='localhost',
|
||
|
port=3306,
|
||
|
database='ka-news-yaa-crawler'
|
||
|
)
|
||
|
else:
|
||
|
conn = pymysql.connect(
|
||
|
user=os.environ['DB_USER'],
|
||
|
password=os.environ['DB_PASSWORD'],
|
||
|
host=os.environ['DB_HOST'],
|
||
|
port=3306,
|
||
|
database='ka-news-yaa-crawler'
|
||
|
)
|
||
|
|
||
|
return conn
|
||
|
except pymysql.Error as e:
|
||
|
logging.error('SQL Connection error: %s', e)
|
||
|
return None
|