Skip to content

Instantly share code, notes, and snippets.

@Thann
Last active May 16, 2017 04:17
Show Gist options
  • Save Thann/ca7e168c6bd7d0863e322bc9d78a06b6 to your computer and use it in GitHub Desktop.
Save Thann/ca7e168c6bd7d0863e322bc9d78a06b6 to your computer and use it in GitHub Desktop.
Read the troll-chat from btc-e.com
#!/bin/env python
import re
import sys
import time
import requests
import lxml.html
from functools import reduce
from subprocess import Popen, PIPE
if '-l' in sys.argv:
pipe = Popen(['less', '+F', '-R'], stdin=PIPE)
write = lambda s: pipe.stdin.write(bytearray(s+'\n' ,'utf8'))
else:
pipe = False
write = print
newchats = None
while True:
try:
if pipe and pipe.poll() is not None:
sys.exit(0)
r = requests.get('http://btc-e.com')
doc = lxml.html.document_fromstring(r.text)
oldchats = newchats
newchats = [{
"id": e.attrib['id'][3:],
"text": e.cssselect('span')[0].text,
"username": e.cssselect('a')[0].text,
}
for e in doc.cssselect('#nChat p')
]
for chat in newchats:
if not oldchats or chat not in oldchats:
color = reduce(lambda x, y: x + y, [ord(c) for c in chat['username']]) % 240
write("\033[38;5;{}m{}\033[0m: {}".format(color, chat['username'], chat['text']))
if pipe and pipe.poll() is None:
pipe.stdin.flush()
for x in range(0, 50):
time.sleep(0.1)
if pipe and pipe.poll() is not None:
sys.exit(0)
except KeyboardInterrupt:
if not pipe:
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment