Inactive twitch names go available january 29th so I decided to make this. Reads names to check from a file, writes names that work to a file, works fast, and has proper error handling.
Get it here: https://github.com/threebarber/twitch
(Jan 8, 2017 09:54 AM)Jcb da Multi Wrote: lol why'd u want a OG twitch...
cool man
Nice man! I had one on my old hard drive that got fucked up :(. I would take a look at some names but I am kind of bitter after Twitch banned my two letter account without reason and refused to do anything about it :(.
Gonna use this as a inspiration, to go code my own, im going to make it in vb.net instead though
Thank you for open sourcing this compact nifty script.
Quite nice mate, will try and grab myself a name or two on the 29th ;)
You really over-complicated it, and in the process made it less readable and more slow.
1) The HTTP status code 404 is returned. You could have just checked the status code since they could feasibly change their 404 page at any time, but are not going to break HTTP standards.
2) It's synchronous and very slow. It is trivial to speed this up with simple multithreading.
3)
If you really want to use requests, then I definitely suggest using threading. The multiprocessing module has a little-known ( and not even document) and yet incredibly useful thread pool class that can be used even before 2.7.
This code will run a lot faster and is, in my opinion, fairly readable. It can also run on Python 2.7/3.X.
PHP Code
<?php from multiprocessing.pool import ThreadPool import re import requests import sys
try: input = raw_input except NameError: pass
TWITCH_URL = "https://www.twitch.tv/{username}" TWITCH_REGEX = re.compile(r"^[a-zA-Z0-9_]{4,25}$") MAX_THREADS = 25 MESSAGES = {True: "Available", False: "Taken"}
def read_valid_usernames(filename): """Reads a list of usernames and filters out invalid ones.""" try: with open(filename, "r") as fin: return [username for username in map(str.strip, fin) if TWITCH_REGEX.match(username)] except IOError: sys.exit("[!] '{}' - Invalid File".format(filename))
def username_available(username): """Checks if a 404 response code is given when requesting the profile. If it is, it is presumed to be available""" try: return username, requests.get(TWITCH_URL.format(username=username)).status_code == 404 except Exception as e: print(e)
usernames = read_valid_usernames(input("Enter path to list of usernames: "))
for username, available in ThreadPool(MAX_THREADS).imap_unordered(username_available, usernames): print("{:<{size}}{}".format(username, MESSAGES.get(available, "Unknown"), size=len(max(usernames, key=len)) + 1))
(Jan 9, 2017 02:07 AM)Goodies Wrote: You really over-complicated it, and in the process made it less readable and more slow.
1) The HTTP status code 404 is returned. You could have just checked the status code since they could feasibly change their 404 page at any time, but are not going to break HTTP standards.
2) It's synchronous and very slow. It is trivial to speed this up with simple multithreading.
3)
If you really want to use requests, then I definitely suggest using threading. The multiprocessing module has a little-known (and not even document) and yet incredibly useful thread pool class that can be used even before 2.7.
This code will run a lot faster and is, in my opinion, fairly readable. It can also run on Python 2.7/3.X.
PHP Code
<?php from multiprocessing.pool import ThreadPool import re import requests import sys
try: input = raw_input except NameError: pass
TWITCH_URL = "https://www.twitch.tv/{username}" TWITCH_REGEX = re.compile(r"^[a-zA-Z0-9_]{4,25}$") MAX_THREADS = 25 MESSAGES = {True: "Available", False: "Taken"}
def read_valid_usernames(filename): """Reads a list of usernames and filters out invalid ones.""" try: with open(filename, "r") as fin: return [username for username in map(str.strip, fin) if TWITCH_REGEX.match(username)] except IOError: sys.exit("[!] '{}' - Invalid File".format(filename))
def username_available(username): """Checks if a 404 response code is given when requesting the profile. If it is, it is presumed to be available""" try: return username, requests.get(TWITCH_URL.format(username=username)).status_code == 404 except Exception as e: print(e)
usernames = read_valid_usernames(input("Enter path to list of usernames: "))
for username, available in ThreadPool(MAX_THREADS).imap_unordered(username_available, usernames): print("{:<{size}}{}".format(username, MESSAGES.get(available, "Unknown"), size=len(max(usernames, key=len)) + 1))
works more than fast enough for me, and I don't see how this is considered simplifying my code when what you posted looks 10x more complicated for something so trivial.
(Jan 11, 2017 12:21 PM)Threebones Wrote: works more than fast enough for me, and I don't see how this is considered simplifying my code when what you posted looks 10x more complicated for something so trivial.
There's really nothing that complex in there. And mine runs significantly faster than yours even can. It can request about ~25 at once... it runs roughly around 20x faster than yours. It can be increased with more threads.
Jan 14, 2017 04:13 AM
(This post was last modified: Jan 14, 2017 04:34 AM by Undefined.)
(Jan 11, 2017 12:21 PM)Threebones Wrote: works more than fast enough for me, and I don't see how this is considered simplifying my code when what you posted looks 10x more complicated for something so trivial.
The one Goodies posted is actually very readable and not complex at all.
@Goodies, I've tested the checker with usernames that contain spaces in them ("this is an username" or "hello there") and it simply doesn't check them. I assume that is because Twitch disallows spaced usernames, right?
(Jan 14, 2017 04:13 AM)Undefined Wrote: @Goodies, I've tested the checker with usernames that contain spaces in them ("this is an username" or "hello there") and it simply doesn't check them. I assume that is because Twitch disallows spaced usernames, right?
I honestly have no idea. The only thing I'm checking is for a 404 status code. I don't even have a twitch account, and nor do I watch any streams. OP's code checks for a 404 page, so I changed it to looking at the status code. Other than that, it does more or less the same thing.
To be honest, though, I wouldn't even use the multithreaded version. I'd use aiohttp/asyncio. You'd limit the number of clients with a bounded semaphore and send the requests asynchronously. You'd get a fair amount of increased speed.
(Jan 14, 2017 01:54 PM)Goodies Wrote: I honestly have no idea. The only thing I'm checking is for a 404 status code. I don't even have a twitch account, and nor do I watch any streams. OP's code checks for a 404 page, so I changed it to looking at the status code. Other than that, it does more or less the same thing.
To be honest, though, I wouldn't even use the multithreaded version. I'd use aiohttp/asyncio. You'd limit the number of clients with a bounded semaphore and send the requests asynchronously. You'd get a fair amount of increased speed.
Yep, there's a problem with it actually. Since Twitch doesn't accept spaces anywhere in the username (even the end) pasted usernames might have issues. The script just doesn't even check them because Twitch declines them.
Check Discord - I've added something to the script. It now outputs the available usernames into a text file in the same location as the script/username list.
thanks bro I will take a look at this
Using .strip() should remove trailing whitespace. If its spaces in the middle of the string, perhaps do .replace(" ", "something else?") to cut out/replace those spaces.
You know, you could probably apply the regex to bruteforce "short" Twitch usernames (i.e. 4 chars, which is the minimum, right?)...
You guys are doing it wrong, you're not supposed to check for a username after the "http://twitch.tv/" because it returns false positives. They have their own URL for checking usernames if you take a look at the headers. I've been using my checker for a bit and not one has shown as available. The method of checking for status codes is correct though.
This is in C# but the same applies for every language.
|