Social media examples

From DISE KnowledgeBase

Jump to: navigation, search

Overview

In this tutorial we will go through usage of the Social media modifier in DISE Parser and write some code to handle the response coming from different social media APIs, such as Twitter and Facebook. The article requires some prior knowledge in JScript.

Info.png Read more: JScript (ECMAScript3) - microsoft.com

If you need more advice on how to customize this to your needs, please feel free to contact support!

Skeleton modifier tree

We will begin by building a skeleton modifier tree. Most examples below will originate from this, with the difference being done in the Script modifier. Parser will write everything to a file which could be data connected in a DISE Movie. At any time you can select File > Export in Parser to save the current modifier structure.

  1. Open your favorite text editor, create a new script file and save it in "C:\DISEContent" as "socialmedia.js"
  2. Begin by inserting a + > Network > Social media modifier.
  3. We don't have to type in anything right now, so click Ok.
  4. Select the Social media modifier, and insert a + > General > Script modifier.
  5. Browse to "C:\DISEContent\socialmedia.js" and click Open
  6. Click Ok.
  7. Select the Script modifier and insert a + > General > File write modifier.
  8. Browse to "C:\DISEContent\socialmediaresult.txt".
  9. Click Save.
  10. Click Ok.

Initial advice

While testing out the examples below, it is a good idea to initially cache the responses from the social media APIs. You can use the File write modifier added directly to the Social media modifier to write the response to file. Then, use the File read modifier to read in the response data, instead of taking the round-trip to the server.

How to use the results

You may ask yourself how you are to use the text files produced below. One way is to data connect the file in a table, text or image object. You may also stream the text out over a network using multicast.

Facebook

Documentation

Info.png Read more: Graph API - Facebook developers

Info.png Read more: Examples - Facebook developers

Settings

  • Open the Social media modifier settings by double clicking it.
  • Under Service type, make sure "Facebook" is selected.
  • Click Select to the right of Selected service.
  • Log in with a Facebook account and authenticate the application.

Lightbulb.png Note: The access token received by Facebook will expire after 60 days.

My likes

Here we simply list the names of things that I like on Facebook, one per line.

  • In the Social media modifier, in the URL field, type in "https://graph.facebook.com/me/likes". Exchange "me" for the ID of the user you wish to fetch likes for.
  • Paste the below code in the "C:\DISEContent\socialmedia.js" file and save.
// This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// specify what script engine to use 
#pragma language JScript
// json2.js by Douglas Crockford, https://github.com/douglascrockford/JSON-js
var JSON;if(!JSON){JSON={}}(function(){function f(a){return a<10?"0"+a:a}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","	":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})()
function format(string){return string.replace(/\r\n|\n/g,"<br/>");}
// Parse response string into object 
var response=JSON.parse(DPModifier.GetInput());
var output="";
if (!response.error&&response.data)
	// Iterate over collection
	for (var i=0,len=response.data.length;i<len;i++)
		output+=format(response.data[i].name)+"\n";
DPModifier.SetOutput(output);
  • Click Test in Parser.
  • Open the "C:\DISEContent\socialmediaresult.txt" file and observe the result.

List checkins at a location

The following lists the persons checked in to a specific location, and dates of check-ins.

  • In the Social media modifier, in the URL field, type in "https://graph.facebook.com/search?type=location&place=181380038549894". In our example this points to the DISE HQ in Karlstad, Sweden.
  • Paste the below code in the "C:\DISEContent\socialmedia.js" file and save.
// This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// specify what script engine to use 
#pragma language JScript
// json2.js by Douglas Crockford, https://github.com/douglascrockford/JSON-js
var JSON;if(!JSON){JSON={}}(function(){function f(a){return a<10?"0"+a:a}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","	":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})();
function parseISO8601(string){return new Date((string||"").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"").replace(/-/,"/").replace(/T/," ").replace(/Z/," UTC").replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"));}
function format(string){return string.replace(/\r\n|\n/g,"<br/>");}
// Parse response string into object 
var response=JSON.parse(DPModifier.GetInput());
var output="";
var settings={
				"separator":"|"
			};
if (!response.error&&response.data)
	// Iterate over collection
	for (var i=0,len1=response.data.length;i<len1;i++)
	{
		var item=response.data[i];
		var fields=[];
		fields.push(parseISO8601(item.created_time).toLocaleString());
		fields.push(format(item.from.name||""));
		if (item.tags&&item.tags.data)
			for (var j=0,len2=item.tags.data.length;j<len2;j++)
				fields.push(format(item.tags.data[j].name));
		output+=fields.join(settings.separator)+"\n";
	}
DPModifier.SetOutput(output);
  • Click Test in Parser.
  • Open the "C:\DISEContent\socialmediaresult.txt" file and observe the result.

Show messages from my wall

This will display top-level messages from my wall. The script may of course be edited to also include comments and other fields.

  • In the Social media modifier, in the URL field, type in "https://graph.facebook.com/me/feed". Exchange "me" for the ID of the user you wish to fetch the wall data for.
  • Paste the below code in the "C:\DISEContent\socialmedia.js" file and save.
// This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// specify what script engine to use 
#pragma language JScript
// json2.js by Douglas Crockford, https://github.com/douglascrockford/JSON-js
var JSON;if(!JSON){JSON={}}(function(){function f(a){return a<10?"0"+a:a}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","	":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})();
function parseISO8601(string){return new Date((string||"").replace(/^(\s|\u00A0)+|(\s|\u00A0)+$/g,"").replace(/-/,"/").replace(/T/," ").replace(/Z/," UTC").replace(/([\+-]\d\d)\:?(\d\d)/," $1$2"));}
function format(string){return string.replace(/\r\n|\n/g,"<br/>");}
// Parse response string into object 
var response=JSON.parse(DPModifier.GetInput());
var output="";
var settings={
				"images":{
							"path":"C:\\DISEContent\\Data\\Facebook\\Images"
						},
				"separator":"|"
			};
if (!response.error&&response.data)
	// Iterate over collection
	for (var i=0,len1=response.data.length;i<len1;i++)
	{
		var item=response.data[i];
		var fields=[];
		fields.push(parseISO8601(item.created_time).toLocaleString());
		fields.push(format(item.from.name));
		fields.push(format(item.message||item.description||""));
		if (item.picture)
		{
			var m=item.picture.match(/\.[^\.]+$/g);
			var imageName=(i+1)+(m!=null?m[0]:".image");
			fields.push("<DownloadFile src=\""+item.picture+"\" dst=\""+settings.images.path+"\\"+imageName+"\"/><image src=\""+settings.images.path+"\\"+imageName+"\"/>");
		}
		output+=fields.join(settings.separator)+"\n";
	}
DPModifier.SetOutput(output);
  • To support images, we need to add a Command modifier between the Script modifier and File write modifier. To do this, select the Script modifier and click + > General > Command.
  • Uncheck Replace <br/> tags with.
  • Click Ok.
  • Drag and drop the File write modifier on top of the Command modifier.
  • To change where images are being saved, change the "settings.images.path" setting in the js file. Remember to escape slashes (\ -> \\).
  • Click Test in Parser.
  • Open the "C:\DISEContent\socialmediaresult.txt" file and observe the result.

Twitter

Documentation

Info.png Read more: REST API v1.1 Resources | Twitter Developers

Settings

  • Open the Social media modifier settings by double clicking it.
  • Under Service type, make sure "Twitter" is selected.
  • Click Select to the right of Selected service.
  • Log in with a Twitter account and authenticate the application.

Get my timeline

The following example lists the tweets made to my timeline, formatted in a way to fit in a table object in a DISE Movie.

  • In the Social media modifier, in the URL field, type in "https://api.twitter.com/1.1/statuses/user_timeline.json", or "https://api.twitter.com/1.1/statuses/home_timeline.json"
  • Paste the below code in the "C:\DISEContent\socialmedia.js" file and save.
// This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// specify what script engine to use 
#pragma language JScript
// json2.js by Douglas Crockford, https://github.com/douglascrockford/JSON-js
var JSON;if(!JSON){JSON={}}(function(){function f(a){return a<10?"0"+a:a}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","	":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})();
function parseTwitterDate(string){return new Date(Date.parse(string.replace(/( \+)/, ' UTC$1')));}
function format(string){return string.replace(/\r\n|\n/g,"<br/>");}
// Parse response string into object 
var response=JSON.parse(DPModifier.GetInput());
var output="";
var settings={
				"images":{
							"path":"C:\\DISEContent\\Data\\Twitter\\Images"
						},
				"separator":"|"
			};
// Iterate over collection
for (var i=0,len1=response.length;i<len1;i++)
{
	var item=response[i];
	if (item.retweeted_status) 
		item=item.retweeted_status;
	var fields=[];
	if (item.user.profile_image_url)
	{
		var m=item.user.profile_image_url.match(/\.[^\.]+$/g);
		var imageName=(i+1)+(m!=null?m[0]:".image");
		fields.push("<DownloadFile src=\""+item.user.profile_image_url+"\" dst=\""+settings.images.path+"\\"+imageName+"\"/><image src=\""+settings.images.path+"\\"+imageName+"\"/>");
	}
	fields.push(format(item.user.name));
	fields.push(format(item.text||""));
	fields.push(parseTwitterDate(item.created_at).toLocaleString());
	output+=fields.join(settings.separator)+"\n";
}
DPModifier.SetOutput(output);
  • To support images, we need to add a Command modifier between the Script modifier and File write modifier. To do this, select the Script modifier and click + > General > Command.
  • Uncheck Replace <br/> tags with.
  • Click Ok.
  • Drag and drop the File write modifier on top of the Command modifier.
  • To change where images are being saved, change the "settings.images.path" setting in the js file. Remember to escape slashes (\ -> \\).
  • Click Test in Parser.
  • Open the "C:\DISEContent\socialmediaresult.txt" file and observe the result.
  • To show in a DISE Movie, please do the following:
  • Create a new DISE Movie in DISE Composer.
  • Insert a table object.
  • Select the Table format tab and enter 1 under Rows and 3 under Columns.
  • Select the Data tab and click the Connect data button.
  • Browse to the file C:\DISEContent\socialmediaresult.txt downloaded by Parser.
  • Click Ok.
  • Still in the Data tab, enter | in the delimiter field.
  • Select the Options tab.
  • Select all cells in the center and rightmost column and tick the Word break checkbox.
  • Also, under Behavior when there is too much text to render, select "Resize the text to fit".
  • Select the Text tab and select Size 40 for the user name column.
  • Go to the Paged tab and select Multi-page table and Display only one page per scene.
  • In the Table format tab, now set the column width to Percent: 10, Percent: 30 and Auto, respectively.
  • In the Layout tab, select all columns in the Preview and under Margins, put in Right: 20, Top: 20, Bottom: 20.
  • Click Ok and view the result. You should have something similar to this:

Twitter table result.png

Search for tweets

The following example searches for the 50 most recent tweets with a hashtag "#ISE2014", formatted in a way to fit in a table object in a DISE Movie.

  • In the Social media modifier, in the URL field, type in "https://api.twitter.com/1.1/search/tweets.json?q=%23ISE2014&count=50".

Lightbulb.png Note: In the URL, the "#" character has to be escaped into "%23".

  • Paste the below code in the "C:\DISEContent\socialmedia.js" file and save.
// This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
// specify what script engine to use 
#pragma language JScript
// json2.js by Douglas Crockford, https://github.com/douglascrockford/JSON-js
var JSON;if(!JSON){JSON={}}(function(){function f(a){return a<10?"0"+a:a}function quote(a){escapable.lastIndex=0;return escapable.test(a)?'"'+a.replace(escapable,function(a){var b=meta[a];return typeof b==="string"?b:"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+a+'"'}function str(a,b){var c,d,e,f,g=gap,h,i=b[a];if(i&&typeof i==="object"&&typeof i.toJSON==="function"){i=i.toJSON(a)}if(typeof rep==="function"){i=rep.call(b,a,i)}switch(typeof i){case"string":return quote(i);case"number":return isFinite(i)?String(i):"null";case"boolean":case"null":return String(i);case"object":if(!i){return"null"}gap+=indent;h=[];if(Object.prototype.toString.apply(i)==="[object Array]"){f=i.length;for(c=0;c<f;c+=1){h[c]=str(c,i)||"null"}e=h.length===0?"[]":gap?"[\n"+gap+h.join(",\n"+gap)+"\n"+g+"]":"["+h.join(",")+"]";gap=g;return e}if(rep&&typeof rep==="object"){f=rep.length;for(c=0;c<f;c+=1){if(typeof rep[c]==="string"){d=rep[c];e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}else{for(d in i){if(Object.prototype.hasOwnProperty.call(i,d)){e=str(d,i);if(e){h.push(quote(d)+(gap?": ":":")+e)}}}}e=h.length===0?"{}":gap?"{\n"+gap+h.join(",\n"+gap)+"\n"+g+"}":"{"+h.join(",")+"}";gap=g;return e}}"use strict";if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(a){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(a){return this.valueOf()}}var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","	":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;if(typeof JSON.stringify!=="function"){JSON.stringify=function(a,b,c){var d;gap="";indent="";if(typeof c==="number"){for(d=0;d<c;d+=1){indent+=" "}}else if(typeof c==="string"){indent=c}rep=b;if(b&&typeof b!=="function"&&(typeof b!=="object"||typeof b.length!=="number")){throw new Error("JSON.stringify")}return str("",{"":a})}}if(typeof JSON.parse!=="function"){JSON.parse=function(text,reviver){function walk(a,b){var c,d,e=a[b];if(e&&typeof e==="object"){for(c in e){if(Object.prototype.hasOwnProperty.call(e,c)){d=walk(e,c);if(d!==undefined){e[c]=d}else{delete e[c]}}}}return reviver.call(a,b,e)}var j;text=String(text);cx.lastIndex=0;if(cx.test(text)){text=text.replace(cx,function(a){return"\\u"+("0000"+a.charCodeAt(0).toString(16)).slice(-4)})}if(/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,""))){j=eval("("+text+")");return typeof reviver==="function"?walk({"":j},""):j}throw new SyntaxError("JSON.parse")}}})();
function parseTwitterDate(string){return new Date(Date.parse(string.replace(/( \+)/, ' UTC$1')));}
function format(string){return string.replace(/\r\n|\n/g,"<br/>");}
// Parse response string into object 
var response=JSON.parse(DPModifier.GetInput());
var output="";
var settings={
				"images":{
							"path":"C:\\DISEContent\\Data\\Twitter\\Images"
						},
				"separator":"|"
			};
// Iterate over collection
for (var i=0,len1=response.statuses.length;i<len1;i++)
{
	var item=response.statuses[i];
	if (item.retweeted_status) 
		item=item.retweeted_status;
	var fields=[];
	if (item.user.profile_image_url)
	{
		var m=item.user.profile_image_url.match(/\.[^\.]+$/g);
		var imageName=(i+1)+(m!=null?m[0]:".image");
		fields.push("<DownloadFile src=\""+item.user.profile_image_url+"\" dst=\""+settings.images.path+"\\"+imageName+"\"/><image src=\""+settings.images.path+"\\"+imageName+"\"/>");
	}
	fields.push(format(item.user.name));
	fields.push(format(item.text||""));
	fields.push(parseTwitterDate(item.created_at).toLocaleString());
	output+=fields.join(settings.separator)+"\n";
}
DPModifier.SetOutput(output);
  • Otherwise follow the same routine as the example above for how to setup DISE Parser.
  • We want to present the table, 5 rows at a time. In Composer, create a new DISE Movie.
  • Insert a table object.
  • Select the Table format tab and enter 5 under Rows and 3 under Columns.
  • Select the Data tab and click the Connect data button.
  • Browse to the file C:\DISEContent\socialmediaresult.txt downloaded by Parser.
  • Click Ok.
  • Still in the Data tab, enter | in the delimiter field.
  • Select the Options tab.
  • Select all cells in the center and rightmost column and tick the Word break checkbox.
  • Also, under Behavior when there is too much text to render, select "Resize the text to fit".
  • Select the Text tab and select Size 40 for the user name column.
  • Go to the Paged tab and select Multi-page table and Display only one page per scene.
  • In the Table format tab, now set the column width to Percent: 10, Percent: 30 and Auto, respectively.
  • In the Layout tab, select all columns in the Preview and under Margins, put in Right: 20, Top: 20, Bottom: 20.
  • Click Ok and view the result.
  • After some formatting, you may end up with something like this:

Twitter table result2.png