Skip to content

Commit

Permalink
Allow record by time of the day
Browse files Browse the repository at this point in the history
  • Loading branch information
avdata99 committed Mar 8, 2021
1 parent f68852e commit 3a8a53c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,4 @@ dmypy.json
run.py

yaasr/settings/google-cloud-storage-credential.json
tests/test_upload.py
2 changes: 1 addition & 1 deletion yaasr/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os


__VERSION__ = '0.4.7'
__VERSION__ = '0.4.8'
BASE_FOLDER = os.path.dirname(__file__)
STREAMS_FOLDER = os.path.join(BASE_FOLDER, "streams")

Expand Down
30 changes: 29 additions & 1 deletion yaasr/recorder/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import os
import pytz
import requests
from time import sleep
from datetime import datetime, timedelta
from yaasr import STREAMS_FOLDER
from yaasr.exceptions import StreamFolderNotFoud, StreamDataFileNotFoud
Expand All @@ -24,6 +25,14 @@ def __init__(self, stream_name, streams_folder=STREAMS_FOLDER, destination_folde
self.destination_folder = destination_folder
self.timezone = pytz.timezone('UTC')

# ranges to record (to avoid record 24h)
self.record_from_time = None
self.record_to_time = None

def set_record_times(self, from_time=None, to_time=None):
self.record_from_time = from_time
self.record_to_time = to_time

def get_stream_folder(self):
""" Get the stream folder """
stream_folder = os.path.join(self.streams_folder, self.name)
Expand Down Expand Up @@ -61,6 +70,20 @@ def record(self, total_seconds=0, chunk_bytes_size=1024, chunk_time_size=60):
chunk_bytes_size: chunk size to iterate over stream downloaded data
chunk_time_size: split the audio files is chunk with this time
"""
if self.record_to_time is not None:
time_now = datetime.now(self.timezone).time()
while time_now >= self.record_to_time:
logger.info(f'Now {time_now} is late to save')
sleep(90)
time_now = datetime.now(self.timezone).time()

if self.record_from_time is not None:
time_now = datetime.now(self.timezone).time()
while time_now < self.record_from_time:
logger.info(f'Now {time_now} is early to save')
sleep(90)
time_now = datetime.now(self.timezone).time()

c = 0
for stream in self.streams:

Expand Down Expand Up @@ -88,7 +111,7 @@ def record(self, total_seconds=0, chunk_bytes_size=1024, chunk_time_size=60):
now = datetime.now(self.timezone)
elapsed = now - start
if total_seconds > 0 and elapsed >= timedelta(seconds=total_seconds):
logger.info(f'Finish recording {now}')
logger.info(f'Finish total_seconds recording {now}')
break
elif now - last_start >= timedelta(seconds=chunk_time_size):
logger.info(f'{now} Elapsed {elapsed} Finish chunk {c}')
Expand All @@ -97,6 +120,11 @@ def record(self, total_seconds=0, chunk_bytes_size=1024, chunk_time_size=60):
self.last_start = last_start
f.close()
f = open(stream_path, 'wb')
elif self.record_to_time is not None:
time_now = datetime.now(self.timezone).time()
if time_now >= self.record_to_time:
logger.info(f'Finished day time {self.record_to_time} at {time_now}')
break

f.close()
# last chunk
Expand Down
22 changes: 20 additions & 2 deletions yaasr/terminal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Iterate over priority domains and send data to server
"""
import argparse
from datetime import time
import json
import logging.config
import os
Expand Down Expand Up @@ -46,7 +47,9 @@ def compress_and_google_store(stream,
total_seconds=300,
chunk_bytes_size=256,
chunk_time_size=60,
audio_format='ogg'):
audio_format='ogg',
from_time=None,
to_time=None):
""" Shows stream info """

ys = YStream(stream)
Expand All @@ -68,6 +71,14 @@ def compress_and_google_store(stream,
}
}
]

if from_time is not None:
parts = from_time.split(':')
ys.record_from_time = time(int(parts[0]), int(parts[1]))
if to_time is not None:
parts = to_time.split(':')
ys.record_to_time = time(int(parts[0]), int(parts[1]))

ys.record(total_seconds=total_seconds, chunk_bytes_size=chunk_bytes_size, chunk_time_size=chunk_time_size)


Expand All @@ -82,6 +93,8 @@ def main():
parser.add_argument('--total_seconds', nargs='?', default=0, type=int)
parser.add_argument('--chunk_bytes_size', nargs='?', default=256, type=int)
parser.add_argument('--chunk_time_size', nargs='?', default=1200, type=int)
parser.add_argument('--from_time', nargs='?', default=None, type=str, help="like 14:32")
parser.add_argument('--to_time', nargs='?', default=None, type=str, help="like 14:32")
# compress parameters
parser.add_argument('--audio_format', nargs='?', default='mp3', choices=['mp3', 'ogg'], type=str)
parser.add_argument('--bucket_name', nargs='?', default=None, type=str)
Expand Down Expand Up @@ -109,13 +122,18 @@ def main():
chunk_time_size=args.chunk_time_size
)
elif args.command == 'compress-and-google-store':
if args.bucket_name is None:
raise Exception('bucket name is required')

return compress_and_google_store(
stream=args.stream,
bucket_name=args.bucket_name,
total_seconds=args.total_seconds,
chunk_bytes_size=args.chunk_bytes_size,
chunk_time_size=args.chunk_time_size,
audio_format=args.audio_format
audio_format=args.audio_format,
from_time=args.from_time,
to_time=args.to_time
)
elif args.command == 'supervisor':
return setup_supervisor(
Expand Down

0 comments on commit 3a8a53c

Please sign in to comment.