from bs4 import BeautifulSoup
from mechanize import Browser
import subprocess
import requests
import socks
import socket
import time
import sys

subprocess.Popen("tor")
time.sleep(5)

proxies = {"http": "socks5://127.0.0.1:9050", "https": "socks5://127.0.0.1:9050"}

res = requests.get("http://inet-ip.info/ip", proxies=proxies)
print(res.text)
print()

 = []
 = []

def main(ids, retry=5):

    for id in ids:

        for i in range(retry):

            try:

                socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, "127.0.0.1", 9050) 
                socket.socket = socks.socksocket
                br = Browser()
                br.set_handle_robots(False)
                br.addheaders = [("User-agent", "Opera/9.30 (Nintendo Wii; U; ; 2047-7; en)")]
                br.open("", timeout=10.0)
                br.select_form(action="")
                br[""] = id
                br.submit()

                soupobject = BeautifulSoup(br.response().read(), "html.parser")
                mailaddresses = soupobject.find_all("")
                fullname = soupobject.find_all("")
                willwritedata = id

                for mailaddress in mailaddresses:
                    willwritedata += "," + mailaddress.text

                for fullname in fullname:
                    fullname = fullname.text

                print(fullname)
                print(willwritedata + "\n")
                .append(fullname + "," + willwritedata + "\n")

            except TypeError:
                print("")
                subprocess.run("taskkill /IM tor.exe /F")
                subprocess.Popen("tor")
                print()
                time.sleep(5)
                res = requests.get("http://inet-ip.info/ip", proxies=proxies)
                print(res.text)
                print()
                pass

            else:
                break

        else:
            print("5回試行しましたが実行できませんでした。")
            subprocess.run("taskkill /IM tor.exe /F")
            exit()

with open(".txt") as f:
    ids = f.read().splitlines()

main(ids, retry=5)

with open(".txt", "w", encoding="utf-8") as f:
    f.write("\n".join())
with open(".txt", "w", encoding="utf-8") as f:
    f.write("\n".join())

subprocess.run("taskkill /IM tor.exe /F")

print("\n終了")
input()
