def get_random_proxy(secure=False, error_rate=0.5, avg_resp_time=0.5): db = get_connection() where = "secure = %s and error_rate <= %s and average_response_time <= %s" % ( int(secure), error_rate, avg_resp_time) result = db.query( "SELECT * FROM `tabProxies` WHERE %s ORDER BY RANDOM() LIMIT 1" % where) if result: return to_addr(result)
def __init__(self, package, sync=False): if isinstance(package, (_pip.Distribution, _pip.DistInfoDistribution, _pip.EggInfoDistribution)): self.name = package.project_name self.current_version = package.version elif isinstance(package, _pip.InstallRequirement): self.name = package.name self.current_version = package.installed_version elif isinstance(package, dict): self.name = package["name"] self.current_version = package["version"] self.latest_version = package.get("latest_version") _db = db.get_connection() res = _db.query(""" SELECT * FROM `tabPackage` WHERE name = '%s' """ % self.name) if not res or sync: _pypi_info = _get_pypi_info(self.name, raise_err=False) or {} if not hasattr(self, "latest_version"): self.latest_version = _pypi_info.get("version") self.home_page = _pypi_info.get("home_page") if not res: _db.query(""" INSERT INTO `tabPackage` (name, latest_version, home_page, _created_at) VALUES ('%s', '%s', '%s', '%s') """ % (self.name, self.latest_version, self.home_page, datetime.now())) else: if sync: _db.query(""" UPDATE `tabPackage` SET latest_version = '%s', home_page = '%s', _updated_at = '%s' WHERE name = '%s' """ % (self.latest_version, self.home_page, datetime.now(), self.name)) else: self.latest_version = res["latest_version"] self.home_page = res["home_page"]
# imports - standard imports from datetime import datetime from functools import partial import os.path as osp import re # imports - module imports from pipupgrade.__attr__ import __name__ as NAME from pipupgrade import _pip, semver, request as req, db, log from pipupgrade.tree import Node as TreeNode from pipupgrade.util.string import kebab_case, strip from pipupgrade._compat import iteritems logger = log.get_logger() _db = db.get_connection() def _get_pypi_info(name, raise_err=True): url = "https://pypi.org/pypi/{}/json".format(name) res = req.get(url) info = None if res.ok: data = res.json() info = data["info"] else: if raise_err: res.raise_for_status() return info
# imports - standard imports import os.path as osp import re import csv from pipupgrade.config import PATH from pipupgrade.db import get_connection from pipupgrade.util.system import popen from pipupgrade.exception import PopenError from pipupgrade import log, db PROXY_COLUMNS = "host,port,secure,anonymity,country_code,available,error_rate,average_response_time" logger = log.get_logger(level=log.DEBUG) connection = db.get_connection() def save(values): connection.query(""" BEGIN TRANSACTION; %s COMMIT; """ % "\n".join([ "INSERT OR IGNORE INTO `tabProxies` (%s) VALUES (%s);" % (PROXY_COLUMNS, ",".join(map(lambda x: '"%s"' % x, v))) for v in values ]), script=True) def fetch(): dir_path = PATH["CACHE"]
def __init__(self, package, sync=False): logger.info("Initializing Package %s of type %s..." % (package, type(package))) if isinstance(package, (_pip.Distribution, _pip.DistInfoDistribution, _pip.EggInfoDistribution)): self.name = package.project_name self.current_version = package.version elif isinstance(package, _pip.InstallRequirement): self.name = package.name self.current_version = package.installed_version elif isinstance(package, dict): self.name = package["name"] self.current_version = package["version"] self.latest_version = package.get("latest_version") _db = db.get_connection() try: res = _db.query(""" SELECT * FROM `tabPackage` WHERE name = '%s' """ % self.name) except db.OperationalError as e: logger.warn("Unable to fetch package name. %s" % e) if not res or sync: logger.info("Fetching PyPI info for package %s..." % self) _pypi_info = _get_pypi_info(self.name, raise_err=False) or {} if not hasattr(self, "latest_version"): self.latest_version = _pypi_info.get("version") self.home_page = _pypi_info.get("home_page") if not res: try: logger.info("Attempting to INSERT package %s into database." % self) _db.query(""" INSERT INTO `tabPackage` (name, latest_version, home_page, _created_at) VALUES ('%s', '%s', '%s', '%s') """ % (self.name, self.latest_version, self.home_page, datetime.now())) except (db.IntegrityError, db.OperationalError) as e: logger.warn("Unable to save package name. %s" % e) else: if sync: logger.info( "Attempting to UPDATE package %s within database." % self) try: _db.query(""" UPDATE `tabPackage` SET latest_version = '%s', home_page = '%s', _updated_at = '%s' WHERE name = '%s' """ % (self.latest_version, self.home_page, datetime.now(), self.name)) except db.OperationalError as e: logger.warn("Unable to update package name. %s" % e) else: logger.info("Using cached info for package %s." % self) self.latest_version = res["latest_version"] self.home_page = res["home_page"]