Skip to content

Commit

Permalink
fixed issue2
Browse files Browse the repository at this point in the history
  • Loading branch information
oliver-lloyd committed Jun 13, 2019
1 parent 32493a8 commit 77b8b83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
Binary file added dist/twitch_listener-1.0.2.tar.gz
Binary file not shown.
38 changes: 22 additions & 16 deletions twitch_listener/Listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from socket import socket
from time import time, sleep
from twitch_listener import utils
import select

class connect_twitch(socket):

Expand Down Expand Up @@ -64,23 +65,27 @@ def listen(self, channels, duration = 0, debug = False):
# Collect data while duration not exceeded and channels are live
while (time() - startTime) < duration:
now = time() # Track loop time for adaptive rate limiting
ready_socks,_,_ = select.select(self._sockets.values(), [], [], 1)

for channel in self.joined:
response = self._sockets[channel].recv(16384)
if b"PING :tmi.twitch.tv\r\n" in response:
self._sockets[channel].send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
if debug:
print("\n\n!!Look, a ping: \n")
print(response)
print("\n\n")
else:
self._loggers[channel].info(response)
if debug:
print(response)
elapsed = time() - now
if elapsed < 60/800:
sleep( (60/800) - elapsed) # Rate limit

sock = self._sockets[channel]
if sock in ready_socks:
response = sock.recv(16384)
if b"PING :tmi.twitch.tv\r\n" in response:
sock.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
if debug:
print("\n\n!!Look, a ping: \n")
print(response)
print("\n\n")
else:
self._loggers[channel].info(response)
if debug:
print(response)
elapsed = time() - now
if elapsed < 60/800:
sleep( (60/800) - elapsed) # Rate limit
else: # if not in ready socks
pass
if debug:
print("Collected for " + str(time()-startTime) + " seconds")

Expand Down Expand Up @@ -185,5 +190,6 @@ def parse_logs(self, timestamp = True, channels = []):
data.append(row)

# Write data to file
pd.DataFrame(data).to_csv(channel + ".csv", index = False)
if len(data) > 0:
pd.DataFrame(data).to_csv(channel + ".csv", index = False)

0 comments on commit 77b8b83

Please sign in to comment.