Commit 5f73d332 authored by Francesco Poldi's avatar Francesco Poldi Committed by GitHub

Added proxy support (#225)

* Added proxy #139
parent 561297f5
......@@ -43,30 +43,6 @@ def check(args):
if args.user_full:
error("Error", "Please use --user-full with --followers or --following.")
# Proxy stuff
if args.proxy_host is not None:
import socks
import socket
if args.proxy_host.lower() == "tor":
socks.set_default_proxy(socks.SOCKS5, "localhost", 9050)
socket.socket = socks.socksocket
elif args.proxy_port and args.proxy_type:
if args.proxy_type.lower() == "socks5":
_type = socks.SOCKS5
elif args.proxy_type.lower() == "socks4":
_type = socks.SOCKS4
elif args.proxy_type.lower() == "http":
_type = socks.HTTP
else:
error("Error", "Proxy types allowed are: socks5, socks4, and http.")
socks.set_default_proxy(_type, args.proxy_host, int(args.proxy_port))
socket.socket = socks.socksocket
else:
error("Error", "Please specify --proxy-host, --proxy-port, and --proxy-type")
else:
if args.proxy_port or args.proxy_type:
error("Error", "Please specify --proxy-host, --proxy-port, and --proxy-type")
def loadUserList(ul, _type):
""" Concatenate users
"""
......
......@@ -5,3 +5,4 @@ cchardet
elasticsearch
pysocks
pandas
aiohttp_socks
\ No newline at end of file
from . import url
from .output import Tweets, Users
from async_timeout import timeout
from bs4 import BeautifulSoup
import sys
import aiohttp
import asyncio
import concurrent.futures
from aiohttp_socks import SocksConnector, SocksVer
from . import url
from .output import Tweets, Users
async def RequestUrl(config, init):
if config.Proxy_host is not None:
if config.Proxy_host.lower() == "tor":
connector = SocksConnector(
socks_ver=SocksVer.SOCKS5,
host='127.0.0.1',
port=9050,
rdns=True)
elif config.Proxy_port and config.Proxy_type:
if config.Proxy_type.lower() == "socks5":
_type = SocksVer.SOCKS5
elif config.Proxy_type.lower() == "socks4":
_type = SocksVer.SOCKS4
else:
print("Error: Proxy types allowed are: socks5 and socks4.")
sys.exit(1)
_connector = SocksConnector(
socks_ver=_type,
host=config.Proxy_host,
port=config.Proxy_port,
rdns=True)
else:
print("Error: Please specify --proxy-host, --proxy-port, and --proxy-type")
sys.exit(1)
else:
if config.Proxy_port or config.Proxy_type:
print("Error: Please specify --proxy-host, --proxy-port, and --proxy-type")
sys.exit(1)
if config.Profile:
if config.Profile_full:
_url = await url.MobileProfile(config.Username, init)
response = await MobileRequest(_url)
response = await MobileRequest(_url, _connector)
else:
_url = await url.Profile(config.Username, init)
response = await Request(_url)
response = await Request(_url, _connector)
elif config.TwitterSearch:
_url = await url.Search(config, init)
response = await Request(_url)
response = await Request(_url, _connector)
else:
if config.Following:
_url = await url.Following(config.Username, init)
......@@ -24,27 +57,25 @@ async def RequestUrl(config, init):
_url = await url.Followers(config.Username, init)
else:
_url = await url.Favorites(config.Username, init)
response = await MobileRequest(_url)
response = await MobileRequest(_url, _connector)
if config.Debug:
print(_url, file=open("twint-request_urls.log", "a", encoding="utf-8"))
return response
async def MobileRequest(url):
async def MobileRequest(url, connector):
ua = {'User-Agent': 'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/0.8.12'}
connect = aiohttp.TCPConnector(verify_ssl=False)
async with aiohttp.ClientSession(headers=ua, connector=connect) as session:
async with aiohttp.ClientSession(headers=ua, connector=connector) as session:
return await Response(session, url)
async def Request(url):
connect = aiohttp.TCPConnector(verify_ssl=False)
async with aiohttp.ClientSession(connector=connect) as session:
async def Request(url, connector):
async with aiohttp.ClientSession(connector=connector) as session:
return await Response(session, url)
async def Response(session, url):
with timeout(30):
async with session.get(url) as response:
async with session.get(url, ssl=False) as response:
return await response.text()
async def Username(_id):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment