summary refs log tree commit diff
diff options
context:
space:
mode:
authorSergey M․ <dstftw@gmail.com>2017-02-21 23:00:43 +0700
committerSergey M․ <dstftw@gmail.com>2017-02-21 23:00:43 +0700
commite39b5d4ab83de7a466c6d4c9528d385758566b22 (patch)
tree72c3751b0f3a582d43b8fc58c8fcb3e8207a53ba
parente469ab25280433781881d0c3ea6fd423ac5fea71 (diff)
downloadyoutube-dl-e39b5d4ab83de7a466c6d4c9528d385758566b22.tar.gz
youtube-dl-e39b5d4ab83de7a466c6d4c9528d385758566b22.tar.xz
youtube-dl-e39b5d4ab83de7a466c6d4c9528d385758566b22.zip
[extractor/common] Allow calling _initialize_geo_bypass from extractors (#11970)
-rw-r--r--youtube_dl/extractor/common.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/youtube_dl/extractor/common.py b/youtube_dl/extractor/common.py
index 1ae264722..86aff3312 100644
--- a/youtube_dl/extractor/common.py
+++ b/youtube_dl/extractor/common.py
@@ -379,12 +379,31 @@ class InfoExtractor(object):
 
     def initialize(self):
         """Initializes an instance (authentication, etc)."""
-        self.__initialize_geo_bypass()
+        self._initialize_geo_bypass(self._GEO_COUNTRIES)
         if not self._ready:
             self._real_initialize()
             self._ready = True
 
-    def __initialize_geo_bypass(self):
+    def _initialize_geo_bypass(self, countries):
+        """
+        Initialize geo restriction bypass mechanism.
+
+        This method is used to initialize geo bypass mechanism based on faking
+        X-Forwarded-For HTTP header. A random country from provided country list
+        is selected and a random IP brlonging to this country is generated. This
+        IP will be passed as X-Forwarded-For HTTP header in all subsequent
+        HTTP requests.
+        Method does nothing if no countries are specified.
+
+        This method will be used for initial geo bypass mechanism initialization
+        during the instance initialization with _GEO_COUNTRIES.
+
+        You may also manually call it from extractor's code if geo countries
+        information is not available beforehand (e.g. obtained during
+        extraction) or due to some another reason.
+        """
+        if not countries:
+            return
         if not self._x_forwarded_for_ip:
             country_code = self._downloader.params.get('geo_bypass_country', None)
             # If there is no explicit country for geo bypass specified and
@@ -393,8 +412,8 @@ class InfoExtractor(object):
             if (not country_code and
                     self._GEO_BYPASS and
                     self._downloader.params.get('geo_bypass', True) and
-                    self._GEO_COUNTRIES):
-                country_code = random.choice(self._GEO_COUNTRIES)
+                    countries):
+                country_code = random.choice(countries)
             if country_code:
                 self._x_forwarded_for_ip = GeoUtils.random_ipv4(country_code)
                 if self._downloader.params.get('verbose', False):