tarinaretake

git clone https://git.tarina.org/tarinaretake
Log | Files | Refs | README | LICENSE

tarinaserver.py (7900B)


      1 #!/usr/bin/env python3
      2 
      3 import web
      4 import os
      5 import socket
      6 import ifaddr
      7 import sys
      8 import time
      9 import random
     10 import hashlib
     11 
     12 # Get path of the current dir, then use it as working directory:
     13 rundir = os.path.dirname(__file__)
     14 if rundir != '':
     15     os.chdir(rundir)
     16 
     17 filmfolder = '/home/pi/Videos/'
     18 
     19 basedir = os.path.dirname(os.path.realpath(__file__))
     20 sys.path.append(basedir)
     21 
     22 # Link video directory to static dir
     23 if os.path.isfile('static/Videos') == False:
     24     os.system("ln -s -t static/ " + filmfolder)
     25 
     26 films = []
     27 
     28 #NETWORKS
     29 
     30 networks=[]
     31 adapters = ifaddr.get_adapters()
     32 for adapter in adapters:
     33     print("IPs of network adapter " + adapter.nice_name)
     34     for ip in adapter.ips:
     35         if '::' not in ip.ip[0] and '127.0.0.1' != ip.ip:
     36             print(ip.ip)
     37             networks.append(ip.ip)
     38 network=networks[0]
     39 
     40 urls = (
     41     '/?', 'index',
     42     '/f/(.*)?', 'films'
     43 )
     44 
     45 app = web.application(urls, globals())
     46 render = web.template.render('templates/', base="base")
     47 web.config.debug=False
     48 os.system('rm '+basedir+'/sessions/*')
     49 store = web.session.DiskStore(basedir + '/sessions/')
     50 session = web.session.Session(app,store,initializer={'login': 0, 'user': '', 'backurl': '', 'bildsida': 0, 'cameras': [], 'reload': 0, 'randhash':''})
     51 
     52 port=55555
     53 ip='0.0.0.0'
     54 cameras=[]
     55 recording = False
     56 
     57 session.randhash = hashlib.md5(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
     58 
     59 ##---------------Connection----------------------------------------------
     60 
     61 def pingtocamera(host, port, data):
     62     print("Sending to "+host+" on port "+str(port)+" DATA:"+data)
     63     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     64     s.settimeout(0.01)
     65     try:
     66         while True:
     67             s.connect((host, port))
     68             s.send(str.encode(data))
     69             if host not in cameras and host not in networks:
     70                 session.cameras.append(host)
     71                 print("Found camera! "+host)
     72             print("Sent to server..")
     73             break
     74     except:
     75         ('did not connect')
     76     s.close()
     77 
     78 def sendtocamera(host, port, data):
     79     print("Sending to "+host+" on port "+str(port)+" DATA:"+data)
     80     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     81     s.settimeout(0.1)
     82     try:
     83         while True:
     84             s.connect((host, port))
     85             s.send(str.encode(data))
     86             print("Sent to server..")
     87             break
     88     except:
     89         ('did not connect')
     90     s.close()
     91 
     92 
     93 def getfilms(filmfolder):
     94     #get a list of films, in order of settings.p file last modified
     95     films_sorted = []
     96     films = next(os.walk(filmfolder))[1]
     97     for i in films:
     98         if os.path.isfile(filmfolder + i + '/settings.p') == True:
     99             lastupdate = os.path.getmtime(filmfolder + i + '/' + 'settings.p')
    100             films_sorted.append((i,lastupdate))
    101         else:
    102             films_sorted.append((i,0))
    103     films_sorted = sorted(films_sorted, key=lambda tup: tup[1], reverse=True)
    104     print(films_sorted)
    105     return films_sorted
    106 
    107 #------------Count scenes--------
    108 
    109 def countscenes(filmfolder, filmname):
    110     scenes = 0
    111     try:
    112         allfiles = os.listdir(filmfolder + filmname)
    113     except:
    114         allfiles = []
    115         scenes = 0
    116     for a in allfiles:
    117         if 'scene' in a:
    118             scenes = scenes + 1
    119     return scenes
    120 
    121 #------------Count shots--------
    122 
    123 def countshots(filmname, filmfolder, scene):
    124     shots = 0
    125     try:
    126         allfiles = os.listdir(filmfolder + filmname + '/scene' + str(scene).zfill(3))
    127     except:
    128         allfiles = []
    129         shots = 0
    130     for a in allfiles:
    131         if 'shot' in a:
    132             shots = shots + 1
    133     return shots
    134 
    135 #------------Count takes--------
    136 
    137 def counttakes(filmname, filmfolder, scene, shot):
    138     takes = 0
    139     try:
    140         allfiles = os.listdir(filmfolder + filmname + '/scene' + str(scene).zfill(3) + '/shot' + str(shot).zfill(3))
    141     except:
    142         allfiles = []
    143         return takes
    144     for a in allfiles:
    145         if '.mp4' in a or '.h264' in a:
    146             takes = takes + 1
    147     return takes
    148 
    149 class index:
    150     def GET(self):
    151         global recording
    152         films = getfilms(filmfolder)
    153         renderedfilms = []
    154         unrenderedfilms = []
    155         for f in films:
    156             if os.path.isfile('static/Videos/' + f[0] + '/' + f[0] + '.mp4') == True:
    157                 renderedfilms.append(f[0])
    158             else:
    159                 unrenderedfilms.append(f[0])
    160         i=web.input(func=None,selected=None)
    161         if i.selected != None:
    162             sendtocamera(ip,port,'SELECTED:'+i.selected)
    163         if i.func == 'search':
    164             session.cameras=[]
    165             # ping ip every 10 sec while not recording to connect cameras
    166             pingip=0
    167             while pingip < 255 :
    168                 pingip+=1
    169                 pingtocamera(network[:-3]+str(pingip),port,'PING')
    170         elif i.func == 'record':
    171             sendtocamera(ip,port,'REC')
    172         elif i.func == 'retake':
    173             print('retake')
    174         elif i.func == 'up':
    175             sendtocamera(ip,port,'UP')
    176         elif i.func == 'down':
    177             sendtocamera(ip,port,'DOWN')
    178         elif i.func == 'left':
    179             sendtocamera(ip,port,'LEFT')
    180         elif i.func == 'right':
    181             sendtocamera(ip,port,'RIGHT')
    182         elif i.func == 'view':
    183             sendtocamera(ip,port,'VIEW')
    184         elif i.func == 'middle':
    185             sendtocamera(ip,port,'MIDDLE')
    186         elif i.func == 'delete':
    187             sendtocamera(ip,port,'DELETE')
    188         elif i.func == 'picture':
    189             sendtocamera(ip,port,'PICTURE')
    190             session.randhash = hashlib.md5(str(random.getrandbits(256)).encode('utf-8')).hexdigest()
    191             session.reload = 1
    192         interface=open('/dev/shm/interface','r')
    193         vumeter=open('/dev/shm/vumeter','r')
    194         menu=interface.readlines()
    195         vumetermessage=vumeter.readlines()[0].rstrip('\n')
    196         try:
    197             selected=int(menu[0])
    198         except:
    199             selected=0
    200         try:
    201             name=menu[3].split(':')[1]
    202             name=name.rstrip('\n')
    203         except:
    204             name=''
    205         try:
    206             scene=menu[4].split(':')[1].split('/')[0]
    207         except:
    208             scene=1
    209         try:
    210             shot=menu[5].split(':')[1].split('/')[0]
    211         except:
    212             shot=1
    213         try:
    214             take=menu[6].split(':')[1].split('/')[0]
    215         except:
    216             take=1
    217             session.reload = 0
    218         if i.func == 'retake': 
    219             print(i.func+'fuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuiccccccccccccccccccccccccccccccccccccccccckkkkkkkkkkkkkkkkkkkkkkkkkk')
    220             if recording == False:
    221                 sendtocamera(ip,port,'RETAKE:'+shot)
    222                 recording = True
    223             else:
    224                 sendtocamera(ip,port,'STOPRETAKE')
    225                 recording = False
    226         if i.func != None:
    227             time.sleep(1)
    228             session.reload = 1
    229             raise web.seeother('/')
    230         thumb="/static/Videos/"+name+"/scene"+str(scene).zfill(3)+"/shot"+str(shot).zfill(3)+"/picture"+str(take).zfill(3)+".jpeg"
    231         print(thumb)
    232         if os.path.isfile(basedir+thumb) == False:
    233             print(basedir+thumb)
    234             thumb=''
    235         return render.index(renderedfilms, unrenderedfilms, session.cameras, menu, selected,name,scene,shot,take,str,session.randhash,thumb,vumetermessage,i.func)
    236 
    237 class films:
    238     def GET(self, film):
    239         shots = 0
    240         takes = 0
    241         i = web.input(page=None, scene=None, shot=None, take=None)
    242         if i.scene != None:
    243             shots = countshots(film, filmfolder, i.scene)
    244             takes = counttakes(film, filmfolder, i.scene, i.shot)
    245         if i.scene != None and i.shot != None:
    246             shots = countshots(film, filmfolder, i.scene)
    247         scenes = countscenes(filmfolder, film)
    248         return render.filmpage(film, scenes, str, filmfolder, counttakes, countshots, shots, i.scene, takes, i.shot, i.take)
    249 
    250 application = app.wsgifunc()
    251