from __future__ import unicode_literals
from builtins import bytes
import copy
import base64
import uuid
[docs]class ProxySettings(object):
__slots__ = ('token', 'session_id', 'country', 'session_group_id',
'project_id', 'tracking_id', 'target_id', 'fail_on_duplicate_ip', 'wiretap', 'prefer_ip', 'blockads', 'no_copy', 'pool')
def __init__(self, token, country=None, tracking_id=None, project_id=None, target_id=None, session_id=None, session_group_id=None, fail_on_duplicate_ip=None, wiretap=None, prefer_ip=None, blockads=None, pool=None):
self.update(token, country, tracking_id, project_id, target_id,
session_id, session_group_id, fail_on_duplicate_ip, wiretap, prefer_ip, blockads, pool)
def _newcopy(self):
if self.no_copy:
return self
return copy.copy(self)
def _geoproxies_proxy_option_name(self, name):
property_pxy_option_map = {
'session_id': 'session',
'session_group_id': 'sessionGroupId',
'project_id': 'projectId',
'tracking_id': 'trackingId',
'target_id': 'targetId',
'fail_on_duplicate_ip': 'failOnDuplicateIp',
'prefer_ip': 'preferIP',
'blockads': 'blockAds'
}
if name not in property_pxy_option_map:
return name
return property_pxy_option_map[name]
def _option_stringified(self):
pxy_options = ''
pipe = ''
for option in self.__slots__:
if option != 'token' and option != 'no_copy' and hasattr(self, option) and getattr(self, option):
pxy_options += pipe + \
self._geoproxies_proxy_option_name(
option) + '=' + str(getattr(self, option))
pipe = '|'
return pxy_options
def __str__(self):
return 'http://' + self.token + ':' + self._option_stringified() + '@proxy.geoproxies.com:1080'
[docs] def update(self, token=None, country=None, tracking_id=None, project_id=None, target_id=None, session_id=None, session_group_id=None, fail_on_duplicate_ip=None, wiretap=None, prefer_ip=None, blockads=None, pool=None):
'''
Returns instance of object with updated members
**Named arguments**
`token`: This is the authentication token provided by Geoproxies, the value is a UUID
`country`: Two-letter country codes defined in ISO 3166-1 for ex. mu for Mauritius, and fr for France etc
`tracking_id`: This entry is required to identify a single reques, so therefore should be unique at all time. You can opt out to use the new_tracking_id() method instead to generate a unique value
`project_id`: Option to categorize data use based on whatever characterization as deemed fit by the proxy user
`target_id`: Parameter to represent a domain. This is a needed input when the fine grain ability to control the usage of IP per domain is needed.
`session_id`: If tracking id represents a single request, then the session_id represents a set of related requests.
`session_group_id`: Use for grouping sessions and can also be use to control the provision of IP
`fail_on_duplicate_ip`: An optional truthy value to modify the Proxy behaviour when no unique IP is available in a serve a request. (Note) this option must be used with the session_group_id parameter
`wiretap`: Provide a truthy value to enable or disable the wiretapping functionality
`prefer_ip`: Use for requesting a particular IP address. IP is returned when available otherwise another IP is used.
`blockads`: Toggle option to reject known advert, analytics or attach url as provided on https://github.com/notracking/hosts-blocklists/
`pool`: Optional parameter to select either Residential or Static IP. Defaults to Static
For more detailed information about all the Proxy Option, please check the http://geoproxies.com/en/pDocs
'''
arguments = locals()
del arguments['self']
self.no_copy = True
for option, val in arguments.items():
if val and option in self.__slots__:
getattr(self, 'set_' + option)(val)
self.no_copy = False
return self
[docs] def get_components(self):
'''
Returns a dictionary of needed Proxy strings/components as required by most Request libraries
'''
option_stringified = self._option_stringified()
return {
'username': self.token,
'password': option_stringified,
'host': 'proxy.geoproxies.com',
'port': 1080,
'proxyurl': 'http://proxy.geoproxies.com:1080',
'authorization': 'Basic ' + base64.b64encode(bytes(self.token + option_stringified, 'utf-8')).decode('utf-8')
}
[docs] def get_api_url(self, name):
'''
Returns the proxy url for the api name specified
'''
return 'http://proxy.geoproxies.com:1080/' + name
[docs] def set_token(self, value):
'''
Clones and return the ProxySettings with the updated *token* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.token = value
return proxy_settings
[docs] def set_session_id(self, value):
'''
Clones and return the ProxySettings with the updated *session_id* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.session_id = value
return proxy_settings
[docs] def set_country(self, value):
'''
Clones and return the ProxySettings with the updated *country* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.country = value
return proxy_settings
[docs] def set_session_group_id(self, value):
'''
Clones and return the ProxySettings with the updated *session_group_id* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.session_group_id = value
return proxy_settings
[docs] def set_project_id(self, value):
'''
Clones and return the ProxySettings with the updated *project_id* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.project_id = value
return proxy_settings
[docs] def set_tracking_id(self, value):
'''
Clones and return the ProxySettings with the updated *tracking_id* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.tracking_id = value
return proxy_settings
[docs] def set_target_id(self, value):
'''
Clones and return the ProxySettings with the updated *target_id* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.target_id = value
return proxy_settings
[docs] def set_fail_on_duplicate_ip(self, value):
'''
Clones and return the ProxySettings with the updated *fail_on_duplicate_ip* attribute.
'''
if value:
return self.enable_fail_on_duplicate_ip()
else:
return self.disable_fail_on_duplicate_ip()
[docs] def enable_fail_on_duplicate_ip(self):
'''
Toggles on the fail_on_duplicate_ip functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.fail_on_duplicate_ip = 1
return proxy_settings
[docs] def disable_fail_on_duplicate_ip(self):
'''
Toggles off the fail_on_duplicate_ip functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.fail_on_duplicate_ip = None
return proxy_settings
[docs] def set_wiretap(self, value):
'''
Clones and return the ProxySettings with the updated *wiretap* attribute.
'''
if value:
return self.enable_wiretap()
else:
return self.disable_wiretap()
[docs] def enable_wiretap(self):
'''
Toggles on the wiretap functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.wiretap = 1
return proxy_settings
[docs] def disable_wiretap(self):
'''
Toggles off the wiretap functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.wiretap = None
return proxy_settings
[docs] def set_prefer_ip(self, value):
'''
Clones and return the ProxySettings with the updated *prefer_ip* attribute.
'''
proxy_settings = self._newcopy()
proxy_settings.prefer_ip = value
return proxy_settings
[docs] def set_blockads(self, value):
'''
Clones and return the ProxySettings with the updated *blockads* attribute.
'''
if value:
return self.enable_blockads()
else:
return self.disable_blockads()
[docs] def enable_blockads(self):
'''
Toggles on the blockads functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.blockads = 1
return proxy_settings
[docs] def disable_blockads(self):
'''
Toggles off the blockads functionality and returns the ProxySettings
'''
proxy_settings = self._newcopy()
proxy_settings.blockads = None
return proxy_settings
[docs] def set_pool(self, value):
'''
Select either from a pool of residential or hosted IP address and returns the ProxySettings
'''
proxy_settings = self._newcopy()
if value == 'residential' or value == 'static':
proxy_settings.pool = value
return proxy_settings
[docs] def get_token(self):
'''
Returns the current value of the *token* attribute.
'''
return self.token
[docs] def get_session_id(self):
'''
Returns the current value of the *session_id* attribute.
'''
return self.session_id
[docs] def get_country(self):
'''
Returns the current value of the *country* attribute.
'''
return self.country
[docs] def get_session_group_id(self):
'''
Returns the current value of the *session_group_id* attribute.
'''
return self.session_group_id
[docs] def get_project_id(self):
'''
Returns the current value of the *project_id* attribute.
'''
return self.project_id
[docs] def get_tracking_id(self):
'''
Returns the current value of the *tracking_id* attribute.
'''
return self.tracking_id
[docs] def get_target_id(self):
'''
Returns the current value of the *target_id* attribute.
'''
return self.target_id
[docs] def is_fail_on_duplicate_ip(self):
'''
Returns the current value of the *fail_on_duplicate_ip* attribute as a boolean.
'''
return bool(self.fail_on_duplicate_ip)
[docs] def is_wiretap(self):
'''
Returns the current value of the *wiretap* attribute as a boolean.
'''
return bool(self.wiretap)
[docs] def get_prefer_ip(self):
'''
Returns the current value of the *prefer_ip* attribute.
'''
return self.prefer_ip
[docs] def is_blockads(self):
'''
Returns the current value of the *blockads* attribute as a boolean.
'''
return bool(self.blockads)
[docs] def get_pool(self):
'''
Returns the current value of the *pool* attribute
'''
return self.pool or 'static'
[docs] def new_session_id(self):
'''
This method clones the ProxySettings, updates the *session_id* attribute with a random generated number and returns the new object
'''
proxy_settings = self.set_session_id(uuid.uuid4().hex)
return proxy_settings
[docs] def new_session_group_id(self):
'''
This method clones the ProxySettings, updates the *session_group_id* attribute with a random generated number and returns the new object
'''
proxy_settings = self.set_session_group_id(uuid.uuid4().hex)
return proxy_settings
[docs] def new_project_id(self):
'''
This method clones the ProxySettings, updates the *project_id* attribute with a random generated number and returns the new object
'''
proxy_settings = self.set_project_id(uuid.uuid4().hex)
return proxy_settings
[docs] def new_tracking_id(self):
'''
This method clones the ProxySettings, updates the *tracking_id* attribute with a random generated number and returns the new object
'''
proxy_settings = self.set_tracking_id(uuid.uuid4().hex)
return proxy_settings
[docs] def new_target_id(self):
'''
This method clones the ProxySettings, updates the *target_id* attribute with a random generated number and returns the new object
'''
proxy_settings = self.set_target_id(uuid.uuid4().hex)
return proxy_settings