mirror of
https://github.com/Mueller-Patrick/Betterzon.git
synced 2026-04-26 23:30:11 +00:00
BETTERZON-57: Adding utility sql functions
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
||||
@@ -0,0 +1,2 @@
|
||||
pymysql
|
||||
logging
|
||||
@@ -0,0 +1,42 @@
|
||||
import pymysql
|
||||
import os
|
||||
import logging
|
||||
|
||||
|
||||
def __getConnection__() -> pymysql.Connection:
|
||||
"""
|
||||
Opens a new pymysql connection and returns it
|
||||
:return: A pymysql Connection object
|
||||
"""
|
||||
logger = logging.getLogger()
|
||||
try:
|
||||
conn = pymysql.connect(
|
||||
user=os.environ['BETTERZON_CRAWLER_USER'],
|
||||
password=os.environ['BETTERZON_CRAWLER_PASSWORD'],
|
||||
host=os.environ['BETTERZON_CRAWLER_HOST'],
|
||||
port=3306,
|
||||
database=os.environ['BETTERZON_CRAWLER_DB']
|
||||
)
|
||||
|
||||
return conn
|
||||
except pymysql.Error as e:
|
||||
logger.error('SQL Connection error: %s', e)
|
||||
return
|
||||
|
||||
|
||||
def getShopsToCrawl() -> [int]:
|
||||
"""
|
||||
Queries the list of vendor IDs and returns them
|
||||
:return: The list of IDs
|
||||
"""
|
||||
conn = __getConnection__()
|
||||
cur = conn.cursor()
|
||||
|
||||
query = 'SELECT vendor_id FROM vendors'
|
||||
|
||||
cur.execute(query)
|
||||
|
||||
# Extract the IDs from the returned tuples into a list
|
||||
vendor_ids = list(map(lambda x: x[0], cur.fetchall()))
|
||||
|
||||
return vendor_ids
|
||||
Reference in New Issue
Block a user