Exemplo n.º 1
0
def video_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = TutorialResource.objects.filter(status=1, id=1)
        serializer = VideoSerializer(snippets, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = VideoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Exemplo n.º 2
0
def get_tutorial_list(request, fossid, langid):
    """
    Retrieve, update or delete a code snippet.
    """

    try:
        tuts = TutorialResource.objects.filter(Q(status=1) | Q(status=2),
                                               language_id=langid,
                                               tutorial_detail_id__foss=fossid)
    except ObjectDoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = VideoSerializer(tuts, many=True)
        return JsonResponse(serializer.data, safe=False)
    elif request.method == 'PUT':

        data = JSONParser().parse(request)
        serializer = VideoSerializer(tut, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)
    elif request.method == 'DELETE':

        tut.delete()
        return HttpResponse(status=204)
Exemplo n.º 3
0
def video_list(request, pk):
    try:
        word = Word.objects.get(pk=pk)
    except Word.DoesNotExist:
        return JsonResponse({"error": f"Word with id {pk} not found"},
                            status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        videos = word.get_all_videos()
        serializer = VideoSerializer(videos, many=True)
        return Response(serializer.data)
Exemplo n.º 4
0
    def post(self, request, *args, **kwargs):
        def create_video_thumbnail(video_obj):
            video_path = os.path.join(MEDIA_ROOT, video_obj.video.name)
            with VideoFileClip(video_path, audio=False) as clip:
                max_duration = int(clip.duration) + 1
                print(max_duration)
                frame_at_second = 3
                thumbnail_frame = clip.get_frame(frame_at_second)
                video_thumbnail = Image.fromarray(thumbnail_frame)
                thumbnail_path = os.path.join(AZURE_STATIC_ROOT,
                                              f"{video}.jpg")
                video_thumbnail.save(thumbnail_path)

                # create an ImageFile compatable with Django's ORM/Postgres

                try:
                    thumbnail_buffer = open(thumbnail_path, "rb")
                except FileExistsError:
                    raise Exception(
                        "thumbnail file not captured from video properly")

                thumbnail = File(thumbnail_buffer)

                video.video_thumbnail.save(f"{video}.jpg", thumbnail)

        serializer = VideoSerializer(data=request.data)
        if serializer.is_valid():
            video = serializer.save(creator=request.user)
            serializer.save()
            create_video_thumbnail(video)
            return Response(data=serializer.data,
                            status=status.HTTP_201_CREATED)
        return Response(data=serializer.errors,
                        status=status.HTTP_400_BAD_REQUEST)
Exemplo n.º 5
0
def get_tutorial_list(request, fossid, langid):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        tuts = TutorialResource.objects.filter(language_id=langid, tutorial_detail_id__foss=fossid,status=1)
    except ObjectDoesNotExist:
        return HttpResponse(status=404)
    
    if request.method == 'GET': 
		serializer = VideoSerializer(tuts, many=True)
		return JsonResponse(serializer.data,  safe=False)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = VideoSerializer(tut, data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data)
        return JsonResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        tut.delete()
        return HttpResponse(status=204)
Exemplo n.º 6
0
def video_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = TutorialResource.objects.filter(status=1, id=1)
        serializer = VideoSerializer(snippets, many=True)
        return JsonResponse(serializer.data, safe=False)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = VideoSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(serializer.data, status=201)
        return JsonResponse(serializer.errors, status=400)
Exemplo n.º 7
0
 def get(self, request, format=None):
     try:
         if request.auth == None:
             raise PermissionDenied
         queryset = Video.objects.all()
         serializer = VideoSerializer(queryset, many=True)
         res = {'videoList': serializer.data}  # 응답코드에 포함될 데이터
         return Response(res, status=status.HTTP_200_OK)
     except FieldDoesNotExist as error:
         return Response(
             {
                 'error': "FieldDoesNotExist ",
                 'date': datetime.now()
             },
             status=status.HTTP_400_BAD_REQUEST)
     except PermissionDenied as error:
         return Response(
             {
                 'error': "PermissionDenied",
                 'date': datetime.now()
             },
             status=status.HTTP_400_BAD_REQUEST)
def record() :
    path = '/home/pi/recorded'  # save path

    target = Record.objects.get(id = 1)
    serializer = RecordSerializer(target, many = False)
    state = serializer.data['recording']
    # rpi setting
    GPIO.setmode(GPIO.BCM)
    pir_pin = 7
    GPIO.setup(pir_pin, GPIO.IN)
    camera = PiCamera()

    try:
        while state :
            target = Record.objects.get(id = 1)
            serializer = RecordSerializer(target, many = False)
            state = serializer.data['recording']
            
            if GPIO.input(pir_pin):  # motion detected
                print("motion detected")
                # take a video
                camera.resolution = [320, 240]
                camera.start_preview()

                now = datetime.datetime.now()
                start_time = time.time()

                vid_name = now.strftime('%Y%m%d-%H%M%S')
                vid_path = path + '/' + vid_name
                h264_vid_path = vid_path + '.h264'
                mp4_vid_path = vid_path + '.mp4'
                thumbnail_path = path + '/' + vid_name + '.jpg'

                camera.start_recording(output=h264_vid_path)
                time.sleep(1)
                camera.capture(thumbnail_path)
                while GPIO.input(pir_pin) :
                    print("recoring..")
                    time.sleep(2)
                camera.stop_recording()
                camera.stop_preview()
                result = subprocess.check_output("MP4Box -add {} {}".format(h264_vid_path, mp4_vid_path), shell=True)
                print(result)
            
                vid_time = time.strftime("%M:%S", time.gmtime(time.time()-start_time))

                # s3 upload
                s3 = boto3.client('s3', region_name = 'ap-northeast-2', aws_access_key_id=S3_ACCESS_KEY_ID, aws_secret_access_key=S3_SECRET_ACCESS_KEY)
                s3.upload_file(Filename = mp4_vid_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '.mp4')
                s3.upload_file(Filename = thumbnail_path, Bucket = S3_STORAGE_BUCKET_NAME, Key = vid_name + '_thumb.jpg')

                uploadVideo = {}
                uploadVideo['vid_name'] = vid_name
                uploadVideo['created'] = now
                uploadVideo['vid_time'] = vid_time
                uploadVideo['thumb'] = S3_ACCESS_URL + vid_name + '_thumb.jpg'
                serializer = VideoSerializer(data = uploadVideo)
                serializer.is_valid()
                serializer.save()
                print(vid_path, "upload success")
                os.remove(h264_vid_path)
                os.remove(mp4_vid_path)
                os.remove(thumbnail_path)
            else:
                camera.stop_preview()
    except KeyboardInterrupt:
        print("quit")
        GPIO.cleanup()