Skip to content

Apricot Notification Protocol

Masaaki Kawata edited this page Feb 1, 2020 · 9 revisions

Examples

C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Net;

namespace ApricotNotify
{
    class Program
    {
        static void Main(string[] args)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Entry[]));
            List<Entry> list = new List<Entry>();

            list.Add(new Entry() { Resource = "http://www.apricotan.net/", Title = "APRICOTAN.NET" });
            list.Add(new Entry() { Title = "Test" });
            
            using (MemoryStream ms = new MemoryStream())
            {
                serializer.WriteObject(ms, list.ToArray());

                WebClient webClient = new WebClient();

                webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");
                webClient.UploadData("http://localhost:47806/alert", ms.GetBuffer());
            }
        }
    }

    [DataContract]
    public class Entry
    {
        [DataMember(Name = "resource")]
        public string Resource { get; set; }
        [DataMember(Name = "title")]
        public string Title { get; set; }
        [DataMember(Name = "description")]
        public string Description { get; set; }
        public string Author { get; set; }
        public DateTime Created { get; set; }
        public DateTime Modified { get; set; }
        public Uri Image { get; set; }
    }
}

PowerShell

# Send-ObjectToApricot.ps1
# Copyright © Masaaki Kawata All rights reserved.
# Usage: Send-ObjectToApricot.ps1 [object]
Add-Type -Assembly System.Runtime.Serialization

$xml = New-Object System.Xml.XmlDocument
$rootElement = $xml.CreateElement('root')
$rootElement.SetAttribute('type', 'array')
[void]$xml.AppendChild($rootElement)

$block = {
    $itemElement = $xml.CreateElement('item')
    $itemElement.SetAttribute('type', 'object')
    [void]$rootElement.AppendChild($itemElement)

    foreach ($key in $args[0].Keys) {
        $element = $xml.CreateElement($key.ToLowerInvariant())

        if ($args[0][$key] -eq $null) {
            $element.SetAttribute('type', 'null')
        }
        else {
            $element.SetAttribute('type', 'string')
            $element.InnerText = $args[0][$key]
        }

        [void]$itemElement.AppendChild($element)
    }
}

foreach ($dictionary in $input) {
    $block.Invoke($dictionary)
}

if ($args.length -gt 0) {
    foreach ($dictionary in $args[0]) {
        $block.Invoke($dictionary)
    }
}

if (-not $rootElement.HasChildNodes) {
    Write-Output("`nUsage: {0} [object] (e.g. {0} @(@{{title='MILCHCHAN.COM';resource='https://milchchan.com/'}})`n" -f (Split-Path -Leaf $PSCommandPath))

    exit
}

$memoryStream = New-Object System.IO.MemoryStream
$jsonWriter = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonWriter($memoryStream)

try {
    $xml.Save($jsonWriter)
    
    $webClient = New-Object System.Net.WebClient
    $webClient.Headers.Add([System.Net.HttpRequestHeader]::ContentType, 'application/json')
    [void]$webClient.UploadData('http://localhost:47806/alert', $memoryStream.ToArray())
}
finally {
    $jsonWriter.Close()
}
> .\Send-ObjectToApricot.ps1 @(@{title='MILCHCHAN.COM';resource='https://milchchan.com/'})

or

> powershell -ExecutionPolicy Unrestricted ".\Send-ObjectToApricot.ps1 @(@{title='MILCHCHAN.COM';resource='https://milchchan.com/'})"
Clone this wiki locally