File: managepytone.py

package info (click to toggle)
autoradio 3.1-6
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,784 kB
  • sloc: python: 10,213; sh: 68; makefile: 16
file content (131 lines) | stat: -rw-r--r-- 3,475 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from __future__ import print_function
from builtins import object
import os,sys
import events, network, requests, version, helper
from datetime import *
from threading import *



def ar_emitted(self):
    self.emission_done=datetime.now()
    self.save()


class ScheduleProgram(object):

    def __init__ (self,function,operation,media,scheduledatetime,programma):
        "init schedule"
        
        self.function=function
        self.operation=operation
        self.media=media
        self.scheduledatetime=scheduledatetime
        self.programma=programma

        scheduledatetime
        print("differenza ",datetime.now(),self.scheduledatetime)
        delta=( self.scheduledatetime - datetime.now())
        print(delta)

        #self.deltasec=max(secondi(delta),1)
        self.deltasec=secondi(delta)
        self.deltasec
        self.timer = Timer(self.deltasec, self.function,
                      [self.operation,self.media,self.programma])

    def start (self):
        "start of programmed schedule"
        
        self.timer.start()

def ManagePytone (operation,media,programma):
    "Manage pytone to do operation on media"


    unixsocketfile =  os.path.expanduser("~/.pytone/pytonectl")
    networklocation = unixsocketfile
    print(networklocation)

    try:
        channel = network.clientchannel(networklocation)

    except Exception as e:

        print("Error: cannot connect to PyTone server: %s" % e)
        sys.exit(2)
        
    channel.start()

    root,ext=os.path.splitext(media)

    if (ext == ".m3u"):
        medias=[]
        f = open(media, "r")
        for line in f.readlines():
            medias.append(line[:-1])
        f.close()

    else:
        medias=(media,)

    for mediasplit in medias:
        print("invio ->",mediasplit)
        if operation == "queueMedia":
            song = channel.request(\
                requests.autoregisterer_queryregistersong("main", mediasplit))
            channel.notify(events.playlistaddsongtop(song))

    channel.quit()


    print("scrivo in django")
    print(programma)
    ar_emitted(programma)
    print("scritto in django")


def secondi(delta):
    secondi=delta.seconds
    # correggo i viaggi che si fa seconds
    if delta.days < 0 :
        secondi = secondi + (3600*24*delta.days)
    return secondi

class dummy_programma(object):

    def __init__(self):
        pass
    
    def save(self):
        print("faccio finta di salvarlo")

def main():

    programma=dummy_programma()

    function=ManagePytone
    operation="queueMedia"
#   media = "/home/pat1/django/autoradio/media/spots/spot-bibbiano.ogg"
    media="/home/pat1/django/autoradio/media/playlist/pomeridiana_ore_14_00.m3u"
    #media = raw_input("dammi il media? ")
    scheduledatetime=datetime.now()+timedelta(seconds=15)
    schedule=ScheduleProgram(function,operation,media,scheduledatetime,programma)
    schedule.start()

    scheduledatetime=datetime.now()+timedelta(seconds=100)
    media = "/home/pat1/django/autoradio/media/spots/spot-nocino.ogg"

    schedule=ScheduleProgram(function,operation,media,scheduledatetime,programma)
    schedule.start()

    scheduledatetime=datetime.now()+timedelta(seconds=200)
    media = "/home/pat1/django/autoradio/media/spots/spot-panedellamoni.ogg"

    schedule=ScheduleProgram(function,operation,media,scheduledatetime,programma)
    schedule.start()

    
if __name__ == '__main__':
    main()  # (this code was run as script)