diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..558ec1c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/config.codekit +/bower_components +/.idea/ +/.codekit-cache/ +/node_modules \ No newline at end of file diff --git a/Gruntfile.js b/Gruntfile.js new file mode 100644 index 0000000..c5dbd6b --- /dev/null +++ b/Gruntfile.js @@ -0,0 +1,40 @@ +module.exports = function(grunt) { + + grunt.initConfig({ + pkg: grunt.file.readJSON('package.json'), + uglify: { + js: { + files : { + 'dist/aping-plugin-codebird.min.js' : [ + 'src/aping-codebird-directive.js', + 'src/aping-codebird-helper.js', + 'bower_components/codebird-js/codebird.js' + ] + } + }, + options: { + banner: '\n/*! <%= pkg.name %> <%= grunt.template.today("dd-mm-yyyy") %> */\n', + } + }, + watch: { + minifiyJs: { + files: [ + 'src/aping-codebird-directive.js', + 'src/aping-codebird-helper.js', + 'bower_components/codebird-js/codebird.js' + ], + tasks: ['uglify'], + options: { + spawn: true, + } + } + } + }); + + grunt.loadNpmTasks('grunt-contrib-uglify'); + grunt.loadNpmTasks('grunt-contrib-watch'); + + grunt.registerTask('default', ['watch']); + +}; + diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..085a684 --- /dev/null +++ b/bower.json @@ -0,0 +1,32 @@ +{ + "name": "apiNG-plugin-codebird", + "homepage": "https://github.com/JohnnyTheTank/apiNG-plugin-codebird", + "authors": [ + "Jonathan Hornung " + ], + "version": "0.1.0", + "description": "twitter plugin for apiNG", + "main": "dist/apiNG-plugin-codebird.min.js", + "moduleType": [], + "keywords": [ + "aping", + "twitter", + "codebird", + "angularjs", + "rest", + "api", + "json" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "dependencies": { + "apiNG": "*", + "codebird-js": "*" + } +} diff --git a/demo/aping-config.js b/demo/aping-config.js new file mode 100644 index 0000000..54bc947 --- /dev/null +++ b/demo/aping-config.js @@ -0,0 +1,17 @@ +"use strict"; +apingApp.config(['$provide', function ($provide) { + + $provide.constant("apingApiKeys", { + twitter : "AAAAAAAAAAAAAAAAAAAAACs3iwAAAAAA%2BH8LdT7YfKIzk8fvQvqKvoePuxo%3DMKfXX2ojtacGL8aOLAhlBDUrEFpy0DUuiyebet22Wa2YVrX7cc", + }); + + $provide.constant("apingDefaultSettings", { + templateUrl : "aping_design_sample.html", + items : 20, //items per request + maxItems: 100, //max items per aping + orderBy : "timestamp", + orderReverse : "true", + type: "social", + }); + +}]); \ No newline at end of file diff --git a/demo/aping_design_sample.html b/demo/aping_design_sample.html new file mode 100644 index 0000000..def01b1 --- /dev/null +++ b/demo/aping_design_sample.html @@ -0,0 +1,7 @@ +
+
+
+
{{item | json}}
+
+
+
diff --git a/demo/app.js b/demo/app.js new file mode 100644 index 0000000..34a466b --- /dev/null +++ b/demo/app.js @@ -0,0 +1 @@ +angular.module('app', ['jtt_aping', 'jtt_aping_codebird']); diff --git a/demo/index.html b/demo/index.html new file mode 100644 index 0000000..58d1230 --- /dev/null +++ b/demo/index.html @@ -0,0 +1,26 @@ + + + + + Codebird Demo + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/dist/aping-plugin-codebird.min.js b/dist/aping-plugin-codebird.min.js new file mode 100644 index 0000000..93d8d18 --- /dev/null +++ b/dist/aping-plugin-codebird.min.js @@ -0,0 +1,3 @@ + +/*! aping-plugin-codebird 22-11-2015 */ +"use strict";var jjtApingCodebird=angular.module("jtt_aping_codebird",[]).directive("apingCodebird",["apingApiKeys","apingCodebirdHelper","apingUtilityHelper",function(a,b,c){return{require:"?aping",restrict:"A",replace:"false",link:function(d,e,f,g,h){var i=g.getAppSettings(),j=c.parseJsonFromAttributes(f.apingCodebird,b.getThisPlattformString()),k=new Codebird;k.setBearerToken(a.twitter),j.forEach(function(a){if(a.search){var c={q:a.search,result_type:a.result_type||"mixed",count:a.items||i.items};k.__call("search_tweets",c,function(c){g.concatToResults(b.getObjectByJsonData(c,a.type)),g.apply()},!0)}else{if(!a.user)return!1;var c={screen_name:a.user,contributor_details:!0,count:a.items||i.items};k.__call("statuses_userTimeline",c,function(c,d,e){g.concatToResults(b.getObjectByJsonData(c,a.type)),g.apply()},!0)}})}}}]);jjtApingCodebird.service("apingCodebirdHelper",["apingModels","apingTimeHelper","apingUtilityHelper",function(a,b,c){this.getThisPlattformString=function(){return"twitter"},this.getThisPlattformLink=function(){return"https://twitter.com/"},this.getBigImageUrlFromSmallImageUrl=function(a){return a.replace("_normal","")},this.getImageUrlFromMediaObject=function(a){if(a){if(a.media_url_https)return this.getBigImageUrlFromSmallImageUrl(a.media_url_https);if(a.media_url)return this.getBigImageUrlFromSmallImageUrl(a.media_url)}return!1},this.getImageUrlFromUserObject=function(a){if(a){if(a.profile_image_url_https)return this.getBigImageUrlFromSmallImageUrl(a.profile_image_url_https);if(a.profile_image_url)return this.getBigImageUrlFromSmallImageUrl(a.profile_image_url)}return!1},this.getObjectByJsonData=function(a,b){var c=[];if(a){var d=this;a.statuses?angular.forEach(a.statuses,function(a,e){c.push(d.getItemByJsonData(a,b))}):a.length>0&&angular.forEach(a,function(a,e){c.push(d.getItemByJsonData(a,b))})}return c},this.getItemByJsonData=function(a,b){var c={};if(a&&b)switch(b){case"social":c=this.getSocialItemByJsonData(a)}return c},this.getSocialItemByJsonData=function(b){var c=a.getNew("social",this.getThisPlattformString());return $.extend(!0,c,{blog_name:b.user.screen_name,blog_id:b.user.id_str,blog_link:this.getThisPlattformLink()+b.user.screen_name+"/",intern_id:b.id_str,timestamp:new Date(Date.parse(b.created_at.replace(/( \+)/," UTC$1"))).getTime(),text:b.text,shares:b.retweet_count,likes:b.favorite_count}),b.entities&&b.entities.media&&b.entities.media.length>0&&(c.source=b.entities.media,c.img_url=this.getImageUrlFromMediaObject(b.entities.media[0])),c.img_url?c.type="image":(c.type="tweet",b.user&&(c.img_url=this.getImageUrlFromUserObject(b.user))),c.post_url=c.blog_link+"status/"+c.intern_id,c}}]),function(undefined){Array.prototype.indexOf||(Array.prototype.indexOf=function(a,b){for(var c=b||0;c-1||navigator.userAgent.indexOf("Trident/5")>-1||navigator.userAgent.indexOf("MSIE 7.0")>-1),_use_proxy="undefined"!=typeof navigator&&"undefined"!=typeof navigator.userAgent,_oauth_token=null,_oauth_token_secret=null,_version="2.6.0",setConsumerKey=function(a,b){_oauth_consumer_key=a,_oauth_consumer_secret=b},setBearerToken=function(a){_oauth_bearer_token=a},getVersion=function(){return _version},setToken=function(a,b){_oauth_token=a,_oauth_token_secret=b},setUseProxy=function(a){_use_proxy=!!a},setProxy=function(a){a.match(/\/$/)||(a+="/"),_endpoint_proxy=a},_parse_str=function(str,array){var glue1="=",glue2="&",array2=String(str).replace(/^&?([\s\S]*?)&?$/,"$1").split(glue2),i,j,chr,tmp,key,value,bracket,keys,evalStr,fixStr=function(a){return decodeURIComponent(a).replace(/([\\"'])/g,"\\$1").replace(/\n/g,"\\n").replace(/\r/g,"\\r")};for(array||(array=this.window),i=0;i0&&(i+="/"),i+=j[g];var k=["screen_name","place_id"];for(g=0;gh;h++)m=m.split(String.fromCharCode(65+h)).join("_"+String.fromCharCode(97+h));console.warn('To call the templated method "'+m+'", specify the parameter value for "'+o+'".')}i=i.split(f).join(e[o]),delete e[o]}for(g=0;26>g;g++)i=i.split(String.fromCharCode(65+g)).join("_"+String.fromCharCode(97+g)),m=m.split(String.fromCharCode(65+g)).join("_"+String.fromCharCode(97+g));var p=_detectMethod(m,e),q=_detectMultipart(m),r=_detectInternal(m);return _callApi(p,i,e,q,d,r,c)},oauth_authenticate=function(a,b){"undefined"==typeof a.force_login&&(a.force_login=null),"undefined"==typeof a.screen_name&&(a.screen_name=null),null===_oauth_token&&console.warn("To get the authenticate URL, the OAuth token must be set.");var c=_endpoint_oauth+"oauth/authenticate?oauth_token="+_url(_oauth_token);return a.force_login===!0&&(c+="&force_login=1",null!==a.screen_name&&(c+="&screen_name="+a.screen_name)),b(c),!0},oauth_authorize=function(a,b){"undefined"==typeof a.force_login&&(a.force_login=null),"undefined"==typeof a.screen_name&&(a.screen_name=null),null===_oauth_token&&console.warn("To get the authorize URL, the OAuth token must be set.");var c=_endpoint_oauth+"oauth/authorize?oauth_token="+_url(_oauth_token);return a.force_login===!0&&(c+="&force_login=1",null!==a.screen_name&&(c+="&screen_name="+a.screen_name)),b(c),!0},oauth2_token=function(a){null===_oauth_consumer_key&&console.warn("To obtain a bearer token, the consumer key must be set."),"undefined"==typeof a&&(a=function(){});var b="grant_type=client_credentials",c=_endpoint_oauth+"oauth2/token";_use_proxy&&(c=c.replace(_endpoint_base,_endpoint_proxy));var d=_getXmlRequestObject();null!==d&&(d.open("POST",c,!0),d.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),d.setRequestHeader((_use_proxy?"X-":"")+"Authorization","Basic "+_base64_encode(_oauth_consumer_key+":"+_oauth_consumer_secret)),d.onreadystatechange=function(){if(d.readyState>=4){var b=12027;try{b=d.status}catch(c){}var e="";try{e=d.responseText}catch(c){}var f=_parseApiReply(e);f.httpstatus=b,200===b&&setBearerToken(f.access_token),a(f)}},d.send(b))},_url=function(a){return/boolean|number|string/.test(typeof a)?encodeURIComponent(a).replace(/!/g,"%21").replace(/'/g,"%27").replace(/\(/g,"%28").replace(/\)/g,"%29").replace(/\*/g,"%2A"):""},_sha1=function(){function a(a,c){a[c>>5]|=128<<24-c%32,a[(c+64>>9<<4)+15]=c;for(var d=new Array(80),e=1732584193,f=-271733879,g=-1732584194,h=271733878,i=-1009589776,j=0;jp;p++){var q;16>p?q=a[j+p]:(q=d[p-3]^d[p-8]^d[p-14]^d[p-16],q=q<<1|q>>>31),d[p]=q,q=b(b(e<<5|e>>>27,20>p?f&g|~f&h:40>p?f^g^h:60>p?f&g|f&h|g&h:f^g^h),b(b(i,d[p]),20>p?1518500249:40>p?1859775393:60>p?-1894007588:-899497514)),i=h,h=g,g=f<<30|f>>>2,f=e,e=q}e=b(e,k),f=b(f,l),g=b(g,m),h=b(h,n),i=b(i,o)}return[e,f,g,h,i]}function b(a,b){var c=(65535&a)+(65535&b);return(a>>16)+(b>>16)+(c>>16)<<16|65535&c}function c(a){for(var b=[],c=(1<>5]|=(a.charCodeAt(e/d)&c)<<24-e%32;return b}var d=8;return function(b){var e=_oauth_consumer_secret+"&"+(null!==_oauth_token_secret?_oauth_token_secret:"");null===_oauth_consumer_secret&&console.warn("To generate a hash, the consumer secret must be set.");var f=c(e);f.length>16&&(f=a(f,e.length*d)),e=new Array(16);for(var g=new Array(16),h=0;16>h;h++)g[h]=909522486^f[h],e[h]=1549556828^f[h];for(f=a(g.concat(c(b)),512+b.length*d),e=a(e.concat(f),672),f="",g=0;g<4*e.length;g+=3)for(h=(e[g>>2]>>8*(3-g%4)&255)<<16|(e[g+1>>2]>>8*(3-(g+1)%4)&255)<<8|e[g+2>>2]>>8*(3-(g+2)%4)&255,b=0;4>b;b++)f=8*g+6*b>32*e.length?f+"=":f+"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(h>>6*(3-b)&63);return f}}(),_base64_encode=function(a){var b,c,d,e,f=0,g=0,h="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",i=[];if(!a)return a;do b=a.charCodeAt(f++),c=a.charCodeAt(f++),d=a.charCodeAt(f++),e=b<<16|c<<8|d,b=e>>18&63,c=e>>12&63,d=e>>6&63,e&=63,i[g++]=h.charAt(b)+h.charAt(c)+h.charAt(d)+h.charAt(e);while(fa&&console.warn("Invalid nonce length.");for(var b="",c=0;a>c;c++){var d=Math.floor(61*Math.random());b+="0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz".substring(d,d+1)}return b},_ksort=function(a){var b,c,d=[];b=function(a,b){var c=parseFloat(a),d=parseFloat(b),e=c+""===a,f=d+""===b;return e&&f?c>d?1:d>c?-1:0:e&&!f?1:!e&&f?-1:a>b?1:b>a?-1:0};for(c in a)a.hasOwnProperty(c)&&d.push(c);return d.sort(b),d},_clone=function(a){var b={};for(var c in a)"object"==typeof a[c]?b[c]=_clone(a[c]):b[c]=a[c];return b},_sign=function(a,b,c,d){"undefined"==typeof c&&(c={}),"undefined"==typeof d&&(d=!1),null===_oauth_consumer_key&&console.warn("To generate a signature, the consumer key must be set.");var e,f={consumer_key:_oauth_consumer_key,version:"1.0",timestamp:Math.round((new Date).getTime()/1e3),nonce:_nonce(),signature_method:"HMAC-SHA1"},g={};for(var h in f)e=f[h],g["oauth_"+h]=_url(e);null!==_oauth_token&&(g.oauth_token=_url(_oauth_token));var i=_clone(g);for(h in c)e=c[h],g[h]=e;for(var j=_ksort(g),k="",l=0;l-1)return d;console.warn("Can't find HTTP method to use for \""+a+'".')},_detectMultipart=function(a){var b=["statuses/update_with_media","account/update_profile_background_image","account/update_profile_image","account/update_profile_banner"];return b.indexOf(a)>-1},_buildMultipart=function(a,b){if(_detectMultipart(a)){var c=["statuses/update_with_media","account/update_profile_background_image","account/update_profile_image","account/update_profile_banner"],d={"statuses/update_with_media":"media[]","account/update_profile_background_image":"image","account/update_profile_image":"image","account/update_profile_banner":"banner"};if(-1!==c.indexOf(a)){d=d[a].split(" ");var e="--------------------"+_nonce(),f="";for(var g in b)f+="--"+e+'\r\nContent-Disposition: form-data; name="'+g+'"',d.indexOf(g)>-1&&(f+="\r\nContent-Transfer-Encoding: base64"),f+="\r\n\r\n"+b[g]+"\r\n";return f+="--"+e+"--"}}},_detectInternal=function(a){var b=["users/recommendations"];return b.join(" ").indexOf(a)>-1},_detectMedia=function(a){var b=["media/upload"];return b.join(" ").indexOf(a)>-1},_detectOld=function(a){var b=["account/push_destinations/device"];return b.join(" ").indexOf(a)>-1},_getEndpoint=function(a){var b;return b="oauth"===a.substring(0,5)?_endpoint_oauth+a:_detectMedia(a)?_endpoint_media+a+".json":_detectOld(a)?_endpoint_old+a+".json":_endpoint+a+".json"},_getXmlRequestObject=function(){var a=null;if("object"==typeof window&&window&&"undefined"!=typeof window.XMLHttpRequest)a=new window.XMLHttpRequest;else if("object"==typeof Ti&&Ti&&"undefined"!=typeof Ti.Network.createHTTPClient)a=Ti.Network.createHTTPClient();else if("undefined"!=typeof ActiveXObject)try{a=new ActiveXObject("Microsoft.XMLHTTP")}catch(b){console.error("ActiveXObject object not defined.")}else if("function"==typeof require&&require)try{var c=require("xmlhttprequest").XMLHttpRequest;a=new c}catch(d){try{var c=require("xhr2");a=new c}catch(e){console.error("xhr2 object not defined, cancelling.")}}return a},_callApi=function(a,b,c,d,e,f,g){"undefined"==typeof c&&(c={}),"undefined"==typeof d&&(d=!1),"undefined"==typeof e&&(e=!1),"function"!=typeof g&&(g=function(){}),f&&(c.adc="phone",c.application_id=333903271);var h=_getEndpoint(b),i=null,j=_getXmlRequestObject();if(null!==j){var k;if("GET"===a){var l=h;if("{}"!==JSON.stringify(c)&&(l+="?"+_http_build_query(c)),e||(i=_sign(a,h,c)),_use_jsonp){l+="{}"!==JSON.stringify(c)?"&":"?";var m=_nonce();window[m]=function(a){a.httpstatus=200;var b=null;"undefined"!=typeof j.getResponseHeader&&""!==j.getResponseHeader("x-rate-limit-limit")&&(b={limit:j.getResponseHeader("x-rate-limit-limit"),remaining:j.getResponseHeader("x-rate-limit-remaining"),reset:j.getResponseHeader("x-rate-limit-reset")}),g(a,b)},c.callback=m,l=h+"?"+_sign(a,h,c,!0);var n=document.createElement("script");n.type="text/javascript",n.src=l;var o=document.getElementsByTagName("body")[0];return void o.appendChild(n)}_use_proxy&&(l=l.replace(_endpoint_base,_endpoint_proxy).replace(_endpoint_base_media,_endpoint_proxy)),j.open(a,l,!0)}else{if(_use_jsonp)return void console.warn("Sending POST requests is not supported for IE7-9.");d?(e||(i=_sign(a,h,{})),c=_buildMultipart(b,c)):(e||(i=_sign(a,h,c)),c=_http_build_query(c)),k=c,(_use_proxy||d)&&(h=h.replace(_endpoint_base,_endpoint_proxy).replace(_endpoint_base_media,_endpoint_proxy)),j.open(a,h,!0),d?j.setRequestHeader("Content-Type","multipart/form-data; boundary="+k.split("\r\n")[0].substring(2)):j.setRequestHeader("Content-Type","application/x-www-form-urlencoded")}if(e){if(null===_oauth_consumer_key&&null===_oauth_bearer_token&&console.warn("To make an app-only auth API request, consumer key or bearer token must be set."),null===_oauth_bearer_token)return oauth2_token(function(){_callApi(a,b,c,d,e,!1,g)});i="Bearer "+_oauth_bearer_token}return null!==i&&j.setRequestHeader((_use_proxy?"X-":"")+"Authorization",i),j.onreadystatechange=function(){if(j.readyState>=4){var a=12027;try{a=j.status}catch(b){}var c="";try{c=j.responseText}catch(b){}var d=_parseApiReply(c);d.httpstatus=a;var e=null;"undefined"!=typeof j.getResponseHeader&&""!==j.getResponseHeader("x-rate-limit-limit")&&(e={limit:j.getResponseHeader("x-rate-limit-limit"),remaining:j.getResponseHeader("x-rate-limit-remaining"),reset:j.getResponseHeader("x-rate-limit-reset")}),g(d,e)}},j.send("GET"===a?null:k),!0}},_parseApiReply=function(a){if("string"!=typeof a||""===a)return{};if("[]"===a)return[];var b;try{b=JSON.parse(a)}catch(c){if(b={},0===a.indexOf(''))b.request=a.match(/(.*)<\/request>/)[1],b.error=a.match(/(.*)<\/error>/)[1];else for(var d=a.split("&"),e=0;e1?b[f[0]]=decodeURIComponent(f[1]):b[f[0]]=null}}return b};return{setConsumerKey:setConsumerKey,getVersion:getVersion,setToken:setToken,setBearerToken:setBearerToken,setUseProxy:setUseProxy,setProxy:setProxy,getApiMethods:getApiMethods,__call:__call,oauth_authenticate:oauth_authenticate,oauth_authorize:oauth_authorize,oauth2_token:oauth2_token}};"object"==typeof module&&module&&"object"==typeof module.exports?module.exports=Codebird:("object"==typeof window&&window&&(window.Codebird=Codebird),"function"==typeof define&&define.amd&&define("codebird",[],function(){return Codebird}))}(); \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..b128e9a --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "aping-plugin-codebird", + "version": "0.1.0", + "description": "twitter plugin for apiNG", + "main": "Gruntfile.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/JohnnyTheTank/apiNG-plugin-codebird.git" + }, + "keywords": [ + "aping", + "twitter", + "angularjs", + "rest", + "api", + "json", + "angular" + ], + "author": "Jonathan Hornung", + "license": "MIT", + "bugs": { + "url": "https://github.com/JohnnyTheTank/apiNG-plugin-codebird/issues" + }, + "homepage": "https://github.com/JohnnyTheTank/apiNG-plugin-codebird#readme", + "devDependencies": { + "grunt": "^0.4.5", + "grunt-contrib-uglify": "^0.10.1", + "grunt-contrib-watch": "^0.6.1" + } +} diff --git a/src/aping-codebird-directive.js b/src/aping-codebird-directive.js new file mode 100644 index 0000000..d3b6f47 --- /dev/null +++ b/src/aping-codebird-directive.js @@ -0,0 +1,70 @@ +"use strict"; + +/** + @author Jonathan Hornung (https://github.com/JohnnyTheTank) + @url https://github.com/JohnnyTheTank/apiNG-plugin-codebird + @licence MIT + */ + +var jjtApingCodebird = angular.module("jtt_aping_codebird", []) + .directive('apingCodebird', ['apingApiKeys', 'apingCodebirdHelper', 'apingUtilityHelper', function (apingApiKeys, apingCodebirdHelper, apingUtilityHelper) { + return { + require: '?aping', + restrict: 'A', + replace: 'false', + link: function (scope, element, attrs, apingController, interval) { + + var appSettings = apingController.getAppSettings(); + + var requests = apingUtilityHelper.parseJsonFromAttributes(attrs.apingCodebird, apingCodebirdHelper.getThisPlattformString()); + + var cb = new Codebird; + cb.setBearerToken(apingApiKeys.twitter); + + requests.forEach(function (request) { + + if(request.search) { + + //https://dev.twitter.com/rest/reference/get/search/tweets + var params = { + q: request.search, + result_type: request.result_type || "mixed", + count:request.items || appSettings.items, + }; + + cb.__call( + "search_tweets", + params, + function (_data) { + apingController.concatToResults(apingCodebirdHelper.getObjectByJsonData(_data, request.type)); + apingController.apply(); + }, + true + ); + + + + } else if(request.user) { + //https://dev.twitter.com/rest/reference/get/statuses/user_timeline + var params = { + screen_name: request.user, + contributor_details: true, + count: request.items || appSettings.items + }; + cb.__call( + "statuses_userTimeline", + params, + function (_data, rate, err) { + apingController.concatToResults(apingCodebirdHelper.getObjectByJsonData(_data, request.type)); + apingController.apply(); + }, + true + ); + } else { + return false; + } + + }); + } + } + }]); \ No newline at end of file diff --git a/src/aping-codebird-helper.js b/src/aping-codebird-helper.js new file mode 100644 index 0000000..01863d7 --- /dev/null +++ b/src/aping-codebird-helper.js @@ -0,0 +1,110 @@ +"use strict"; + +/** + @author Jonathan Hornung (https://github.com/JohnnyTheTank) + @url https://github.com/JohnnyTheTank/apiNG-plugin-codebird + @licence MIT + */ + +jjtApingCodebird.service('apingCodebirdHelper', ['apingModels', 'apingTimeHelper', 'apingUtilityHelper', function (apingModels, apingTimeHelper, apingUtilityHelper) { + this.getThisPlattformString = function () { + return "twitter"; + }; + + this.getThisPlattformLink = function () { + return "https://twitter.com/"; + }; + this.getBigImageUrlFromSmallImageUrl = function (_smallImageUrl) { + return _smallImageUrl.replace("_normal", ""); + }; + this.getImageUrlFromMediaObject = function (_item) { + if(_item) { + if(_item.media_url_https) { + return this.getBigImageUrlFromSmallImageUrl(_item.media_url_https); + } + if(_item.media_url) { + return this.getBigImageUrlFromSmallImageUrl(_item.media_url); + } + } + return false; + }; + this.getImageUrlFromUserObject = function (_item) { + if(_item) { + if(_item.profile_image_url_https) { + return this.getBigImageUrlFromSmallImageUrl(_item.profile_image_url_https); + } + if(_item.profile_image_url) { + return this.getBigImageUrlFromSmallImageUrl(_item.profile_image_url); + } + } + return false; + }; + + this.getObjectByJsonData = function (_data, _type) { + + var requestResults = []; + if (_data) { + var _this = this; + + if (_data.statuses) { + + angular.forEach(_data.statuses, function (value, key) { + requestResults.push(_this.getItemByJsonData(value, _type)); + }); + } else if (_data.length > 0) { + angular.forEach(_data, function (value, key) { + requestResults.push(_this.getItemByJsonData(value, _type)); + }); + } + + } + + return requestResults; + }; + + this.getItemByJsonData = function (_item, _type) { + var returnObject = {}; + if (_item && _type) { + switch (_type) { + case "social": + returnObject = this.getSocialItemByJsonData(_item); + break; + } + } + return returnObject; + }; + + this.getSocialItemByJsonData = function (_item) { + var socialObject = apingModels.getNew("social", this.getThisPlattformString()); + + $.extend(true, socialObject, { + blog_name: _item.user.screen_name, + blog_id: _item.user.id_str, + blog_link: this.getThisPlattformLink() + _item.user.screen_name + "/", + intern_id: _item.id_str, + timestamp: new Date(Date.parse(_item.created_at.replace(/( \+)/, ' UTC$1'))).getTime(), + text: _item.text, + shares: _item.retweet_count, + likes: _item.favorite_count, + }); + + if(_item.entities && _item.entities.media && _item.entities.media.length>0) { + socialObject.source = _item.entities.media; + socialObject.img_url = this.getImageUrlFromMediaObject(_item.entities.media[0]); + } + + if(socialObject.img_url) { + socialObject.type = "image"; + } else { + socialObject.type = "tweet"; + if(_item.user) { + socialObject.img_url = this.getImageUrlFromUserObject(_item.user); + } + } + + socialObject.post_url = socialObject.blog_link+"status/"+socialObject.intern_id; + + return socialObject; + }; + +}]); \ No newline at end of file