I am new to Python and trying to use a twitter name checker coded by matrixik.
It is working quite well, but after 5-10 iterations checking about 25 names at once, an exception happens:
Code: Alles auswählen
Exception in thread Thread-18:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 8
self.run()
File "C:\Python27\lib\site-packages\workerp
job.run()
File "check_twitter_names.py", line 44, in
name_info = json.loads(name_json.text)
File "C:\Python27\lib\json\__init__.py", li
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", lin
obj, end = self.raw_decode(s, idx=_w(s, 0
File "C:\Python27\lib\json\decoder.py", lin
raise ValueError("No JSON object could be
ValueError: No JSON object could be decoded
Code: Alles auswählen
"""
Twitter usernames checker
"""
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function, unicode_literals
from __future__ import absolute_import
import json
import requests
import sys
import workerpool
from threading import Lock
#-----------------------------------------------------------------------------#
TWITTER_NAMES_FILE = 'twitter_usernames.txt'
TWITTER_AVAILABLE_NAMES_FILE = 'names_available.txt'
TWITTER_TAKEN_NAMES_FILE = 'names_taken.txt'
NR_OF_THREADS = 20
#-----------------------------------------------------------------------------#
HEADERS = {'User-Agent': 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; '
'Trident/6.0)'}
MUTEX = Lock()
class CheckName(workerpool.Job):
"""Function will check if name is available"""
def __init__(self, name):
self.name = name
def run(self):
name_check_url = 'https://twitter.com/users/username_available'
name_json = requests.get(name_check_url,
params={'username': self.name},
headers=HEADERS)
name_info = json.loads(name_json.text)
if name_info['valid']:
MUTEX.acquire()
# print('Name available: {}'.format(self.name,))
with open(TWITTER_AVAILABLE_NAMES_FILE, 'a') as my_file:
my_file.writelines('{}\n'.format(self.name,))
MUTEX.release()
else:
MUTEX.acquire()
# print('Name taken: {}'.format(self.name,))
with open(TWITTER_TAKEN_NAMES_FILE, 'a') as my_file:
my_file.writelines('{}\n'.format(self.name,))
MUTEX.release()
def main():
"""\
Main program
"""
pool = workerpool.WorkerPool(size=NR_OF_THREADS)
for name in open(TWITTER_NAMES_FILE):
job = CheckName(name.strip())
pool.put(job)
pool.shutdown()
pool.wait()
return 0 # OK
if __name__ == '__main__':
#Start Program
STATUS = main()
sys.exit(STATUS)
It seems the problem is happening with several threads only. Any suggestions? Maybe the problem is not releated to the code itself, but Twitter blocking the mass requests?
I am very thankful for any help.
Best regards,
peterdot