Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed public list streaming #169

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions rainbowstream/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
t['event'] = 'retweet'
c['events'].append(t)
notify_retweet(t)
return
return True
except:
pass

Expand Down Expand Up @@ -250,9 +250,9 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
fil = list(set((fil or []) + c['ONLY_LIST']))
ig = list(set((ig or []) + c['IGNORE_LIST']))
if fil and screen_name not in fil:
return
return False
if ig and screen_name in ig:
return
return False

# Get rainbow id
if tid not in c['tweet_dict']:
Expand Down Expand Up @@ -375,6 +375,7 @@ def draw(t, keyword=None, humanize=True, noti=False, fil=[], ig=[]):
image_to_display(BytesIO(response.content))
except Exception:
printNicely(red('Sorry, image link is broken'))
return True


def print_threads(d):
Expand Down
30 changes: 19 additions & 11 deletions rainbowstream/rainbow.py
Original file line number Diff line number Diff line change
Expand Up @@ -2010,20 +2010,22 @@ def stream(domain, args, name='Rainbow Stream'):
# The Logo
art_dict = {
c['USER_DOMAIN']: name,
c['PUBLIC_DOMAIN']: args.track_keywords or 'Global',
c['PUBLIC_DOMAIN']: args.track_keywords or name or 'Global',
c['SITE_DOMAIN']: name,
}
if c['ASCII_ART']:
ascii_art(art_dict.get(domain, name))
# These arguments are optional:
stream_args = dict(
timeout=0.5, # To check g['stream_stop'] after each 0.5 s
timeout=0.25, # To check g['stream_stop'] after each 0.5 s
block=True,
heartbeat_timeout=c['HEARTBEAT_TIMEOUT'] * 60)
# Track keyword
query_args = dict()
if args.track_keywords:
query_args['track'] = args.track_keywords
if args.filter_ids:
query_args['follow'] = args.filter_ids
# Get stream
stream = TwitterStream(
auth=authen(),
Expand All @@ -2035,7 +2037,7 @@ def stream(domain, args, name='Rainbow Stream'):
elif domain == c['SITE_DOMAIN']:
tweet_iter = stream.site(**query_args)
else:
if args.track_keywords:
if args.track_keywords or args.filter_ids:
tweet_iter = stream.statuses.filter(**query_args)
else:
tweet_iter = stream.statuses.sample()
Expand Down Expand Up @@ -2071,22 +2073,22 @@ def stream(domain, args, name='Rainbow Stream'):
if g['pause']:
continue
while c['lock']:
time.sleep(0.5)
time.sleep(0.25)
# Draw the tweet
draw(
did_draw = draw(
t=tweet,
keyword=args.track_keywords,
humanize=False,
fil=args.filter,
ig=args.ignore,
)
# Current readline buffer
current_buffer = readline.get_line_buffer().strip()
current_buffer = readline.get_line_buffer()
# There is an unexpected behaviour in MacOSX readline + Python 2:
# after completely delete a word after typing it,
# somehow readline buffer still contains
# the 1st character of that word
if current_buffer and g['cmd'] != current_buffer:
if current_buffer and g['cmd'] != current_buffer and did_draw:
sys.stdout.write(
g['decorated_name'](g['PREFIX']) + current_buffer)
sys.stdout.flush()
Expand All @@ -2098,7 +2100,7 @@ def stream(domain, args, name='Rainbow Stream'):
if g['pause']:
continue
while c['lock']:
time.sleep(0.5)
time.sleep(0.25)
print_message(tweet['direct_message'])
elif tweet.get('event'):
c['events'].append(tweet)
Expand All @@ -2122,6 +2124,7 @@ def spawn_public_stream(args, keyword=None):
Spawn a new public stream
"""
# Only set keyword if specified
args.filter_ids = ''
if keyword:
if keyword[0] == '#':
keyword = keyword[1:]
Expand All @@ -2131,6 +2134,7 @@ def spawn_public_stream(args, keyword=None):
g['keyword'] = 'Global'
g['PREFIX'] = u2str(emojize(format_prefix(keyword=g['keyword'])))
g['listname'] = ''

# Start new thread
th = threading.Thread(
target=stream,
Expand Down Expand Up @@ -2162,7 +2166,7 @@ def spawn_list_stream(args, stuff=None):
printNicely(light_yellow('getting list members ...'))
# Get members
t = Twitter(auth=authen())
members = []
members, member_ids = [], []
next_cursor = -1
while next_cursor != 0:
m = t.lists.members(
Expand All @@ -2172,15 +2176,18 @@ def spawn_list_stream(args, stuff=None):
include_entities=False)
for u in m['users']:
members.append('@' + u['screen_name'])
member_ids.append(u['id'])
next_cursor = m['next_cursor']
printNicely(light_yellow('... done.'))
# Build thread filter array
args.filter = members
args.filter_ids = ','.join([str(x) for x in member_ids])

# Start new thread
th = threading.Thread(
target=stream,
args=(
c['USER_DOMAIN'],
c['PUBLIC_DOMAIN'],
args,
slug))
th.daemon = True
Expand All @@ -2197,6 +2204,7 @@ def spawn_personal_stream(args, stuff=None):
"""
Spawn a new personal stream
"""
args.filter_ids = ''
# Reset the tracked keyword and listname
g['keyword'] = g['listname'] = ''
# Reset prefix
Expand Down Expand Up @@ -2254,7 +2262,7 @@ def fly():
spawn_dict.get(target)(args, stuff)

# Start listen process
time.sleep(0.5)
time.sleep(0.25)
g['reset'] = True
g['prefix'] = True
listen()