We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Fix location to use lat/lng if directly provided instead of lookups


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pogom/proxy.py`
Content:
```
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 
4 import logging
5 import requests
6 import sys
7 import time
8 
9 from queue import Queue
10 from threading import Thread
11 from random import randint
12 
13 log = logging.getLogger(__name__)
14 
15 # Last used proxy for round-robin.
16 last_proxy = -1
17 
18 # Proxy check result constants.
19 check_result_ok = 0
20 check_result_failed = 1
21 check_result_banned = 2
22 check_result_wrong = 3
23 check_result_timeout = 4
24 check_result_exception = 5
25 check_result_empty = 6
26 check_result_max = 6  # Should be equal to maximal return code.
27 
28 
29 # Simple function to do a call to Niantic's system for
30 # testing proxy connectivity.
31 def check_proxy(proxy_queue, timeout, proxies, show_warnings, check_results):
32 
33     # Url for proxy testing.
34     proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'
35     proxy = proxy_queue.get()
36 
37     check_result = check_result_ok
38 
39     if proxy and proxy[1]:
40 
41         log.debug('Checking proxy: %s', proxy[1])
42 
43         try:
44             proxy_response = requests.post(proxy_test_url, '',
45                                            proxies={'http': proxy[1],
46                                                     'https': proxy[1]},
47                                            timeout=timeout)
48 
49             if proxy_response.status_code == 200:
50                 log.debug('Proxy %s is ok.', proxy[1])
51                 proxy_queue.task_done()
52                 proxies.append(proxy[1])
53                 check_results[check_result_ok] += 1
54                 return True
55 
56             elif proxy_response.status_code == 403:
57                 proxy_error = ("Proxy " + proxy[1] +
58                                " is banned - got status code: " +
59                                str(proxy_response.status_code))
60                 check_result = check_result_banned
61 
62             else:
63                 proxy_error = ("Wrong status code - " +
64                                str(proxy_response.status_code))
65                 check_result = check_result_wrong
66 
67         except requests.ConnectTimeout:
68             proxy_error = ("Connection timeout (" + str(timeout) +
69                            " second(s) ) via proxy " + proxy[1])
70             check_result = check_result_timeout
71 
72         except requests.ConnectionError:
73             proxy_error = "Failed to connect to proxy " + proxy[1]
74             check_result = check_result_failed
75 
76         except Exception as e:
77             proxy_error = e
78             check_result = check_result_exception
79 
80     else:
81         proxy_error = "Empty proxy server."
82         check_result = check_result_empty
83 
84     # Decrease output amount if there are lot of proxies.
85     if show_warnings:
86         log.warning('%s', repr(proxy_error))
87     else:
88         log.debug('%s', repr(proxy_error))
89     proxy_queue.task_done()
90 
91     check_results[check_result] += 1
92     return False
93 
94 
95 # Check all proxies and return a working list with proxies.
96 def check_proxies(args):
97 
98     source_proxies = []
99 
100     check_results = [0] * (check_result_max + 1)
101 
102     # Load proxies from the file. Override args.proxy if specified.
103     if args.proxy_file is not None:
104         log.info('Loading proxies from file.')
105 
106         with open(args.proxy_file) as f:
107             for line in f:
108                 # Ignore blank lines and comment lines.
109                 if len(line.strip()) == 0 or line.startswith('#'):
110                     continue
111                 source_proxies.append(line.strip())
112 
113         log.info('Loaded %d proxies.', len(source_proxies))
114 
115         if len(source_proxies) == 0:
116             log.error('Proxy file was configured but ' +
117                       'no proxies were loaded. Aborting.')
118             sys.exit(1)
119     else:
120         source_proxies = args.proxy
121 
122     # No proxies - no cookies.
123     if (source_proxies is None) or (len(source_proxies) == 0):
124         log.info('No proxies are configured.')
125         return None
126 
127     if args.proxy_skip_check:
128         return source_proxies
129 
130     proxy_queue = Queue()
131     total_proxies = len(source_proxies)
132 
133     log.info('Checking %d proxies...', total_proxies)
134     if (total_proxies > 10):
135         log.info('Enable "-v or -vv" to see checking details.')
136 
137     proxies = []
138 
139     for proxy in enumerate(source_proxies):
140         proxy_queue.put(proxy)
141 
142         t = Thread(target=check_proxy,
143                    name='check_proxy',
144                    args=(proxy_queue, args.proxy_timeout, proxies,
145                          total_proxies <= 10, check_results))
146         t.daemon = True
147         t.start()
148 
149     # This is painful but we need to wait here until proxy_queue is
150     # completed so we have a working list of proxies.
151     proxy_queue.join()
152 
153     working_proxies = len(proxies)
154 
155     if working_proxies == 0:
156         log.error('Proxy was configured but no working ' +
157                   'proxies were found. Aborting.')
158         sys.exit(1)
159     else:
160         other_fails = (check_results[check_result_failed] +
161                        check_results[check_result_wrong] +
162                        check_results[check_result_exception] +
163                        check_results[check_result_empty])
164         log.info('Proxy check completed. Working: %d, banned: %d, ' +
165                  'timeout: %d, other fails: %d of total %d configured.',
166                  working_proxies, check_results[check_result_banned],
167                  check_results[check_result_timeout],
168                  other_fails,
169                  total_proxies)
170         return proxies
171 
172 
173 # Thread function for periodical proxy updating.
174 def proxies_refresher(args):
175 
176     while True:
177         # Wait BEFORE refresh, because initial refresh is done at startup.
178         time.sleep(args.proxy_refresh)
179 
180         try:
181             proxies = check_proxies(args)
182 
183             if len(proxies) == 0:
184                 log.warning('No live proxies found. Using previous ones ' +
185                             'until next round...')
186                 continue
187 
188             args.proxy = proxies
189             log.info('Regular proxy refresh complete.')
190         except Exception as e:
191             log.exception('Exception while refresh proxies: %s', repr(e))
192 
193 
194 # Provide new proxy for a search thread.
195 def get_new_proxy(args):
196 
197     global last_proxy
198 
199     # If none/round - simply get next proxy.
200     if ((args.proxy_rotation is None) or (args.proxy_rotation == 'none') or
201             (args.proxy_rotation == 'round')):
202         if last_proxy >= len(args.proxy) - 1:
203             last_proxy = 0
204         else:
205             last_proxy = last_proxy + 1
206         lp = last_proxy
207     # If random - get random one.
208     elif (args.proxy_rotation == 'random'):
209         lp = randint(0, len(args.proxy) - 1)
210     else:
211         log.warning('Parameter -pxo/--proxy-rotation has wrong value. ' +
212                     'Use only first proxy.')
213         lp = 0
214 
215     return lp, args.proxy[lp]
216
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pogom/proxy.py b/pogom/proxy.py
--- a/pogom/proxy.py
+++ b/pogom/proxy.py
@@ -32,6 +32,10 @@
 
     # Url for proxy testing.
     proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'
+    proxy_test_ptc_url = 'https://sso.pokemon.com/sso/oauth2.0/authorize?' \
+                         'client_id=mobile-app_pokemon-go&redirect_uri=' \
+                         'https%3A%2F%2Fwww.nianticlabs.com%2Fpokemongo' \
+                         '%2Ferror'
     proxy = proxy_queue.get()
 
     check_result = check_result_ok
@@ -46,22 +50,42 @@
                                                     'https': proxy[1]},
                                            timeout=timeout)
 
-            if proxy_response.status_code == 200:
+            proxy_response_ptc = requests.get(proxy_test_ptc_url, '',
+                                              proxies={'http': proxy[1],
+                                                       'https': proxy[1]},
+                                              timeout=timeout,
+                                              headers={'User-Agent':
+                                                       'pokemongo/1 '
+                                                       'CFNetwork/811.4.18 '
+                                                       'Darwin/16.5.0',
+                                                       'Host':
+                                                       'sso.pokemon.com',
+                                                       'X-Unity-Version':
+                                                       '5.5.1f1'})
+
+            niantic_status = proxy_response.status_code
+            ptc_status = proxy_response_ptc.status_code
+
+            banned_status_codes = [403, 409]
+
+            if niantic_status == 200 and ptc_status == 200:
                 log.debug('Proxy %s is ok.', proxy[1])
                 proxy_queue.task_done()
                 proxies.append(proxy[1])
                 check_results[check_result_ok] += 1
                 return True
 
-            elif proxy_response.status_code == 403:
+            elif (niantic_status in banned_status_codes or
+                  ptc_status in banned_status_codes):
                 proxy_error = ("Proxy " + proxy[1] +
-                               " is banned - got status code: " +
-                               str(proxy_response.status_code))
+                               " is banned - got Niantic status code: " +
+                               str(niantic_status) + ", PTC status code: " +
+                               str(ptc_status))
                 check_result = check_result_banned
 
             else:
-                proxy_error = ("Wrong status code - " +
-                               str(proxy_response.status_code))
+                proxy_error = ("Wrong status codes - " + str(niantic_status) +
+                               ", " + str(ptc_status))
                 check_result = check_result_wrong
 
         except requests.ConnectTimeout:

{"golden_diff": "diff --git a/pogom/proxy.py b/pogom/proxy.py\n--- a/pogom/proxy.py\n+++ b/pogom/proxy.py\n@@ -32,6 +32,10 @@\n \n     # Url for proxy testing.\n     proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'\n+    proxy_test_ptc_url = 'https://sso.pokemon.com/sso/oauth2.0/authorize?' \\\n+                         'client_id=mobile-app_pokemon-go&redirect_uri=' \\\n+                         'https%3A%2F%2Fwww.nianticlabs.com%2Fpokemongo' \\\n+                         '%2Ferror'\n     proxy = proxy_queue.get()\n \n     check_result = check_result_ok\n@@ -46,22 +50,42 @@\n                                                     'https': proxy[1]},\n                                            timeout=timeout)\n \n-            if proxy_response.status_code == 200:\n+            proxy_response_ptc = requests.get(proxy_test_ptc_url, '',\n+                                              proxies={'http': proxy[1],\n+                                                       'https': proxy[1]},\n+                                              timeout=timeout,\n+                                              headers={'User-Agent':\n+                                                       'pokemongo/1 '\n+                                                       'CFNetwork/811.4.18 '\n+                                                       'Darwin/16.5.0',\n+                                                       'Host':\n+                                                       'sso.pokemon.com',\n+                                                       'X-Unity-Version':\n+                                                       '5.5.1f1'})\n+\n+            niantic_status = proxy_response.status_code\n+            ptc_status = proxy_response_ptc.status_code\n+\n+            banned_status_codes = [403, 409]\n+\n+            if niantic_status == 200 and ptc_status == 200:\n                 log.debug('Proxy %s is ok.', proxy[1])\n                 proxy_queue.task_done()\n                 proxies.append(proxy[1])\n                 check_results[check_result_ok] += 1\n                 return True\n \n-            elif proxy_response.status_code == 403:\n+            elif (niantic_status in banned_status_codes or\n+                  ptc_status in banned_status_codes):\n                 proxy_error = (\"Proxy \" + proxy[1] +\n-                               \" is banned - got status code: \" +\n-                               str(proxy_response.status_code))\n+                               \" is banned - got Niantic status code: \" +\n+                               str(niantic_status) + \", PTC status code: \" +\n+                               str(ptc_status))\n                 check_result = check_result_banned\n \n             else:\n-                proxy_error = (\"Wrong status code - \" +\n-                               str(proxy_response.status_code))\n+                proxy_error = (\"Wrong status codes - \" + str(niantic_status) +\n+                               \", \" + str(ptc_status))\n                 check_result = check_result_wrong\n \n         except requests.ConnectTimeout:\n", "issue": "Fix location to use lat/lng if directly provided instead of lookups\n\n", "before_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\nimport logging\nimport requests\nimport sys\nimport time\n\nfrom queue import Queue\nfrom threading import Thread\nfrom random import randint\n\nlog = logging.getLogger(__name__)\n\n# Last used proxy for round-robin.\nlast_proxy = -1\n\n# Proxy check result constants.\ncheck_result_ok = 0\ncheck_result_failed = 1\ncheck_result_banned = 2\ncheck_result_wrong = 3\ncheck_result_timeout = 4\ncheck_result_exception = 5\ncheck_result_empty = 6\ncheck_result_max = 6  # Should be equal to maximal return code.\n\n\n# Simple function to do a call to Niantic's system for\n# testing proxy connectivity.\ndef check_proxy(proxy_queue, timeout, proxies, show_warnings, check_results):\n\n    # Url for proxy testing.\n    proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'\n    proxy = proxy_queue.get()\n\n    check_result = check_result_ok\n\n    if proxy and proxy[1]:\n\n        log.debug('Checking proxy: %s', proxy[1])\n\n        try:\n            proxy_response = requests.post(proxy_test_url, '',\n                                           proxies={'http': proxy[1],\n                                                    'https': proxy[1]},\n                                           timeout=timeout)\n\n            if proxy_response.status_code == 200:\n                log.debug('Proxy %s is ok.', proxy[1])\n                proxy_queue.task_done()\n                proxies.append(proxy[1])\n                check_results[check_result_ok] += 1\n                return True\n\n            elif proxy_response.status_code == 403:\n                proxy_error = (\"Proxy \" + proxy[1] +\n                               \" is banned - got status code: \" +\n                               str(proxy_response.status_code))\n                check_result = check_result_banned\n\n            else:\n                proxy_error = (\"Wrong status code - \" +\n                               str(proxy_response.status_code))\n                check_result = check_result_wrong\n\n        except requests.ConnectTimeout:\n            proxy_error = (\"Connection timeout (\" + str(timeout) +\n                           \" second(s) ) via proxy \" + proxy[1])\n            check_result = check_result_timeout\n\n        except requests.ConnectionError:\n            proxy_error = \"Failed to connect to proxy \" + proxy[1]\n            check_result = check_result_failed\n\n        except Exception as e:\n            proxy_error = e\n            check_result = check_result_exception\n\n    else:\n        proxy_error = \"Empty proxy server.\"\n        check_result = check_result_empty\n\n    # Decrease output amount if there are lot of proxies.\n    if show_warnings:\n        log.warning('%s', repr(proxy_error))\n    else:\n        log.debug('%s', repr(proxy_error))\n    proxy_queue.task_done()\n\n    check_results[check_result] += 1\n    return False\n\n\n# Check all proxies and return a working list with proxies.\ndef check_proxies(args):\n\n    source_proxies = []\n\n    check_results = [0] * (check_result_max + 1)\n\n    # Load proxies from the file. Override args.proxy if specified.\n    if args.proxy_file is not None:\n        log.info('Loading proxies from file.')\n\n        with open(args.proxy_file) as f:\n            for line in f:\n                # Ignore blank lines and comment lines.\n                if len(line.strip()) == 0 or line.startswith('#'):\n                    continue\n                source_proxies.append(line.strip())\n\n        log.info('Loaded %d proxies.', len(source_proxies))\n\n        if len(source_proxies) == 0:\n            log.error('Proxy file was configured but ' +\n                      'no proxies were loaded. Aborting.')\n            sys.exit(1)\n    else:\n        source_proxies = args.proxy\n\n    # No proxies - no cookies.\n    if (source_proxies is None) or (len(source_proxies) == 0):\n        log.info('No proxies are configured.')\n        return None\n\n    if args.proxy_skip_check:\n        return source_proxies\n\n    proxy_queue = Queue()\n    total_proxies = len(source_proxies)\n\n    log.info('Checking %d proxies...', total_proxies)\n    if (total_proxies > 10):\n        log.info('Enable \"-v or -vv\" to see checking details.')\n\n    proxies = []\n\n    for proxy in enumerate(source_proxies):\n        proxy_queue.put(proxy)\n\n        t = Thread(target=check_proxy,\n                   name='check_proxy',\n                   args=(proxy_queue, args.proxy_timeout, proxies,\n                         total_proxies <= 10, check_results))\n        t.daemon = True\n        t.start()\n\n    # This is painful but we need to wait here until proxy_queue is\n    # completed so we have a working list of proxies.\n    proxy_queue.join()\n\n    working_proxies = len(proxies)\n\n    if working_proxies == 0:\n        log.error('Proxy was configured but no working ' +\n                  'proxies were found. Aborting.')\n        sys.exit(1)\n    else:\n        other_fails = (check_results[check_result_failed] +\n                       check_results[check_result_wrong] +\n                       check_results[check_result_exception] +\n                       check_results[check_result_empty])\n        log.info('Proxy check completed. Working: %d, banned: %d, ' +\n                 'timeout: %d, other fails: %d of total %d configured.',\n                 working_proxies, check_results[check_result_banned],\n                 check_results[check_result_timeout],\n                 other_fails,\n                 total_proxies)\n        return proxies\n\n\n# Thread function for periodical proxy updating.\ndef proxies_refresher(args):\n\n    while True:\n        # Wait BEFORE refresh, because initial refresh is done at startup.\n        time.sleep(args.proxy_refresh)\n\n        try:\n            proxies = check_proxies(args)\n\n            if len(proxies) == 0:\n                log.warning('No live proxies found. Using previous ones ' +\n                            'until next round...')\n                continue\n\n            args.proxy = proxies\n            log.info('Regular proxy refresh complete.')\n        except Exception as e:\n            log.exception('Exception while refresh proxies: %s', repr(e))\n\n\n# Provide new proxy for a search thread.\ndef get_new_proxy(args):\n\n    global last_proxy\n\n    # If none/round - simply get next proxy.\n    if ((args.proxy_rotation is None) or (args.proxy_rotation == 'none') or\n            (args.proxy_rotation == 'round')):\n        if last_proxy >= len(args.proxy) - 1:\n            last_proxy = 0\n        else:\n            last_proxy = last_proxy + 1\n        lp = last_proxy\n    # If random - get random one.\n    elif (args.proxy_rotation == 'random'):\n        lp = randint(0, len(args.proxy) - 1)\n    else:\n        log.warning('Parameter -pxo/--proxy-rotation has wrong value. ' +\n                    'Use only first proxy.')\n        lp = 0\n\n    return lp, args.proxy[lp]\n", "path": "pogom/proxy.py"}], "after_files": [{"content": "#!/usr/bin/python\n# -*- coding: utf-8 -*-\n\nimport logging\nimport requests\nimport sys\nimport time\n\nfrom queue import Queue\nfrom threading import Thread\nfrom random import randint\n\nlog = logging.getLogger(__name__)\n\n# Last used proxy for round-robin.\nlast_proxy = -1\n\n# Proxy check result constants.\ncheck_result_ok = 0\ncheck_result_failed = 1\ncheck_result_banned = 2\ncheck_result_wrong = 3\ncheck_result_timeout = 4\ncheck_result_exception = 5\ncheck_result_empty = 6\ncheck_result_max = 6  # Should be equal to maximal return code.\n\n\n# Simple function to do a call to Niantic's system for\n# testing proxy connectivity.\ndef check_proxy(proxy_queue, timeout, proxies, show_warnings, check_results):\n\n    # Url for proxy testing.\n    proxy_test_url = 'https://pgorelease.nianticlabs.com/plfe/rpc'\n    proxy_test_ptc_url = 'https://sso.pokemon.com/sso/oauth2.0/authorize?' \\\n                         'client_id=mobile-app_pokemon-go&redirect_uri=' \\\n                         'https%3A%2F%2Fwww.nianticlabs.com%2Fpokemongo' \\\n                         '%2Ferror'\n    proxy = proxy_queue.get()\n\n    check_result = check_result_ok\n\n    if proxy and proxy[1]:\n\n        log.debug('Checking proxy: %s', proxy[1])\n\n        try:\n            proxy_response = requests.post(proxy_test_url, '',\n                                           proxies={'http': proxy[1],\n                                                    'https': proxy[1]},\n                                           timeout=timeout)\n\n            proxy_response_ptc = requests.get(proxy_test_ptc_url, '',\n                                              proxies={'http': proxy[1],\n                                                       'https': proxy[1]},\n                                              timeout=timeout,\n                                              headers={'User-Agent':\n                                                       'pokemongo/1 '\n                                                       'CFNetwork/811.4.18 '\n                                                       'Darwin/16.5.0',\n                                                       'Host':\n                                                       'sso.pokemon.com',\n                                                       'X-Unity-Version':\n                                                       '5.5.1f1'})\n\n            niantic_status = proxy_response.status_code\n            ptc_status = proxy_response_ptc.status_code\n\n            banned_status_codes = [403, 409]\n\n            if niantic_status == 200 and ptc_status == 200:\n                log.debug('Proxy %s is ok.', proxy[1])\n                proxy_queue.task_done()\n                proxies.append(proxy[1])\n                check_results[check_result_ok] += 1\n                return True\n\n            elif (niantic_status in banned_status_codes or\n                  ptc_status in banned_status_codes):\n                proxy_error = (\"Proxy \" + proxy[1] +\n                               \" is banned - got Niantic status code: \" +\n                               str(niantic_status) + \", PTC status code: \" +\n                               str(ptc_status))\n                check_result = check_result_banned\n\n            else:\n                proxy_error = (\"Wrong status codes - \" + str(niantic_status) +\n                               \", \" + str(ptc_status))\n                check_result = check_result_wrong\n\n        except requests.ConnectTimeout:\n            proxy_error = (\"Connection timeout (\" + str(timeout) +\n                           \" second(s) ) via proxy \" + proxy[1])\n            check_result = check_result_timeout\n\n        except requests.ConnectionError:\n            proxy_error = \"Failed to connect to proxy \" + proxy[1]\n            check_result = check_result_failed\n\n        except Exception as e:\n            proxy_error = e\n            check_result = check_result_exception\n\n    else:\n        proxy_error = \"Empty proxy server.\"\n        check_result = check_result_empty\n\n    # Decrease output amount if there are lot of proxies.\n    if show_warnings:\n        log.warning('%s', repr(proxy_error))\n    else:\n        log.debug('%s', repr(proxy_error))\n    proxy_queue.task_done()\n\n    check_results[check_result] += 1\n    return False\n\n\n# Check all proxies and return a working list with proxies.\ndef check_proxies(args):\n\n    source_proxies = []\n\n    check_results = [0] * (check_result_max + 1)\n\n    # Load proxies from the file. Override args.proxy if specified.\n    if args.proxy_file is not None:\n        log.info('Loading proxies from file.')\n\n        with open(args.proxy_file) as f:\n            for line in f:\n                # Ignore blank lines and comment lines.\n                if len(line.strip()) == 0 or line.startswith('#'):\n                    continue\n                source_proxies.append(line.strip())\n\n        log.info('Loaded %d proxies.', len(source_proxies))\n\n        if len(source_proxies) == 0:\n            log.error('Proxy file was configured but ' +\n                      'no proxies were loaded. Aborting.')\n            sys.exit(1)\n    else:\n        source_proxies = args.proxy\n\n    # No proxies - no cookies.\n    if (source_proxies is None) or (len(source_proxies) == 0):\n        log.info('No proxies are configured.')\n        return None\n\n    if args.proxy_skip_check:\n        return source_proxies\n\n    proxy_queue = Queue()\n    total_proxies = len(source_proxies)\n\n    log.info('Checking %d proxies...', total_proxies)\n    if (total_proxies > 10):\n        log.info('Enable \"-v or -vv\" to see checking details.')\n\n    proxies = []\n\n    for proxy in enumerate(source_proxies):\n        proxy_queue.put(proxy)\n\n        t = Thread(target=check_proxy,\n                   name='check_proxy',\n                   args=(proxy_queue, args.proxy_timeout, proxies,\n                         total_proxies <= 10, check_results))\n        t.daemon = True\n        t.start()\n\n    # This is painful but we need to wait here until proxy_queue is\n    # completed so we have a working list of proxies.\n    proxy_queue.join()\n\n    working_proxies = len(proxies)\n\n    if working_proxies == 0:\n        log.error('Proxy was configured but no working ' +\n                  'proxies were found. Aborting.')\n        sys.exit(1)\n    else:\n        other_fails = (check_results[check_result_failed] +\n                       check_results[check_result_wrong] +\n                       check_results[check_result_exception] +\n                       check_results[check_result_empty])\n        log.info('Proxy check completed. Working: %d, banned: %d, ' +\n                 'timeout: %d, other fails: %d of total %d configured.',\n                 working_proxies, check_results[check_result_banned],\n                 check_results[check_result_timeout],\n                 other_fails,\n                 total_proxies)\n        return proxies\n\n\n# Thread function for periodical proxy updating.\ndef proxies_refresher(args):\n\n    while True:\n        # Wait BEFORE refresh, because initial refresh is done at startup.\n        time.sleep(args.proxy_refresh)\n\n        try:\n            proxies = check_proxies(args)\n\n            if len(proxies) == 0:\n                log.warning('No live proxies found. Using previous ones ' +\n                            'until next round...')\n                continue\n\n            args.proxy = proxies\n            log.info('Regular proxy refresh complete.')\n        except Exception as e:\n            log.exception('Exception while refresh proxies: %s', repr(e))\n\n\n# Provide new proxy for a search thread.\ndef get_new_proxy(args):\n\n    global last_proxy\n\n    # If none/round - simply get next proxy.\n    if ((args.proxy_rotation is None) or (args.proxy_rotation == 'none') or\n            (args.proxy_rotation == 'round')):\n        if last_proxy >= len(args.proxy) - 1:\n            last_proxy = 0\n        else:\n            last_proxy = last_proxy + 1\n        lp = last_proxy\n    # If random - get random one.\n    elif (args.proxy_rotation == 'random'):\n        lp = randint(0, len(args.proxy) - 1)\n    else:\n        log.warning('Parameter -pxo/--proxy-rotation has wrong value. ' +\n                    'Use only first proxy.')\n        lp = 0\n\n    return lp, args.proxy[lp]\n", "path": "pogom/proxy.py"}]}