#!/usr/bin//env python

import requests, subprocess, threading
from bs4 import BeautifulSoup
process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
HTTP, HTTPS, lastHTTP, lastHTTPS = "", "", "", ""

def change_proxy():
  threading.Timer(120.0, change_proxy).start()
  r = requests.get('http://free-proxy-list.net/anonymous-proxy.html')
  soup = BeautifulSoup(r.text, 'html.parser')
  td = soup.find_all('td')
  global HTTP, HTTPS, lastHTTP, lastHTTPS

  for index in range(0, len(td), 8):
    if td[index+6].string == 'no' and lastHTTP != td[index].string+':'+td[index+1].string:
      HTTP = td[index].string+':'+td[index+1].string
    elif td[index+6].string == 'yes' and lastHTTPS != td[index].string+':'+td[index+1].string:
      HTTPS = td[index].string+':'+td[index+1].string
    if HTTP != lastHTTP and HTTPS != lastHTTPS:
      lastHTTP, lastHTTPS = HTTP, HTTPS
      break
  print 'Changing proxy....', HTTP, HTTPS

def run_command(command):
  process = subprocess.Popen(['/bin/bash'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
  process.stdin.write('export http_proxy=http://'+HTTP+'\n')
  process.stdin.write('export https_proxy=https://'+HTTPS+'\n')
  process.stdin.write(command+'\n')
  output, err = process.communicate()
  print output

change_proxy()

while True:
    command = raw_input('$ ')
    run_command(command)

