def get_or_create(self, session=None, update_defaults=None): session = session or create_session() defaults = { 'updated_at': datetime.datetime.now(), 'results': 0, } defaults.update(update_defaults or {}) return get_or_create(session, ProviderRequest, defaults=defaults, request_id=self.id, provider=self.provider.name)
def process_proxies(self, proxies): session = create_session() for proxy in proxies: instance, exists = get_or_create(session, Proxy, defaults=dict(votes=0), id='http://{}'.format(proxy['proxy'])) if not instance.country: instance.country = ip_country(get_domain(instance.id)) instance.votes += UPDATE_VOTES session.commit() return proxies
def process_proxies(self, proxies, session=None, update_votes=UPDATE_VOTES): session = session or create_session() proxy_instances = [] for proxy in proxies: protocol = proxy.get('protocol', 'http') instance, exists = get_or_create(session, Proxy, defaults=dict(votes=0, protocol=protocol), id='{}://{}'.format( protocol, proxy['proxy'])) if not instance.country: detected_country = ip_country(get_domain(instance.id)) instance.country = detected_country or proxy.get( 'country_code') or '' instance.votes += update_votes proxy_instances.append(instance) session.commit() return proxy_instances