Python/Anything - Alter lines from text file, Randomize, Then save on specific line

294 views Asked by At

This is a bit of a an odd question but I'll make it out the best I can. I'm in the works on writing a script with the use of proxies, but I ran in to a problem automating part of it, which sucks as it's the last part of everything that I need then I'm done.

Example: Lets say I have a list of 200 or so proxies in this format stored in a .txt file:

110.138.183.60:8080
110.138.20.67:8080
110.138.208.116:8008
110.138.237.80:3128
110.138.248.17:8080
110.138.248.78:80
110.139.182.234:8080
ect ect ect...

First part of this script is I need to randomize them, so just mix them all up in different orders but making sure they all end up on one line each as they are now.

Second part after randomizing them, and then taking the first 99 of the randomized proxies and moving them in to one line in this format, ignoring any proxies after the 99th. (Separated by |, without one at the beginning or end. )

110.138.183.60:8080|110.138.208.116:8008|110.138.237.80:3128|110.138.248.17:8080|110.138.248.78:80|110.139.182.234:8080|110.138.20.67:8080

Third part is I need this new line of proxies to edit and replace a line in a file. The line would currently look something like this:

user_pref("extensions.proxytool.http_proxies", "129.59.26.40:8909|87.106.143.132:3128|85.17.121.205:9090|88.85.125.78:8080|80.82.150.82:8080");

Where you see the proxies in that line, I need to replace them with the new batch of proxies we just created.

End Result looking something like this:

...
user_pref("extensions.proxytool.clear_cookies_on_new_window", true);
user_pref("extensions.proxytool.firstrun", false);
user_pref("extensions.proxytool.http_proxies", "110.138.183.60:8080|110.138.208.116:8008|110.138.237.80:3128|110.138.248.17:8080|110.138.248.78:80|110.139.182.234:8080|110.138.20.67:8080");
user_pref("extensions.proxytool.proxy_type", "http");
user_pref("extensions.proxytool.referer", "default");
...

I wouldn't ask so much if I wasn't desperate, I'm literally on the last hair of my project before completing it. Any help is greatly appreciated! Thanks in advance!

EDIT: Script complete. Credit goes to Zack Bloom for the help! I got a little lazy with how the python script works, instead I have created a default prefs file with an empty proxy line, so outside the python script, a default prefs file is copied over in to my program, and then the python script does it's magic and replaces the line "proxies go here" with the newly compiled list. Thanks again Zack!

from random import sample

with open("C:/Users/USERACCOUNT/test/today.txt") as proxy_file:
    proxies = [proxy.strip() for proxy in proxy_file.readlines()]

    proxies = sample(proxies, min(len(proxies), 99))

    proxy_str = "|".join(proxies)

import fileinput
import sys

def replaceAll(file,searchExp,replaceExp):
    for line in fileinput.input(file, inplace=1):
        if searchExp in line:
            line = line.replace(searchExp,replaceExp)
        sys.stdout.write(line)

replaceAll('C:/Users/USERACCOUNT/test/prefs.js','proxiesgohere',proxy_str)
1

There are 1 answers

3
Zack Bloom On BEST ANSWER

Creating the list is simple enough:

from random import sample

with open("proxy_list.txt") as proxy_file:
    proxies = [proxy.strip() for proxy in proxy_file.readlines()]

    proxies = sample(proxies, min(len(proxies), 99))

    proxy_str = "|".join(proxies)

Then you must use a templating language (there's one in the std lib: http://docs.python.org/library/string.html#template-strings) to create your output file with the string included.