vimeo - randomcrap - random crap programs of varying quality
 (HTM) git clone git://git.codemadness.org/randomcrap
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       vimeo (1994B)
       ---
            1 #!/bin/sh
            2 # Vimeo to Atom converter.
            3 # Can be used as an sfeed connector included script.
            4 # Dependencies: json2tsv, awk.
            5 #
            6 # URLs should be like: https://vimeo.com/api/v2/user/${useridhere}/videos.json
            7 
            8 # vimeo JSON video feed (API v2).
            9 # vimeo(name, feedurl, [basesiteurl], [encoding])
           10 vimeo() {
           11         feed "$1" "$2" "$3" "utf-8"
           12 }
           13 
           14 vimeo2atom() {
           15         json2tsv -F '\x1f' -R '\x1e' | \
           16         awk '
           17 BEGIN {
           18         FS = "\x1f"; RS = "\x1e";
           19         print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
           20         print "<feed xmlns=\"http://www.w3.org/2005/Atom\" xml:lang=\"en\">";
           21 }
           22 function xmlencode(s) {
           23         gsub("&", "\\&amp;", s);
           24         gsub("<", "\\&lt;", s);
           25         gsub(">", "\\&gt;", s);
           26         gsub("\"", "\\&quot;", s);
           27         gsub("'"'"'", "\\&#39;", s);
           28         return s;
           29 }
           30 function show() {
           31         if (!length(o["id"]))
           32                 return;
           33 
           34         uploaded = o["upload_date"];
           35         gsub(" ", "T", uploaded);
           36         uploaded = uploaded "Z";
           37 
           38         s = int(o["duration"]);
           39         m = s / 60;
           40         h = s / 3600;
           41         title = sprintf("%s [%d:%02d:%02d]", o["title"], h, m % 60, s % 60);
           42         title = xmlencode(title);
           43         id = xmlencode(o["id"]);
           44         author = xmlencode(o["user_name"]);
           45         enclosure = xmlencode(o["thumbnail_large"]);
           46         description = o["description"]; # do not xmlencode(), it is wrapped in CDATA.
           47 
           48         print "<entry>";
           49         print  "\t<title type=\"text\">" title "</title>";
           50         print  "\t<published>" uploaded "</published>";
           51         print  "\t<updated>" uploaded "</updated>";
           52         print  "\t<link rel=\"alternate\" type=\"text/html\" href=\"https://vimeo.com/" id "\"/>";
           53         if (description != "")
           54                 print "\t<content type=\"html\"><![CDATA[" description "]]></content>";
           55         if (author != "")
           56                 print  "\t<author><name>" author "</name></author>";
           57         if (enclosure != "")
           58                 print  "\t<link rel=\"enclosure\" type=\"image/jpeg\" href=\"" enclosure "\"/>";
           59         print  "</entry>";
           60 }
           61 $1 == "[]" && $2 == "o" {
           62         show();
           63         delete o;
           64 }
           65 $1 ~ /^\[\]\.[a-zA-Z0-9_]*$/ {
           66         o[substr($1, 4)] = $3;
           67 }
           68 END {
           69         show();
           70         print "</feed>";
           71 }'
           72 }
           73 
           74 # test:
           75 #hurl 'https://vimeo.com/api/v2/user/66583856/videos.json' | vimeo2atom | sfeed | sfeed_curses