Skip to content

Instantly share code, notes, and snippets.

@jlengrand jlengrand/tpp.py Secret
Created Aug 27, 2012

Embed
What would you like to do?
tpp - The Pirate Patch, Python flavored
import os
import urllib
import shutil
import re
import sys
# Global variables
platform = "win" # default is windows
folder = "C:\Windows\System32\drivers\etc"
host = os.path.join(folder, "hosts")
host_backup = os.path.join(folder, "hosts.tpp")
# Utilities
def clear():
"""
Clears the previous console output without OS issue
"""
if platform == "win":
os.system("cls")
else: # linux or mac
os.system("clear")
def define_platform():
"""
Finds on which OS we are.
Sets up paths and variables accordingly
"""
global host, host_backup, platform, folder
myos = sys.platform
if "win" in myos:
platform = "win"
#folder = "C:\Windows\System32\drivers\etc"
folder = "C:\Users\jll\Desktop"
else: # we are on linux or mac (or something really weird)
platform = "lin"
folder = "/etc"
host = os.path.join(folder, "hosts")
host_backup = os.path.join(folder, "hosts.tpp")
def parse_ip(myline):
"""
Parses a line, extracting the ip address and putting it first
"""
ip_addr = re.findall(r'[0-9]+(?:\.[0-9]+){3}', myline)[0]
#name = re.findall(r'[\w.-]+', myline)
name = myline.split(ip_addr)[0]
return ip_addr, name
# Core functions
def apply():
"""
Applies changes to host file
"""
clear()
shutil.copyfile(host, host_backup) # First, safe stuff
urllib.urlretrieve("http://elite.so/tpp/proxy.txt", "proxy.txt")
print ""
print " // Status: Applying sexy changes..."
print ""
hostfile = open(host, 'a')
proxyfile = open('proxy.txt', 'r')
for proxyline in proxyfile.readlines():
ip, addr = parse_ip(proxyline)
hostfile.write("\n" + ip + "\t" + addr)
print ""
print "// Status: OK!"
print ""
print "// Press any key to go back..."
raw_input("")
clear()
def restore():
"""
Removes all the changes we made
"""
clear()
print""
print""
print"/////////////////////////////////////////////////////////////////////"
print"// //"
print"// THE PIRATE PATCH - CODED BY QARIZMA //"
print"// //"
print"/////////////////////////////////////////////////////////////////////"
print""
print"// Status: Removing changes and traces..."
print""
patterns = ["thepiratebay.com",
"www.thepiratebay.com",
"thepiratebay.org",
"www.thepiratebay.org",
"thepiratebay.se",
"www.thepiratebay.se",
"www.baiedespirates.be",
"baiedespirates.be",
"www.depiraatbaai.be",
"depiraatbaai.be"]
outlines = []
hostfile = open(host, 'r')
for hostline in hostfile.readlines():
found = False
for pattern in patterns:
if pattern in hostline:
found = True
if not found:
outlines.append(hostline)
file(host, 'w').writelines(outlines)
print""
print"// Status: OK!"
print""
print"// Press any key to go back..."
raw_input("")
clear()
def update():
"""
Well, updates our changes
"""
restore()
apply()
def about():
"""
prints some more information
"""
clear()
print ""
print ""
print "/////////////////////////////////////////////////////////////////////"
print "// //"
print "// THE PIRATE PATCH - CODED BY QARIZMA //"
print "// //"
print "/////////////////////////////////////////////////////////////////////"
print ""
print "// ThePiratePatch is specially made for users that can't access"
print "// TPB thanks to their ISP. This patch fetches the latest proxy"
print "// and applies some small changes to your hosts file without losing"
print "// the users custom rules. There is no need for backup."
print "//"
print "// You can find me on www.elite.so"
print "// TPP website can be found here: www.elite.so/tpp"
print "//"
print "// Press any key to go back..."
raw_input("")
clear()
def start():
clear()
print ""
print ""
print "/////////////////////////////////////////////////////////////////////"
print "// //"
print "// THE PIRATE PATCH - CODED BY QARIZMA //"
print "// //"
print "// VERSION: 2.1 - RELEASE: 20-08-2012 - WWW.ELITE.SO/TPP //"
print "// //"
print "/////////////////////////////////////////////////////////////////////"
print ""
print "// Welcome to the new TPP!"
print "// Please let me know what to do :)"
print ""
print "// 1 = APPLY // 2 = RESTORE // 3 = UPDATE // 4 = ABOUT // 5 = EXIT"
print ""
def check_state(choice):
"""
State-machine, waiting for user inputs
"""
if (choice == 1):
apply()
elif (choice == 2):
restore()
elif (choice == 3):
update()
elif (choice == 4):
about()
elif (choice == 5):
sys.exit() # Exiting process
else:
print "Invalid choice, try again !"
# Main here
define_platform()
os.system('title ThePiratePatch!')
while (1):
start()
choice = raw_input("Choice : ")
check_state(int(choice))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.