<commit_before>#!/usr/bin/python

from musicazoo.lib.mzbot import MZBot
import BaseHTTPServer
import os
import mimetypes
import httplib
import threading
import time

class PusherBot(MZBot,threading.Thread): # i am the pusher robot
	def __init__(self,mzq_url,file_to_host,host_name='',port_number=9100,vid_name=None):
		MZBot.__init__(self,mzq_url)
		threading.Thread.__init__(self)
		self.daemon=True

		self.port_number=port_number

		mimetypes.init()
		self.httpd = PusherBot.MultiThreadedHTTPServer((host_name,port_number), PusherBot.MyHandler)
		self.httpd.file_to_host = file_to_host
		if vid_name is None:
			self.vid_name = os.path.basename(file_to_host)
		else:
			self.vid_name=vid_name

	def push(self):
		self.my_ip=self.whoami()
		self.start()
		self.add()
		try:
			while True:
				time.sleep(3)
				if not self.still_playing():
					break
			time.sleep(3)
		except KeyboardInterrupt:
			self.remove()
		self.httpd.shutdown()
		self.join()

	def add(self):
		result=self.assert_success(self.doCommand({
			'cmd':'add',
			'args':{
				'type':'netvid',
				'args':{
					'url':'http://{0}:{1}'.format(self.my_ip,self.port_number),
					'short_description':self.vid_name
				}
			}
		}))
		self.added_uid=result['uid']

	def queue_plus_cur(self):
		results=self.assert_success(self.doCommands([
		{
			'cmd':'queue',
			'args':{'parameters':{}}
		},
		{
			'cmd':'cur',
			'args':{'parameters':{}}
		}
		]))
		if len(results)==1:
			return results[0]
		else:
			return results[0]+[results[1]]

	def still_playing(self):
		return self.added_uid in [mod['uid'] for mod in self.queue_plus_cur()]

	def remove(self):
		self.doCommands([
		{
			'cmd':'tell_module',
			'args':{
				'uid':self.added_uid,
				'cmd':'stop'
			}
		},
		{
			'cmd':'rm',
			'args':{
				'uids':[self.added_uid]
			}
		}
		])

	def run(self):
		self.httpd.serve_forever()
		self.httpd.server_close()

	#class MultiThreadedHTTPServer(ThreadingMixIn,BaseHTTPServer.HTTPServer): # No threading for testing
	class MultiThreadedHTTPServer(BaseHTTPServer.HTTPServer):
		pass

	class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
		def address_string(self):
			return str(self.client_address[0])

		def content_length(self,f):
			l=os.fstat(f.fileno()).st_size
			self.send_header("Content-length",str(l))

		def chunked_write(self,fr,to,chunksize=4096):
			while True:
				chunk=fr.read(chunksize)
				if not chunk:
					break
				to.write(chunk)

		def do_GET(self):
			infile=open(self.server.file_to_host)
			self.send_response(200)
			self.send_header("Content-type", mimetypes.guess_type(self.server.file_to_host)[0])
			self.content_length(infile)
			self.end_headers()
			self.chunked_write(infile,self.wfile)
			infile.close()
			return

		def do_HEAD(self):
			infile=open(self.server.file_to_host)
			self.send_response(200)
			self.send_header("Content-type", mimetypes.guess_type(self.server.file_to_host)[0])
			self.content_length(infile)
			self.end_headers()
			infile.close()

if __name__=='__main__':
	PusherBot('http://localhost:9000','chainsaw.avi').push()
<commit_msg>making push bot take command-line arguments<commit_after>#!/usr/bin/python

from musicazoo.lib.mzbot import MZBot
import BaseHTTPServer
import os
import mimetypes
import httplib
import threading
import time

class PusherBot(MZBot,threading.Thread): # i am the pusher robot
	def __init__(self,mzq_url,file_to_host,host_name='',port_number=9100,vid_name=None):
		MZBot.__init__(self,mzq_url)
		threading.Thread.__init__(self)
		self.daemon=True

		self.port_number=port_number

		mimetypes.init()
		self.httpd = PusherBot.MultiThreadedHTTPServer((host_name,port_number), PusherBot.MyHandler)
		self.httpd.file_to_host = file_to_host
		if vid_name is None:
			self.vid_name = os.path.basename(file_to_host)
		else:
			self.vid_name=vid_name

	def push(self):
		self.my_ip=self.whoami()
		self.start()
		self.add()
		try:
			while True:
				time.sleep(3)
				if not self.still_playing():
					break
			time.sleep(3)
		except KeyboardInterrupt:
			self.remove()
		self.httpd.shutdown()
		self.join()

	def add(self):
		result=self.assert_success(self.doCommand({
			'cmd':'add',
			'args':{
				'type':'netvid',
				'args':{
					'url':'http://{0}:{1}'.format(self.my_ip,self.port_number),
					'short_description':self.vid_name
				}
			}
		}))
		self.added_uid=result['uid']

	def queue_plus_cur(self):
		results=self.assert_success(self.doCommands([
		{
			'cmd':'queue',
			'args':{'parameters':{}}
		},
		{
			'cmd':'cur',
			'args':{'parameters':{}}
		}
		]))
		if len(results)==1:
			return results[0]
		else:
			return results[0]+[results[1]]

	def still_playing(self):
		return self.added_uid in [mod['uid'] for mod in self.queue_plus_cur()]

	def remove(self):
		self.doCommands([
		{
			'cmd':'tell_module',
			'args':{
				'uid':self.added_uid,
				'cmd':'stop'
			}
		},
		{
			'cmd':'rm',
			'args':{
				'uids':[self.added_uid]
			}
		}
		])

	def run(self):
		self.httpd.serve_forever()
		self.httpd.server_close()

	#class MultiThreadedHTTPServer(ThreadingMixIn,BaseHTTPServer.HTTPServer): # No threading for testing
	class MultiThreadedHTTPServer(BaseHTTPServer.HTTPServer):
		pass

	class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
		def address_string(self):
			return str(self.client_address[0])

		def content_length(self,f):
			l=os.fstat(f.fileno()).st_size
			self.send_header("Content-length",str(l))

		def chunked_write(self,fr,to,chunksize=4096):
			while True:
				chunk=fr.read(chunksize)
				if not chunk:
					break
				to.write(chunk)

		def do_GET(self):
			infile=open(self.server.file_to_host)
			self.send_response(200)
			self.send_header("Content-type", mimetypes.guess_type(self.server.file_to_host)[0])
			self.content_length(infile)
			self.end_headers()
			self.chunked_write(infile,self.wfile)
			infile.close()
			return

		def do_HEAD(self):
			infile=open(self.server.file_to_host)
			self.send_response(200)
			self.send_header("Content-type", mimetypes.guess_type(self.server.file_to_host)[0])
			self.content_length(infile)
			self.end_headers()
			infile.close()

if __name__=='__main__':
	PusherBot(sys.argv[1],sys.argv[2]).push()

{'fraction_non_alphanumeric': 0.14909858697417575, 'fraction_numerical': 0.009095338638947539, 'mean_word_length': 4.430335097001763, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6510633', 'n_tokens_mistral': 2745, 'n_tokens_neox': 2377, 'n_words': 331}