import signal
import argparse
import time
+import threading
import requests
import webbrowser
# Database
db = RainbowDB()
+# Lock for streams
+
+StreamLock = threading.Lock()
+
# Commands
cmdset = [
'switch',
keyword = g['stuff'].split()[1]
if keyword[0] == '#':
keyword = keyword[1:]
- # Kill old process
- os.kill(g['stream_pid'], signal.SIGKILL)
+ # Kill old thread
+ g['stream_stop'] = True
args.track_keywords = keyword
- # Start new process
- p = Process(
- target=stream,
- args=(
- c['PUBLIC_DOMAIN'],
- args))
- p.start()
- g['stream_pid'] = p.pid
+ # Start new thread
+ th = threading.Thread(target=stream, args=(c['PUBLIC_DOMAIN'], args))
+ th.daemon = True
+ th.start()
# Personal stream
elif target == 'mine':
- # Kill old process
- os.kill(g['stream_pid'], signal.SIGKILL)
- # Start new process
- p = Process(
- target=stream,
- args=(
- c['USER_DOMAIN'],
- args,
- g['original_name']))
- p.start()
- g['stream_pid'] = p.pid
+ # Kill old thread
+ g['stream_stop'] = True
+ # Start new thread
+ th = threading.Thread(target=stream, args=(c['USER_DOMAIN'], args, g['original_name']))
+ th.daemon = True
+ th.start()
printNicely('')
if args.filter:
printNicely(cyan('Only: ' + str(args.filter)))
"""
Exit all
"""
- save_history()
- os.system('rm -rf rainbow.db')
- os.kill(g['stream_pid'], signal.SIGKILL)
- printNicely(green('See you next time :)'))
+ try:
+ save_history()
+ os.system('rm -rf rainbow.db')
+ printNicely(green('See you next time :)'))
+ except:
+ pass
sys.exit()
# Process the command
process(cmd)()
# Not re-display
- if cmd in ['switch', 't', 'rt', 'rep']:
+ if cmd in ['t', 'rt', 'rep']:
g['prefix'] = False
else:
g['prefix'] = True
# These arguments are optional:
stream_args = dict(
timeout=args.timeout,
- block=not args.no_block,
+ block=False,
heartbeat_timeout=args.heartbeat_timeout)
# Track keyword
query_args = dict()
tweet_iter = stream.statuses.filter(**query_args)
else:
tweet_iter = stream.statuses.sample()
+ # Block new stream until other one exits
+ StreamLock.acquire()
+ g['stream_stop'] = False
for tweet in tweet_iter:
+ if(g['stream_stop'] == True):
+ StreamLock.release()
+ break
if tweet is None:
- printNicely("-- None --")
+ pass
elif tweet is Timeout:
printNicely("-- Timeout --")
elif tweet is HeartbeatTimeout:
fil=args.filter,
ig=args.ignore,
)
+ sys.stdout.write(g['decorated_name'](c['PREFIX']) + readline.get_line_buffer())
+ sys.stdout.flush()
elif tweet.get('direct_message'):
- print_message(tweet['direct_message'],check_semaphore=True)
+ print_message(tweet['direct_message'], check_semaphore=True)
except TwitterHTTPError:
printNicely('')
printNicely(
except TwitterHTTPError:
printNicely('')
printNicely(
- magenta("Something wrong with Twitter Oauth right now :("))
- printNicely(
- magenta("Please delete ~/.rainbow_oauth and try again."))
+ magenta("We have maximum connection problem with twitter'stream API right now :("))
+ printNicely(magenta("Let's try again later."))
save_history()
os.system('rm -rf rainbow.db')
sys.exit()
- # Spawn stream process
- p = Process(
- target=stream,
- args=(
- c['USER_DOMAIN'],
- args,
- g['original_name']))
- p.start()
+ # Spawn stream thread
+ th = threading.Thread(target=stream, args=(c['USER_DOMAIN'], args, g['original_name']))
+ th.daemon = True
+ th.start()
# Start listen process
time.sleep(0.5)
g['reset'] = True
g['prefix'] = True
- g['stream_pid'] = p.pid
listen()