servidor web

mail@pastecode.io avatar
unknown
python
2 years ago
7.6 kB
5
Indexable
# -*- coding: utf-8 -*-
import six
try:
    from libs.control import oneplay
except:
    from control import oneplay
try:
    from urllib.parse import urlparse, parse_qs, quote, unquote, quote_plus, unquote_plus, urlencode #python 3
except ImportError:    
    from urlparse import urlparse, parse_qs #python 2
    from urllib import quote, unquote, quote_plus, unquote_plus, urlencode
import sys
import os
import re
try:
    import json
except:
    import simplejson as json
import time
import base64
if six.PY3:
    from http.server import HTTPServer
    from http.server import BaseHTTPRequestHandler
    from http.server import SimpleHTTPRequestHandler
else:
    from BaseHTTPServer import BaseHTTPRequestHandler
    from SimpleHTTPServer import SimpleHTTPRequestHandler
    from BaseHTTPServer import HTTPServer
import threading
try:
    from libs.config import PORT
except:
    from config import PORT

class handler(SimpleHTTPRequestHandler):
    def do_GET(self):
        query_components = parse_qs(urlparse(self.path).query)
        if 'url' in query_components:
            url = query_components["url"][0]
            #print('acessou url :', url)
            try:
                headers = url.split('|')[1]
            except:
                headers = False
            h = {}
            if headers:
                if 'useragent' in headers:
                    try:
                        useragent = headers.split('useragent=')[1]
                    except:
                        useragent = ''
                    try:
                        useragent = headers.split('&')[0]
                    except:
                        pass
                    h.update({"User-Agent": useragent})
                else:
                    h.update({"User-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Mobile Safari/537.36"})
                if 'origin' in headers:
                    try:
                        origin = headers.split('origin=')[1]
                    except:
                        origin = ''
                    try:
                        origin = origin.split('&')[0]
                    except:
                        pass
                    h.update({"Origin": origin})
                if 'referer' in headers:
                    try:
                        referer = headers.split('referer=')[1]
                    except:
                        referer = ''
                    try:
                        referer = referer.split('&')[0]
                    except:
                        pass
                    h.update({"Referer": referer})
                headers = h
            else:
                headers = False
            try:
                url = url.split("|")[0]
            except:
                pass
            try:
                if int(url.count('/')) == 5 and not '.m3u8' in url and not '/hls/' in url and not '/hlsr/' in url:
                    #print('pegou link')
                    itens_proibidos = ['.mp4', '.mp3', '.avi', '.mpd', 'aac', '.mkv', '.wmv', '.wma', 'playlist', 'chunk', 'index', 'media', 'master']
                    block = []
                    for i in itens_proibidos:
                        if i in url:
                            block.append('block')
                    if not block:                      
                        url = oneplay().ts_to_m3u8(url)
            except:
                pass
            stream = ''
            if 'streamtape' in url:
                stream,sub = oneplay().resolve_streamtape(url)
                try:
                    stream = stream.split('|')[0]
                except:
                    pass
            elif 'canal_' in url:
                try:
                    channel = url.split('canal_')[1]
                except:
                    channel = ''
                if channel:
                    stream = oneplay().embedflixtv(channel_id=channel)
            elif '.ts' in url:
                self.send_response(200)
                self.send_header('Content-type','video/mp2t')
                self.end_headers()
                try:
                    self.copyfile(oneplay().streaming(url=url,headers=headers), self.wfile)
                except:
                    pass                    
            elif '.m3u8' in url and not '.webp' in url:
                self.send_response(200)
                self.end_headers()
                file = oneplay().open_m3u8(url=url,headers=headers)
                try:
                    self.copyfile(file,self.wfile)
                except:
                    pass
                try:
                    file.close()
                except:
                    pass
            elif '.html' in url:
                self.send_response(200)
                self.send_header('Content-type','video/mp2t')
                self.end_headers()
                try:
                    self.copyfile(oneplay().streaming(url=url,headers=headers), self.wfile)
                except:
                    pass
            elif '.mp4' in url:
                self.send_response(200)
                self.send_header('Content-type','video/mp4')
                self.end_headers()
                try:
                    self.copyfile(oneplay().streaming(url=stream,headers=headers), self.wfile)
                except:
                    pass
            elif '.webp' in url:             
                self.send_response(200)
                self.send_header('Content-type','video/mp2t')
                self.end_headers()
                try:
                    self.copyfile(oneplay().streaming(url=url,headers=headers), self.wfile)
                except:
                    pass
            elif stream:
                self.send_response(200)        
                self.send_header('Content-type','video/mp4')
                self.end_headers()
                # self.send_response(301)
                # self.send_header('Location',stream)
                # self.end_headers()
                #self.wfile.write(oneplay().streaming(stream))
                try:
                    self.copyfile(oneplay().streaming(url=stream,headers=headers), self.wfile)
                except:
                    pass
            else:
                self.send_response(200)
                self.end_headers()
                try:
                    self.copyfile(oneplay().streaming(url=url,headers=headers), self.wfile)
                except:
                    pass
        elif not 'url' in query_components:            
            self.send_response(200)
            self.send_header('Content-type','text/plain')
            self.end_headers()
            message = 'ONEPLAY DLNA SERVICE'
            if six.PY3:
                self.wfile.write(bytes(message, "utf8"))
            else:
                self.wfile.write(message)

httpd = HTTPServer(('', PORT), handler)
def serve_forever(httpd):
    with httpd:  # to make sure httpd.server_close is called
        httpd.serve_forever()#

class mediaserver:
    def thread(self):
        t = threading.Thread(target=serve_forever, args=(httpd, ))
        return t
    def start(self):
        self.thread().start()
    def stop(self):
        httpd.shutdown()



# mediaserver().start()
# time.sleep(10)
# mediaserver().stop()
# print('finalizado')