<commit_before>/*
This function returns media analytics from an asset.

Input:
{
    "assetFaceRedactionId" : "nb:cid:UUID:88432c30-cb4a-4496-88c2-b2a05ce9033b", // Id of the source asset that contains media analytics (face redaction)
    "assetMotionDetectionId" : "nb:cid:UUID:88432c30-cb4a-4496-88c2-b2a05ce9033b",  // Id of the source asset that contains media analytics (motion detection)
    "timeOffset" :"00:01:00", // optional, offset to add to subtitles (used for live analytics)
    "copyToContainer" : "jpgfaces" // Optional, to copy jpg files to a specific container in the same storage account. Use lowercases as this is the container name and there are restrictions
    "deleteAsset" : true // Optional, delete the asset(s) once data has been read from it
 }

Output:
{
    "faceRedaction" :
        {
        "json" : "",      // the json of the face redaction
        "jsonOffset" : "",      // the json of the face redaction with offset
        "jpgFaces":[
                {
                    "id" :24,
                    "fileId": "nb:cid:UUID:a93464ae-cbd5-4e63-9459-a3e2cf869f0e",
                    "fileName": "ArchiveTopBitrate_video_800000_thumb000024.jpg",
                    "url" : "http://xpouyatdemo.streaming.mediaservices.windows.net/903f9261-d745-48aa-8dfe-ebcd6e6128d6/ArchiveTopBitrate_video_800000_thumb000024.jpg"
                }
                ]
        "pathUrl" : "",     // the path to the asset if asset is published
        },
    "motionDetection":
        {
        "json" : "",      // the json of the face redaction
        "jsonOffset" : ""      // the json of the face redaction with offset
        }
 }
*/

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "System.Xml.Linq"
#load "../Shared/mediaServicesHelpers.csx"
#load "../Shared/copyBlobHelpers.csx"

using System;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.Azure.WebJobs;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;

// Read values from the App.config file.
private static readonly string _mediaServicesAccountName = Environment.GetEnvironmentVariable("AMSAccount");
private static readonly string _mediaServicesAccountKey = Environment.GetEnvironmentVariable("AMSKey");

static string _storageAccountName = Environment.GetEnvironmentVariable("MediaServicesStorageAccountName");
static string _storageAccountKey = Environment.GetEnvironmentVariable("MediaServicesStorageAccountKey");

// Field for service context.
private static CloudMediaContext _context = null;
private static MediaServicesCredentials _cachedCredentials = null;
private static CloudStorageAccount _destinationStorageAccount = null;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"Webhook was triggered!");

    // Init variables
    string pathUrl = "";
    string jsonFaceRedaction = "";
    dynamic jpgFaces = new JArray() as dynamic;
    dynamic objFaceDetection = new JObject();
    dynamic objFaceDetectionOffset = new JObject();

    string jsonMotionDetection = "";
    dynamic objMotionDetection = new JObject();
    dynamic objMotionDetectionOffset = new JObject();

    string copyToContainer = "";
    string prefixjpg = "";
    string targetContainerUri = "";

    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    log.Info(jsonContent);

    if (data.assetFaceRedactionId == null && data.assetMotionDetectionId == null)
    {
        // for test
        // data.assetId = "nb:cid:UUID:d9496372-32f5-430d-a4c6-d21ec3e01525";

        return req.CreateResponse(HttpStatusCode.BadRequest, new
        {
            error = "Please pass asset ID in the input object (assetFaceRedactionId and/or assetMotionDetectionId)"
        });
    }

    log.Info($"Using Azure Media Services account : {_mediaServicesAccountName}");

    try
    {
        // Create and cache the Media Services credentials in a static class variable.
        _cachedCredentials = new MediaServicesCredentials(
                        _mediaServicesAccountName,
                        _mediaServicesAccountKey);

        // Used the chached credentials to create CloudMediaContext.
        _context = new CloudMediaContext(_cachedCredentials);


        //
        // FACE REDACTION
        //
        if (data.assetFaceRedactionId != null)
        {
            // Get the asset
            string assetid = data.assetFaceRedactionId;
            var outputAsset = _context.Assets.Where(a => a.Id == assetid).FirstOrDefault();

            if (outputAsset == null)
            {
                log.Info($"Asset not found {assetid}");
                return req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Asset not found"
                });
            }

            var jsonFile = outputAsset.AssetFiles.Where(a => a.Name.ToUpper().EndsWith(".JSON")).FirstOrDefault();
            var jpgFiles = outputAsset.AssetFiles.Where(a => a.Name.ToUpper().EndsWith(".JPG"));

            Uri publishurl = GetValidOnDemandPath(outputAsset);
            if (publishurl != null)
            {
                pathUrl = publishurl.ToString();
            }
            else
            {
                log.Info($"Asset not published");
            }

            if (data.copyToContainer != null)
            {
                copyToContainer = data.copyToContainer;
                // let's copy JPG to a container
                prefixjpg = outputAsset.Uri.Segments[1] + "-";
                log.Info($"prefixjpg {prefixjpg}");
                var sourceContainer = GetCloudBlobContainer(_storageAccountName, _storageAccountKey, outputAsset.Uri.Segments[1]);
                var targetContainer = GetCloudBlobContainer(_storageAccountName, _storageAccountKey, copyToContainer);
                CopyJPGsAsync(sourceContainer, targetContainer, prefixjpg, log);
                targetContainerUri = targetContainer.Uri.ToString();
            }

            foreach (IAssetFile file in jpgFiles)
            {
                string index = file.Name.Substring(file.Name.Length - 10, 6);
                int index_i = 0;
                if (int.TryParse(index, out index_i))
                {
                    dynamic entry = new JObject();
                    entry.id = index_i;
                    entry.fileId = file.Id;<commit_msg>Update to code date in container name<commit_after>/*
This function returns media analytics from an asset.

Input:
{
    "assetFaceRedactionId" : "nb:cid:UUID:88432c30-cb4a-4496-88c2-b2a05ce9033b", // Id of the source asset that contains media analytics (face redaction)
    "assetMotionDetectionId" : "nb:cid:UUID:88432c30-cb4a-4496-88c2-b2a05ce9033b",  // Id of the source asset that contains media analytics (motion detection)
    "timeOffset" :"00:01:00", // optional, offset to add to subtitles (used for live analytics)
    "copyToContainer" : "jpgfaces" // Optional, to copy jpg files to a specific container in the same storage account. Use lowercases as this is the container name and there are restrictions
    "deleteAsset" : true // Optional, delete the asset(s) once data has been read from it
 }

Output:
{
    "faceRedaction" :
        {
        "json" : "",      // the json of the face redaction
        "jsonOffset" : "",      // the json of the face redaction with offset
        "jpgFaces":[
                {
                    "id" :24,
                    "fileId": "nb:cid:UUID:a93464ae-cbd5-4e63-9459-a3e2cf869f0e",
                    "fileName": "ArchiveTopBitrate_video_800000_thumb000024.jpg",
                    "url" : "http://xpouyatdemo.streaming.mediaservices.windows.net/903f9261-d745-48aa-8dfe-ebcd6e6128d6/ArchiveTopBitrate_video_800000_thumb000024.jpg"
                }
                ]
        "pathUrl" : "",     // the path to the asset if asset is published
        },
    "motionDetection":
        {
        "json" : "",      // the json of the face redaction
        "jsonOffset" : ""      // the json of the face redaction with offset
        }
 }
*/

#r "Newtonsoft.Json"
#r "Microsoft.WindowsAzure.Storage"
#r "System.Xml.Linq"
#load "../Shared/mediaServicesHelpers.csx"
#load "../Shared/copyBlobHelpers.csx"

using System;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json;
using Microsoft.WindowsAzure.MediaServices.Client;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.IO;
using System.Web;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.Azure.WebJobs;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;

// Read values from the App.config file.
private static readonly string _mediaServicesAccountName = Environment.GetEnvironmentVariable("AMSAccount");
private static readonly string _mediaServicesAccountKey = Environment.GetEnvironmentVariable("AMSKey");

static string _storageAccountName = Environment.GetEnvironmentVariable("MediaServicesStorageAccountName");
static string _storageAccountKey = Environment.GetEnvironmentVariable("MediaServicesStorageAccountKey");

// Field for service context.
private static CloudMediaContext _context = null;
private static MediaServicesCredentials _cachedCredentials = null;
private static CloudStorageAccount _destinationStorageAccount = null;

public static async Task<object> Run(HttpRequestMessage req, TraceWriter log)
{
    log.Info($"Webhook was triggered!");

    // Init variables
    string pathUrl = "";
    string jsonFaceRedaction = "";
    dynamic jpgFaces = new JArray() as dynamic;
    dynamic objFaceDetection = new JObject();
    dynamic objFaceDetectionOffset = new JObject();

    string jsonMotionDetection = "";
    dynamic objMotionDetection = new JObject();
    dynamic objMotionDetectionOffset = new JObject();

    string copyToContainer = "";
    string prefixjpg = "";
    string targetContainerUri = "";

    string jsonContent = await req.Content.ReadAsStringAsync();
    dynamic data = JsonConvert.DeserializeObject(jsonContent);

    log.Info(jsonContent);

    if (data.assetFaceRedactionId == null && data.assetMotionDetectionId == null)
    {
        // for test
        // data.assetId = "nb:cid:UUID:d9496372-32f5-430d-a4c6-d21ec3e01525";

        return req.CreateResponse(HttpStatusCode.BadRequest, new
        {
            error = "Please pass asset ID in the input object (assetFaceRedactionId and/or assetMotionDetectionId)"
        });
    }

    log.Info($"Using Azure Media Services account : {_mediaServicesAccountName}");

    try
    {
        // Create and cache the Media Services credentials in a static class variable.
        _cachedCredentials = new MediaServicesCredentials(
                        _mediaServicesAccountName,
                        _mediaServicesAccountKey);

        // Used the chached credentials to create CloudMediaContext.
        _context = new CloudMediaContext(_cachedCredentials);


        //
        // FACE REDACTION
        //
        if (data.assetFaceRedactionId != null)
        {
            // Get the asset
            string assetid = data.assetFaceRedactionId;
            var outputAsset = _context.Assets.Where(a => a.Id == assetid).FirstOrDefault();

            if (outputAsset == null)
            {
                log.Info($"Asset not found {assetid}");
                return req.CreateResponse(HttpStatusCode.BadRequest, new
                {
                    error = "Asset not found"
                });
            }

            var jsonFile = outputAsset.AssetFiles.Where(a => a.Name.ToUpper().EndsWith(".JSON")).FirstOrDefault();
            var jpgFiles = outputAsset.AssetFiles.Where(a => a.Name.ToUpper().EndsWith(".JPG"));

            Uri publishurl = GetValidOnDemandPath(outputAsset);
            if (publishurl != null)
            {
                pathUrl = publishurl.ToString();
            }
            else
            {
                log.Info($"Asset not published");
            }

            if (data.copyToContainer != null)
            {
                copyToContainer = data.copyToContainer + DateTime.UtcNow.ToString("yyyyMMdd");
                // let's copy JPG to a container
                prefixjpg = outputAsset.Uri.Segments[1] + "-";
                log.Info($"prefixjpg {prefixjpg}");
                var sourceContainer = GetCloudBlobContainer(_storageAccountName, _storageAccountKey, outputAsset.Uri.Segments[1]);
                var targetContainer = GetCloudBlobContainer(_storageAccountName, _storageAccountKey, copyToContainer);
                CopyJPGsAsync(sourceContainer, targetContainer, prefixjpg, log);
                targetContainerUri = targetContainer.Uri.ToString();
            }

            foreach (IAssetFile file in jpgFiles)
            {
                string index = file.Name.Substring(file.Name.Length - 10, 6);
                int index_i = 0;
                if (int.TryParse(index, out index_i))
                {
                    dynamic entry = new JObject();
                    entry.id = index_i;
                    entry.fileId = file.Id;
{'fraction_non_alphanumeric': 0.09654717396085331, 'fraction_numerical': 0.02096620482369328, 'mean_word_length': 2.5259757043163606, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4736046', 'n_tokens_mistral': 3880, 'n_tokens_neox': 3663, 'n_words': 916}