Created
November 18, 2013 16:46
-
-
Save mion/7531100 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import time | |
import sh | |
import redis | |
from selenium import webdriver | |
fake_queue = [ | |
{ | |
'course': 'Processo do Trabalho - Marcelo Moura', | |
'class': 'Aula 01', | |
'link': 'http://www.cursoforumtv.com/Course.aspx?idsHierarchy=2769_3790_3789&md5hash=d98353149b925e35fd28dac817176b92', | |
'seq': '4' | |
}, | |
{ | |
'course': 'Processo do Trabalho - Marcelo Moura', | |
'class': 'Aula 01', | |
'link': 'http://www.cursoforumtv.com/Course.aspx?idsHierarchy=2769_3790_3789&md5hash=d98353149b925e35fd28dac817176b92', | |
'seq': '3' | |
}] | |
#rds = redis.Redis(host='localhost', port=1234) | |
def get_job(): | |
# return rds.lpop('jobs') | |
return fake_queue.pop(0) | |
class Worker(object): | |
def __init__(self): | |
self.browser = webdriver.Firefox() | |
self.browser.get('http://www.cursoforumtv.com/') | |
def js(self, script): | |
self.browser.execute_script(open(script + '.js').read()) | |
def process(self, job): | |
print ' -> Processing: ', '|'.join([job['course'], job['class'], job['seq']]) | |
self.js('login') | |
print ' -> Setting up iptables' | |
sh.sudo.iptables('-t', 'nat', | |
'-A', 'OUTPUT', | |
'-p', 'tcp', | |
'--dport', '1935', | |
'-j', 'REDIRECT') | |
print ' -> Starting rtmpsuck' | |
p = sh.rtmpsuck(_out=handle_rtmpsuck_output) | |
print ' -> Streaming video...' | |
self.browser.get(job['link'] + '&Seq=' + job['seq']) | |
p.wait() | |
sh.exit() | |
print ' -> Done! Restoring iptables' | |
sh.sudo.iptables('-t', 'nat', | |
'-D', 'OUTPUT', | |
'-p', 'tcp', | |
'--dport', '1935', | |
'-j', 'REDIRECT') | |
def start(self): | |
try: | |
while True: | |
job = job_queue.pop() | |
self.process(job) | |
time.sleep(5) | |
except Exception, e: | |
print '[!] SHITFUCK something went wrong:', e | |
if len(job_queue) == 0: | |
print ' -> Job queue is empty' | |
return | |
else: | |
print ' -> Restarting' | |
self.restart() | |
def restart(self): | |
print ' -> Logging out' | |
self.js('logout') | |
print '[!] Trying again in 2min...' | |
time.sleep(120) | |
self.start() | |
def handle_rtmpsuck_output(line, stdin, process): | |
print '\trtmpsuck>> ', line | |
if line.endswith('done!'): # Connection... done! | |
print '\trtmpsuck is done recording' | |
process.terminate() | |
return True | |
def main(): | |
worker = Worker() | |
worker.start() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment