Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.
Open
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
57 changes: 42 additions & 15 deletions acehttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
from socket import error as SocketException
from socket import SHUT_RDWR
import urllib2
import base64
import json
import hashlib
import aceclient
import aceconfig
Expand Down Expand Up @@ -203,6 +205,35 @@ def do_GET(self, headers_only=False):
return
self.handleRequest(headers_only)

def get_cid(self):
try:
acelive_url=''+self.path_unquoted
if self.reqtype == 'torrent':
if acelive_url[:4].lower()=='http' :
if acelive_url[-8:].lower()=='.acelive' :
logger.debug('Trying to download acelive')
logger.info(acelive_url)
req = urllib2.Request(acelive_url, headers={'User-Agent' : "Magic Browser"})
acelive_file = urllib2.urlopen(
req, timeout=10).read()
acelive_file_base64 = base64.b64encode(acelive_file)

req = urllib2.Request('http://api.torrentstream.net/upload/raw', acelive_file_base64)

req.add_header('Content-Length', '%d' % len(acelive_file_base64))

req.add_header('Content-Type', 'application/octet-stream')

res = urllib2.urlopen(req, timeout=10).read()
CID=''+json.loads(res)['content_id']
if CID!='' :
logger.debug('CID: '+CID)
return CID.encode('UTF-8')
except:
logger.error("Can't download acelive!")

return self.path_unquoted

def handleRequest(self, headers_only):
# Check if third parameter exists
# …/pid/blablablablabla/video.mpg
Expand Down Expand Up @@ -237,6 +268,7 @@ def handleRequest(self, headers_only):
return

self.path_unquoted = urllib2.unquote(self.splittedpath[2])
ContentID=self.get_cid()
# Make list with parameters
self.params = list()
for i in xrange(3, 8):
Expand All @@ -246,25 +278,20 @@ def handleRequest(self, headers_only):
self.params.append('0')

# Adding client to clientcounter
clients = AceStuff.clientcounter.add(self.path_unquoted, self.clientip)
clients = AceStuff.clientcounter.add(ContentID, self.clientip)
# If we are the one client, but sucessfully got ace from clientcounter,
# then somebody is waiting in the videodestroydelay state
self.ace = AceStuff.clientcounter.getAce(self.path_unquoted)
self.ace = AceStuff.clientcounter.getAce(ContentID)
if not self.ace:
shouldcreateace = True
else:
shouldcreateace = False

# Use PID as VLC ID if PID requested
# Or torrent url MD5 hash if torrent requested
if self.reqtype == 'pid':
self.vlcid = self.path_unquoted
else:
self.vlcid = hashlib.md5(self.path_unquoted).hexdigest()
self.vlcid = ContentID

# If we don't use VLC and we're not the first client
if clients != 1 and not AceConfig.vlcuse:
AceStuff.clientcounter.delete(self.path_unquoted, self.clientip)
AceStuff.clientcounter.delete(ContentID, self.clientip)
logger.error(
"Not the first client, cannot continue in non-VLC mode")
self.dieWithError(503) # 503 Service Unavailable
Expand All @@ -277,12 +304,12 @@ def handleRequest(self, headers_only):
AceConfig.acehost, AceConfig.aceport, connect_timeout=AceConfig.aceconntimeout,
result_timeout=AceConfig.aceresulttimeout)
# Adding AceClient instance to pool
AceStuff.clientcounter.addAce(self.path_unquoted, self.ace)
AceStuff.clientcounter.addAce(ContentID, self.ace)
logger.debug("AceClient created")
except aceclient.AceException as e:
logger.error("AceClient create exception: " + repr(e))
AceStuff.clientcounter.delete(
self.path_unquoted, self.clientip)
ContentID, self.clientip)
self.dieWithError(502) # 502 Bad Gateway
return

Expand Down Expand Up @@ -403,21 +430,21 @@ def handleRequest(self, headers_only):
self.dieWithError()
finally:
logger.debug("END REQUEST")
AceStuff.clientcounter.delete(self.path_unquoted, self.clientip)
if not self.errorhappened and not AceStuff.clientcounter.get(self.path_unquoted):
AceStuff.clientcounter.delete(ContentID, self.clientip)
if not self.errorhappened and not AceStuff.clientcounter.get(ContentID):
# If no error happened and we are the only client
logger.debug("Sleeping for " + str(
AceConfig.videodestroydelay) + " seconds")
gevent.sleep(AceConfig.videodestroydelay)
if not AceStuff.clientcounter.get(self.path_unquoted):
if not AceStuff.clientcounter.get(ContentID):
logger.debug("That was the last client, destroying AceClient")
if AceConfig.vlcuse:
try:
AceStuff.vlcclient.stopBroadcast(self.vlcid)
except:
pass
self.ace.destroy()
AceStuff.clientcounter.deleteAce(self.path_unquoted)
AceStuff.clientcounter.deleteAce(ContentID)


class HTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
Expand Down