Skip to content

Commit 078b707

Browse files
author
2knal
committed
Refactor code structure
Also updated README.md
1 parent e889b27 commit 078b707

File tree

22 files changed

+181
-379
lines changed

22 files changed

+181
-379
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,46 @@
1+
<div align="center">
2+
<img src="https://cdn.auth0.com/blog/wpad/logo.png" width=100px height=100px>
3+
4+
<br>
5+
<br>
6+
7+
[![](https://img.shields.io/badge/Made_with-requests-blue?style=for-the-badge)](https://requests.readthedocs.io/en/master/ "Requests")
8+
</div>
9+
110
# random_proxies
2-
Python package to generate random proxy on the fly!
11+
12+
### Python package to generate a random proxy on the fly!
13+
14+
## Features
15+
16+
- Supports `HTTP`, `HTTPS` or `SOCKS` proxy.
17+
> Currently support SOCKS over HTTP only with version 4.
18+
- Fetch specific country proxy by using country name or country code.
19+
- Fetch elite / transparent / anonymous proxies respectively.
20+
- Fetch directly from [free-proxy-list](https://free-proxy-list.net).
21+
- For better response time, fetch from an elasticsearch `cache_server`.
22+
- `cache_server` is updated via routines described [here](./random_proxies/cache_server/README.md)
23+
24+
25+
## Example usage
26+
27+
```bash
28+
git clone https://github.com/2knal/random_proxies.git`
29+
cd random_proxies/
30+
pip install -r requirements.txt
31+
```
32+
Open python interpreter. (Supports version 3.4+)
33+
34+
```python
35+
>>> from random_proxies import random_proxy
36+
>>> random_proxy()
37+
'23.101.2.247:81'
38+
```
39+
40+
Refer more examples [here](./examples/)
41+
42+
## TODO
43+
- [ ] Publish package version 0.0.1
44+
- [ ] Scrape proxies from other sources
45+
- [ ] Add support for SOCKS version 5
46+
- [ ] Add unit tests

examples/example.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
(Once the package is published)
3+
pip install random_proxies
4+
or
5+
Follow example usage to import the package
6+
'''
7+
8+
from random_proxies import random_proxy
9+
10+
'''
11+
Fetch directly from free-proxy-list.net
12+
13+
:param use_cache: Boolean value to fetch from cache_server or not (default: True)
14+
:return: returns `ip:port`
15+
:rtype: str
16+
'''
17+
random_proxy(use_cache=False)
18+
19+
'''
20+
Fetch by protocol name.
21+
22+
:param protocol: Protocol name, either http, https or socks (default: http)
23+
:return: returns `ip:port`
24+
:rtype: str
25+
'''
26+
random_proxy(protocol='https')
27+
28+
'''
29+
Fetch by country name or code.
30+
31+
:param country: Country name (default: None)
32+
:param code: Country code, should match with country name if added (default: None)
33+
:return: returns `ip:port`
34+
:rtype: str
35+
'''
36+
random_proxy(country='india')
37+
random_proxy(code='in')
38+
39+
'''
40+
Fetch by proxy standard.
41+
42+
:param standard: Proxy standard, either anonymous, elite proxy, transparent (default: anonymous)
43+
:return: returns `ip:port`
44+
:rtype: str
45+
'''
46+
random_proxy(standard='elite proxy')

proxies/random_proxies/db.py

Lines changed: 0 additions & 38 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, unicode_literals
33

4-
from proxies.random_proxies import random_proxy
4+
from random_proxies.proxies import random_proxy
File renamed without changes.
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
from time import time
77

8-
from proxies.cache_server.config import es
9-
from proxies.cache_server.config import is_good_proxy
10-
from proxies.cache_server.config import logger
11-
from proxies.cache_server.utils import add
8+
from random_proxies.cache_server.config import es
9+
from random_proxies.cache_server.config import is_good_proxy
10+
from random_proxies.cache_server.config import logger
11+
from random_proxies.cache_server.utils import add
1212

1313
def _clean():
1414
# Get all the proxies from proxies index
@@ -18,13 +18,14 @@ def _clean():
1818
# Delete those which arent good
1919
for proxy in proxies:
2020
ip = proxy['ip address'] + ':' + proxy['port']
21+
protocol = ('http', 'https')[proxy['https'] == 'yes']
22+
2123
# Implies SOCKS proxy
2224
if 'version' in proxy:
2325
ip = proxy['version'] + '://' + ip
24-
protocol = ('http', 'https')[proxy['https'] == 'yes']
25-
26+
protocol = 'http'
2627
try:
27-
# Only if it works
28+
# If it doesn't work
2829
if not is_good_proxy(ip, protocol=protocol):
2930
# Delete from proxies index
3031
es.delete(index='proxies', doc_type='proxy', id=ip)
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from elasticsearch import Elasticsearch
88

99
env_path = join(dirname(__file__), '.env')
10-
# print('ENV PATH:', env_path)
1110
load_dotenv(env_path)
1211

1312
# GLOBALS
@@ -27,7 +26,8 @@
2726
if not es.indices.exists(index='recents'):
2827
es.indices.create(index='recents', ignore=400)
2928

30-
from proxies.random_proxies.settings import BASE_URL, SSL_URL, SOCKS_URL
31-
from proxies.random_proxies.log import logger
32-
from proxies.random_proxies.utils import fetch, parse_response
33-
from proxies.random_proxies.proxy_health import is_good_proxy
29+
# Removing circular import
30+
from random_proxies.proxies.settings import BASE_URL, SSL_URL, SOCKS_URL
31+
from random_proxies.proxies.log import logger
32+
from random_proxies.proxies.utils import fetch, parse_response
33+
from random_proxies.proxies.proxy_health import is_good_proxy
File renamed without changes.
Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,26 @@
55

66
from time import time
77

8-
from proxies.cache_server.config import es
9-
from proxies.cache_server.config import fetch, parse_response
10-
from proxies.cache_server.config import is_good_proxy
11-
from proxies.cache_server.config import logger
12-
from proxies.cache_server.utils import add
13-
from proxies.cache_server.config import BASE_URL, SSL_URL, SOCKS_URL
8+
from random_proxies.cache_server.config import es
9+
from random_proxies.cache_server.config import fetch, parse_response
10+
from random_proxies.cache_server.config import is_good_proxy
11+
from random_proxies.cache_server.config import logger
12+
from random_proxies.cache_server.utils import add
13+
from random_proxies.cache_server.config import BASE_URL, SSL_URL, SOCKS_URL
1414

1515
def _check():
1616
urls = [BASE_URL, SSL_URL, SOCKS_URL]
1717
proxies = []
18+
1819
# Fetch all the proxies from these urls
1920
for url in urls:
20-
print('URL:', url)
2121
res = fetch(url)
2222
# Passing empty conditions so that
2323
proxies.extend(parse_response(res, {}))
2424

25-
print('Total proxies: ', len(proxies))
2625
# Check if they work
2726
working_proxies = []
28-
count = 0
2927
for proxy in proxies:
30-
count += 1
31-
print('Proxy count:', count)
3228
ip = proxy['ip address'] + ':' + proxy['port']
3329
protocol = ('http', 'https')[proxy['https'] == 'yes']
3430

0 commit comments

Comments
 (0)