Last active
March 3, 2016 17:19
-
-
Save ersgonzalo/05108bf7550e2f8e4b6f to your computer and use it in GitHub Desktop.
Twitter Service Angular 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Injectable} from 'angular2/core'; | |
import {URLSearchParams, Jsonp} from 'angular2/http'; | |
@Injectable() | |
export class TwitterService { | |
constructor(private jsonp:Jsonp) { | |
} | |
search(widgetID:string) { | |
let randomNumber:number = Math.random(); | |
function parse(data:any) { | |
let response = { | |
tweets: [] | |
}, | |
els:any, | |
el:any, | |
tweet:any, | |
x:number, | |
tmp:any; | |
if (data._body) { | |
els = $(data._body.body)[0].getElementsByClassName('timeline-Tweet'); | |
for (x = 0; x < els.length; x++) { | |
el = els[x]; | |
tweet = {}; | |
tweet.retweet = (el.getElementsByClassName('timeline-Tweet-retweetCredit').length > 0); | |
tweet.id = el.getAttribute('data-tweet-id'); | |
tmp = el.getElementsByClassName('timeline-Tweet-text')[0]; | |
tweet.title = tmp.innerHTML; | |
tweet.text = tmp.textContent || tmp.innerText; // IE8 doesn't support textContent | |
tmp = el.getElementsByClassName('timeline-Tweet-author')[0]; | |
tweet.author = { | |
url: tmp.getElementsByClassName('TweetAuthor-link')[0].getAttribute('href'), | |
avatar: tmp.getElementsByClassName('Avatar')[0].getAttribute('data-src-1x'), | |
fullName: tmp.getElementsByClassName('TweetAuthor-name')[0].innerText, | |
nickName: tmp.getElementsByClassName('TweetAuthor-screenName')[0].innerText | |
}; | |
tweet.updated = el.getElementsByClassName('dt-updated')[0].innerText; | |
tweet.link = el.getElementsByClassName('timeline-Tweet-timestamp')[0].getAttribute('href'); | |
if (el.getElementsByClassName('timeline-Tweet-media')[0]) { | |
tweet.inlineMedia = el.getElementsByClassName('timeline-Tweet-media')[0].innerHTML; | |
} | |
response.tweets.push(tweet); | |
} | |
} | |
return response; | |
} | |
return this.jsonp | |
.get(`http://cdn.syndication.twimg.com/widgets/timelines/${widgetID}?&lang=en&callback=JSONP_CALLBACK&suppress_response_codes=true&rnd=${randomNumber}`) | |
.toPromise() | |
.then(parse) | |
.then((unfilteredJSON:any) => { | |
let filteredJSON = {tweets: []}; | |
const pictureRegex = new RegExp(">pic.twitter.com/(.*?)<"); | |
const shortLinkString = `<span class="u-hiddenVisually">http://</span>zndg.tl/(.*?)</span>`; | |
const shortLinkRegex = new RegExp(shortLinkString); | |
let filterArray = []; | |
unfilteredJSON.tweets.map((tweet:any)=> { | |
if (tweet.retweet === false) | |
filterArray.push(tweet) | |
}); | |
for (let i = 0; i < filterArray.length; i++) { | |
filterArray[i].shortLink = filterArray[i].title.match(shortLinkRegex); | |
if (filterArray[i].shortLink) { | |
filterArray[i].shortLink = filterArray[i].shortLink[1]; | |
filterArray[i].shortLink = filterArray[i].shortLink.replace('<span class="u-hiddenVisually"> ', ''); | |
filterArray[i].shortLink = `http://zndg.tl/${filterArray[i].shortLink}`; | |
filterArray[i].title = filterArray[i].title.replace(shortLinkRegex, ''); | |
filterArray[i].title = filterArray[i].title.replace(' ', ''); | |
} | |
} | |
filterArray.map((tweet:any)=> { | |
if (!tweet.text.startsWith('RT')) | |
filteredJSON.tweets.push(tweet); | |
}); | |
return filteredJSON; | |
}) | |
.then((finalJSON:any) => { | |
return finalJSON; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment