Beispiel #1
0
        private static void UploadVideo(VideoRecord record)
        {
            Print($"Uploading new video");
            var uri =
                $"https://{InstanceURL}/api/now/attachment/file?table_name=kb_knowledge&table_sys_id={record.ArticleSysId}&file_name={WebUtility.UrlEncode(record.FileName)}";
            var request = GetRequest(WebRequestMethods.Http.Post, uri, "video/mp4");

            var rs = request.GetRequestStream();

            using (FileStream fileStream = new FileStream(record.NewFilePath, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer    = new byte[4096];
                int    bytesRead = 0;
                while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    rs.Write(buffer, 0, bytesRead);
                }
            }

            var jobj = GetResponseJObject(request);

            if (jobj != null)
            {
                record.NewSysId = jobj["result"]["sys_id"].ToString();
                record.Status   = ProcessingStatus.Uploaded;
                Print($"Completed successfully. Attachment sys_id: {record.NewSysId}");
            }
        }
Beispiel #2
0
        private static void PatchTranslatedVersion(VideoRecord record)
        {
            Print($"Searching for translated version of article {record.ArticleSysId}");
            var uri     = $"https://{InstanceURL}/api/now/table/kb_knowledge?sysparm_query=parent.sys_id%3D{record.ArticleSysId}&sysparm_limit=1";
            var request = GetRequest(WebRequestMethods.Http.Get, uri);

            var jobj = GetResponseJObject(request);

            if (jobj == null)
            {
                Print($"Article {record.ArticleSysId} not translated yet");
                return;
            }
            var sys_id = "";
            var body   = "";

            if (jobj["result"].Any())
            {
                Print("Translated version has been found");
                sys_id = jobj["result"][0]["sys_id"].ToString();
                body   = jobj["result"][0]["text"].ToString();
                Print($"Translated version sys_id{sys_id}");

                var fixedBody = ReplaceOldSysIdInText(body, record);
                if (string.IsNullOrEmpty(fixedBody))
                {
                    return;
                }
                PatchArticle(record, fixedBody, sys_id);
            }
            else
            {
                Print($"Translated version not found");
            }
        }
Beispiel #3
0
        private static void PatchArticle(VideoRecord record, string patchedText, string sys_id = "")
        {
            Print($"Patching article body");
            var sysid = string.IsNullOrEmpty(sys_id) ? record.ArticleSysId : sys_id;
            var uri   = $"https://{InstanceURL}/api/now/table/kb_knowledge/{sysid}";

            var request = GetRequest("PATCH", uri);

            var requestBody = $"{{\"text\":\"{patchedText}\"}}";

            Print($"request body: {requestBody}");
            UTF8Encoding encoding  = new UTF8Encoding();
            var          byteArray = Encoding.ASCII.GetBytes(requestBody);

            request.ContentLength = byteArray.Length;

            Stream dataStream = request.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            var jobj = GetResponseJObject(request);

            if (jobj != null)
            {
                Print($"Atricle patched successfully");
                record.Status = ProcessingStatus.FixedArticle;
            }
            else
            {
                Print($"Error occured while patching article!");
                record.Status = ProcessingStatus.Corrupted;
            }
        }
Beispiel #4
0
        private static void ConvertVideo(VideoRecord record)
        {
            var newFile_path = Path.Combine(DefaultPath, "output");

            newFile_path = Path.Combine(newFile_path, record.FileName);
            if (File.Exists(newFile_path))
            {
                newFile_path = AddPrefixToFileName(newFile_path);
            }
            var processPath = Path.Combine(DefaultPath, "ffmpeg.exe");
            var argsl       = $"-y -i \"{record.FileName}\" -movflags faststart -acodec copy -vcodec copy \"output/{record.FileName}\"";


            var ffmpeg = new Process
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName               = "ffmpeg.exe",
                    Arguments              = argsl,
                    UseShellExecute        = false,
                    RedirectStandardOutput = true,
                    CreateNoWindow         = true
                }
            };

            ffmpeg.Start();
            ffmpeg.WaitForExit();
            if (ffmpeg.ExitCode == 0)
            {
                Print($"File converted and plased at path: {newFile_path}");
                record.NewFilePath = newFile_path;
                record.Status      = ProcessingStatus.Converted;
            }
        }
Beispiel #5
0
        private static void RemoveOldVideo(VideoRecord record)
        {
            Print($"Removing attachment {record.OldSysId} from table");
            var uri     = $"https://{InstanceURL}/api/now/attachment/{record.OldSysId}";
            var request = GetRequest("DELETE", uri);
            var jobj    = GetResponseJObject(request);

            if (jobj == null)
            {
                Print($"Removing old video from attachment table");
                record.Status = ProcessingStatus.Processed;
            }
        }
Beispiel #6
0
        private static void RequestVideo(VideoRecord record)
        {
            Print($"Requesting for video file");
            HttpWebResponse response = null;
            var             request  = GetRequest(WebRequestMethods.Http.Get, record.DownloadLink);
            var             path     = Path.Combine(DefaultPath, record.FileName);

            if (File.Exists(path))
            {
                path            = AddPrefixToFileName(path);
                record.FileName = Path.GetFileNameWithoutExtension(path);
            }
            try
            {
                response = (HttpWebResponse)request.GetResponse();
                var rs = response.GetResponseStream();

                FileStream fs         = File.Create(path);
                int        bufferSize = 1024;
                byte[]     buffer     = new byte[bufferSize];
                int        bytesRead  = 0;

                while ((bytesRead = rs.Read(buffer, 0, bufferSize)) != 0)
                {
                    fs.Write(buffer, 0, bytesRead);
                }
                record.FilePath = path;
                record.Status   = ProcessingStatus.Downloaded;
            }
            catch (WebException e)
            {
                if (e.Status == WebExceptionStatus.ProtocolError)
                {
                    response = (HttpWebResponse)e.Response;
                    Console.WriteLine("Errorcode: {0}", (int)response.StatusCode);
                }
                else
                {
                    Console.WriteLine("Error: {0}", e.Status);
                }
            }
            finally
            {
                if (response != null)
                {
                    response.Close();
                }
            }
        }
Beispiel #7
0
        private static string ReplaceOldSysIdInText(string text, VideoRecord record)
        {
            if (text.Contains(record.OldSysId))
            {
                Print($"sys_id: {record.OldSysId} is in article. Replacing with {record.NewSysId}");
                text = text.Replace(record.OldSysId, record.NewSysId);
                text = text.Replace("\"", "\\\"").Replace(Environment.NewLine, " ");
            }
            else
            {
                Print($"sys_id: {record.OldSysId} not found in article. Delete action will skipped.");
                text          = string.Empty;
                record.Status = ProcessingStatus.ProcessedWithError;
            }

            return(text);
        }
Beispiel #8
0
        private static string GetArticleBody(VideoRecord record)
        {
            Print($"Obtaining article from {InstanceURL}");
            var uri     = $"https://{InstanceURL}/api/now/table/kb_knowledge/{record.ArticleSysId}";
            var request = GetRequest(WebRequestMethods.Http.Get, uri);

            var obj  = GetResponseJObject(request);
            var text = "";

            if (obj != null)
            {
                text = obj["result"]["text"].ToString();
                Print($"Article obtained successfully.");
            }

            return(text);
        }
Beispiel #9
0
        private static void UpdateArticle(VideoRecord currentRecord)
        {
            var body = GetArticleBody(currentRecord);

            if (!string.IsNullOrEmpty(body))
            {
                var newBody = ReplaceOldSysIdInText(body, currentRecord);
                if (!string.IsNullOrEmpty(newBody))
                {
                    PatchArticle(currentRecord, newBody);
                }
                else
                {
                    Print("Article not need to be updated!");
                }

                PatchTranslatedVersion(currentRecord);
            }
            else
            {
                currentRecord.Status = ProcessingStatus.Corrupted;
            }
        }
Beispiel #10
0
        private static List <VideoRecord> GetVideoList()
        {
            Print($"Getting video metadata from {InstanceURL}");
            var uri =
                $"https://{InstanceURL}/api/now/attachment?sysparm_query=table_name%3Dkb_knowledge%5Econtent_type%3Dvideo%2Fmp4";
            var request    = GetRequest("GET", uri);
            var obj        = GetResponseJObject(request);
            var recordList = new List <VideoRecord>();

            foreach (var result in obj["result"])
            {
                var videoRecord = new VideoRecord();

                videoRecord.FileName     = result["file_name"].ToString();
                videoRecord.OldSysId     = result["sys_id"].ToString();
                videoRecord.DownloadLink = result["download_link"].ToString();
                videoRecord.ArticleSysId = result["table_sys_id"].ToString();
                videoRecord.Status       = ProcessingStatus.NotProcessed;
                recordList.Add(videoRecord);
            }

            return(recordList);
        }