Skip to content

Commit

Permalink
Initial support for getting wikidata images
Browse files Browse the repository at this point in the history
  • Loading branch information
davidebbo committed Jun 14, 2024
1 parent c095cd9 commit 2234f45
Show file tree
Hide file tree
Showing 4 changed files with 415 additions and 26 deletions.
28 changes: 2 additions & 26 deletions OZprivate/ServerScripts/Utilities/EoLQueryPicsNames.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from itertools import islice
from db_helper import connect_to_database

## Local packages
from getEOL_crops import subdir_name, get_credit, get_file_from_json_struct, convert_rating
Expand Down Expand Up @@ -642,32 +643,7 @@ def check_recently_inspected(sess, API_key, db_connection, batch_size, all_table
status_forcelist=[ 500, 502, 503, 504 ])
s.mount('https://', HTTPAdapter(max_retries=retries))

if args.database.startswith("sqlite://"):
from sqlite3 import dbapi2 as sqlite
db_connection = sqlite.connect(os.path.relpath(args.database[len("sqlite://"):], args.treedir))
datetime_now = "datetime('now')";
subs="?"

elif args.database.startswith("mysql://"): #mysql://<mysql_user>:<mysql_password>@localhost/<mysql_database>
import pymysql
match = re.match(r'mysql://([^:]+):([^@]*)@([^/]+)/([^?]*)', args.database.strip())
if match.group(2) == '':
#enter password on the command line, if not given (more secure)
if args.script:
pw = input("pw: ")
else:
from getpass import getpass
pw = getpass("Enter the sql database password: ")
else:
pw = match.group(2)
db_connection = pymysql.connect(user=match.group(1), passwd=pw, host=match.group(3), db=match.group(4), port=3306, charset='utf8mb4')
datetime_now = "NOW()"
diff_minutes=lambda a,b: 'TIMESTAMPDIFF(MINUTE,{},{})'.format(a,b)
subs="%s"
else:
logger.error("No recognized database specified: {}".format(args.database))
sys.exit()

db_connection, datetime_now, subs = connect_to_database(args.database, None, args.script)
batch_size=15
if args.UPDATE_FROM_RESERVATIONS:
db_curs = db_connection.cursor()
Expand Down
49 changes: 49 additions & 0 deletions OZprivate/ServerScripts/Utilities/db_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import configparser
import logging
import os
import re
import sys


logger = logging.getLogger(__name__)

def connect_to_database(database, treedir, script):
if database.startswith("sqlite://"):
from sqlite3 import dbapi2 as sqlite
db_connection = sqlite.connect(os.path.relpath(database[len("sqlite://"):], treedir))
datetime_now = "datetime('now')";
subs="?"

elif database.startswith("mysql://"): #mysql://<mysql_user>:<mysql_password>@localhost/<mysql_database>
import pymysql
match = re.match(r'mysql://([^:]+):([^@]*)@([^/]+)/([^?]*)', database.strip())
if match.group(2) == '':
#enter password on the command line, if not given (more secure)
if script:
pw = input("pw: ")
else:
from getpass import getpass
pw = getpass("Enter the sql database password: ")
else:
pw = match.group(2)
db_connection = pymysql.connect(user=match.group(1), passwd=pw, host=match.group(3), db=match.group(4), port=3306, charset='utf8mb4')
datetime_now = "NOW()"
subs="%s"
else:
logger.error("No recognized database specified: {}".format(database))
sys.exit()

return db_connection, datetime_now, subs

def read_config(config_file):
"""
Read the passed-in configuration file, defaulting to the standard appconfig.ini
"""

if config_file is None:
config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../../../private/appconfig.ini")

config = configparser.ConfigParser()

config.read(config_file)
return config
Loading

0 comments on commit 2234f45

Please sign in to comment.