move news into its own folder and file

This commit is contained in:
Jon Heller 2015-10-19 00:42:34 -04:00
parent c2f9d0a06c
commit 859b7591fc
4 changed files with 128 additions and 34 deletions

View File

@ -30,6 +30,7 @@
<script src="js/compliments/compliments.js" type="text/javascript"></script> <script src="js/compliments/compliments.js" type="text/javascript"></script>
<script src="js/weather/weather.js" type="text/javascript"></script> <script src="js/weather/weather.js" type="text/javascript"></script>
<script src="js/time/time.js" type="text/javascript"></script> <script src="js/time/time.js" type="text/javascript"></script>
<script src="js/news/news.js" type="text/javascript"></script>
<script src="js/main.js?nocache=<?php echo md5(microtime()) ?>"></script> <script src="js/main.js?nocache=<?php echo md5(microtime()) ?>"></script>
<!-- <script src="js/socket.io.min.js"></script> --> <!-- <script src="js/socket.io.min.js"></script> -->

View File

@ -32,10 +32,8 @@ var config = {
'I\'d like to kick you in the teeth, but why should I improve your looks?', 'I\'d like to kick you in the teeth, but why should I improve your looks?',
'If I had a face like yours. I\'d sue my parents!' 'If I had a face like yours. I\'d sue my parents!'
] ]
},
news: {
feed: 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml'
} }
} }
// var feed = 'http://feeds.nos.nl/nosjournaal?format=rss';
//var feed = 'http://www.nu.nl/feeds/rss/achterklap.rss';
//var feed = 'http://www.nu.nl/feeds/rss/opmerkelijk.rss';
var feed = 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml';

View File

@ -26,9 +26,6 @@ function roundVal(temp)
jQuery(document).ready(function($) { jQuery(document).ready(function($) {
var news = [];
var newsIndex = 0;
var eventList = []; var eventList = [];
var lastCompliment; var lastCompliment;
@ -180,31 +177,6 @@ jQuery(document).ready(function($) {
weather.init(); weather.init();
(function fetchNews() { news.init();
$.feedToJson({
feed: feed,
success: function(data){
news = [];
for (var i in data.item) {
var item = data.item[i];
news.push(item.title);
}
}
});
setTimeout(function() {
fetchNews();
}, 60000);
})();
(function showNews() {
var newsItem = news[newsIndex];
$('.news').updateWithText(newsItem,2000);
newsIndex--;
if (newsIndex < 0) newsIndex = news.length - 1;
setTimeout(function() {
showNews();
}, 5500);
})();
}); });

123
js/news/news.js Normal file
View File

@ -0,0 +1,123 @@
// A lot of this code is from the original feedToJson function that was included with this project
// The new code allows for multiple feeds to be used but a bunch of variables and such have literally been copied and pasted into this code and some help from here: http://jsfiddle.net/BDK46/
// The original version can be found here: http://airshp.com/2011/jquery-plugin-feed-to-json/
var news = {
feed: config.news.feed || null,
newsLocation: '.news',
newsItems: [],
seenNewsItem: [],
_yqURL: 'http://query.yahooapis.com/v1/public/yql',
_yqlQS: '?format=json&q=select%20*%20from%20rss%20where%20url%3D',
_cacheBuster: Math.floor((new Date().getTime()) / 1200 / 1000),
_failedAttempts: 0,
fetchInterval: config.news.fetchInterval || 60000,
updateInterval: config.news.interval || 5500,
fadeInterval: 2000,
intervalId: null,
fetchNewsIntervalId: null
}
news.buildQueryString = function (feed) {
return this._yqURL + this._yqlQS + '\'' + encodeURIComponent(feed) + '\'';
}
news.fetchNews = function () {
// Reset the news feed
this.newsItems = [];
this.feed.forEach(function (_curr) {
var _yqUrlString = this.buildQueryString(_curr);
this.fetchFeed(_yqUrlString);
}.bind(this));
}
news.fetchFeed = function (yqUrl) {
$.ajax({
type: 'GET',
datatype:'jsonp',
url: yqUrl,
success: function (data) {
this.parseFeed(data.query.results.item);
}.bind(this),
error: function () {
}
});
}
news.parseFeed = function (data) {
var _rssItems = [];
for (var i = 0, count = data.length; i < count; i++) {
_rssItems.push(data[i].title);
}
this.newsItems = this.newsItems.concat(_rssItems);
return true;
}
news.showNews = function () {
// If all items have been seen, swap seen to unseen
if (this.newsItems.length === 0 && this.seenNewsItem.length !== 0) {
if (this._failedAttempts === 20) {
console.error('Failed to show a news story 20 times, stopping any attempts');
return false;
}
this._failedAttempts++;
setTimeout(function () {
this.showNews();
}.bind(this), 30000);
} else if (this.newsItems.length === 0 && this.seenNewsItem.length !== 0) {
this.newsItems = this.seenNewsItem.splice(0);
}
var _location = Math.floor(Math.random() * this.newsItems.length);
var _item = news.newsItems.splice(_location, 1)[0];
this.seenNewsItem.push(_item);
$(this.newsLocation).updateWithText(_item, this.fadeInterval);
}
news.init = function () {
if (this.feed === null || this.feed instanceof Array) {
return false;
} else if (typeof this.feed === 'string') {
this.feed = [this.feed];
}
this.fetchNews();
this.showNews();
this.fetchNewsIntervalId = setInterval(function () {
this.fetchNews()
}.bind(this), this.fetchInterval)
this.intervalId = setInterval(function () {
this.showNews();
}.bind(this), this.updateInterval);
}