Moved from amazon mail to caliban

This commit is contained in:
Martin Donnelly 2019-09-10 09:56:43 +01:00
parent fb862533dc
commit b8f967e1a5
17 changed files with 34182 additions and 736 deletions

View File

@ -1,6 +1,6 @@
{
"parserOptions": {
"ecmaVersion": 6,
"ecmaVersion": 2017,
"sourceType": "module",
"ecmaFeatures": {
"jsx": false

1
.gitignore vendored
View File

@ -189,3 +189,4 @@ node_modules
npm-debug.log
testem.log
/lib/newdata.json
!/scratch/

77
lib/euronews.js Normal file
View File

@ -0,0 +1,77 @@
const FeedMe = require('feedme');
const fecha = require('fecha');
const http = require('http');
const logger = require('log4js').getLogger('euronews');
module.exports = {
'getEuroNews': doGetEuroNews,
'render': render
};
class Template {
constructor(item) {
// "pubdate": "Tue, 06 Feb 2018 17:05:00 +0100",
const pubdateSrc = fecha.parse(item.pubdate, 'ddd, DD MMM YYYY HH:mm:SS ZZ');
const pubdate = fecha.format(pubdateSrc, 'dddd MMMM Do, YYYY');
const description = item.description.replace(/(<script(\s|\S)*?<\/script>)|(<style(\s|\S)*?<\/style>)|(<!--(\s|\S)*?-->)|(<\/?(\s|\S)*?>)/g, '');
this.data = `<article>
<header>
<a href="${item.guid.text}">${item.title}</a>
<time class="published">${pubdate}</time>
</header>
<p class="description">${description}</p>
</article>`;
}
toString() {
return this.data;
}
}
function doGetEuroNews() {
return new Promise((resolve, reject) => {
logger.info('Retrieving Euronews Headlines..');
http.get('http://feeds.feedburner.com/euronews/en/news/', (res) => {
const { statusCode } = res;
const contentType = res.headers['content-type'];
let error;
if (statusCode !== 200)
error = new Error('Request Failed.\n' +
`Status Code: ${statusCode}`);
else if (!/^text\/xml/.test(contentType))
error = new Error('Invalid content-type.\n' +
`Expected text/xml but received ${contentType}`);
if (error) {
logger.error(error.message);
// consume response data to free up memory
res.resume();
return reject(error);
}
const parser = new FeedMe(true);
res.pipe(parser);
parser.on('end', () => {
return resolve(parser.done());
});
});
});
}
function render(data) {
logger.debug('Rendering euronews');
// logger.debug(JSON.stringify(data));
const html = [];
const items = data.slice(0, 10);
for (const item of items)
html.push(new Template(item).toString());
return(html.join(''));
}

View File

@ -32,12 +32,12 @@ module.exports = {
url = ['http://www.computerhistory.org/tdih/', month, '/', day].join('');
logger.debug(url);
return new Promise(function(resolve, reject) {
'use strict';
request(url, function(err, resp, body) {
if (err)
if (err)
// Logger.error(err);
return reject(err);
// Throw err;
@ -51,12 +51,19 @@ module.exports = {
output.push(STRING(s).collapseWhitespace().s);
});
let $title = $('.chm-tdih-entry-title');
let $content = $('.chm-tdih-entry-content');
output.push(STRING($title.text()).collapseWhitespace().s);
output.push(STRING($content.text()).collapseWhitespace().s);
return resolve(output);
}, function(error, response, body) {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
return reject(error);
}
});
@ -94,7 +101,7 @@ module.exports = {
return new Promise(function(resolve, reject) {
request(url, function(err, resp, body) {
if (err)
if (err)
// Logger.error(err);
return reject(err);
// Throw err;
@ -106,7 +113,7 @@ module.exports = {
nbody.find('.story > p').each(function(div) {
const s = $(this).text();
if (s.indexOf('Today\'s recipe:') == -1)
if (s.indexOf('Today\'s recipe:') == -1)
output.push(s);
});
@ -115,7 +122,7 @@ module.exports = {
if (response.statusCode !== 200) {
logger.error(response.statusCode);
logger.error(body);
return reject(error);
}
});
@ -138,13 +145,13 @@ module.exports = {
})
.catch((e) => {
logger.error(e);
return reject(e);
});
})
.catch((e) => {
logger.error(e);
return reject(e);
});
});

View File

@ -5,10 +5,12 @@
* Time: 16:35
*
*/
const jade = require('pug'), UltraSES = require('ultrases'), dateFormat = require('dateformat');
const UltraSES = require('ultrases'), dateFormat = require('dateformat');
const logger = require('log4js').getLogger('mailer');
const pug = require('pug');
const mailer = new UltraSES({
'aws': {
'accessKeyId': 'AKIAJWJS75F7WNCGK64A',
@ -19,6 +21,17 @@ const mailer = new UltraSES({
}
});
const email = require('smtp-email-sender')({
'host': 'mail.caliban.io',
'port': '465',
'auth': {
'user': 'aida@caliban.io',
'pass': 'WaF#E+5am7.)\\csD',
'type': 'LOGIN' // PLAIN, LOGIN, MD5 etc...
},
'secure': 'secure'
});
module.exports = {
'sendEmailV1': function(todayCache, newpath) {
@ -29,9 +42,21 @@ module.exports = {
const template = {
'file': `${newpath }/` + 'jade/today.jade', 'locals': todayCache
};
mailer.sendTemplate(email, template, function(err) {
mailer.sendTemplate(email, template, err => {
if (err) throw err;
logger.info('compiled template email sent');
});
},
'sendSMTP': function(todayCache, newpath) {
const now = new Date();
const html = pug.renderFile( `${newpath }/jade/today.jade`, todayCache);
email({
'from': 'aida@caliban.io',
'to': 'martind2000@gmail.com',
'subject': `Today - ${ dateFormat(now, 'dddd, mmmm dS, yyyy')}`,
'html': html
});
}
};

View File

@ -9,17 +9,19 @@ const ftse = require('ftse');
module.exports = {
'getFTSE': function() {
return new Promise(function(resolve, reject) {
'use strict';
const err = 0;
ftse('100', 10, 'risers', function(items) {
if (items === err)
return reject(err);
return resolve(items);
});
});
}
'getFTSE': getFtse
};
function getFtse() {
return new Promise((resolve, reject) => {
'use strict';
const err = 0;
ftse('100', 10, 'risers', items => {
if (items === err)
return reject(err);
return resolve(items);
});
});
}

View File

@ -10,39 +10,41 @@ const forecastOptions = {
};
module.exports = {
'newDoGetWeather': function() {
'use strict';
return new Promise(function(resolve, reject) {
logger.info('New Retrieving weather..');
const j = {};
const forecast = new Forecast(forecastOptions);
forecast.get(55.95, -4.566667,
{ 'units': 'uk2' },
function(err, res, data) {
if (err)
return reject(err);
const tempMin = parseInt(data.daily.data[0].temperatureMin);
const tempMax = parseInt(data.daily.data[0].temperatureMax);
j.currently = data.currently.summary;
j.today = data.daily.data[0].summary;
j.later = data.daily.summary;
j.alerts = data.alerts || {};
j.data = data;
const fs = STRING(j.currently).endsWith('.') ? '' : '.';
if (tempMax === tempMin)
j.currently += `${fs } Around ${ tempMin.toString() } degrees.`;
else
j.currently += `${fs } Around ${ tempMin.toString() } to ${ tempMax.toString() } degrees.`;
// logger.debug(j);
return resolve(j);
});
});
}
'newDoGetWeather': doGetWeather
};
function doGetWeather() {
'use strict';
return new Promise((resolve, reject) => {
logger.info('New Retrieving weather..');
const j = {};
const forecast = new Forecast(forecastOptions);
forecast.get(55.95, -4.566667,
{'units': 'uk2'},
(err, res, data) => {
if (err)
return reject(err);
const tempMin = parseInt(data.daily.data[0].temperatureMin, 10);
const tempMax = parseInt(data.daily.data[0].temperatureMax, 10);
j.currently = data.currently.summary;
j.today = data.daily.data[0].summary;
j.later = data.daily.summary;
j.alerts = data.alerts || {};
j.data = data;
const fs = STRING(j.currently).endsWith('.') ? '' : '.';
if (tempMax === tempMin)
j.currently += `${fs } Around ${ tempMin.toString() } degrees.`;
else
j.currently += `${fs } Around ${ tempMin.toString() } to ${ tempMax.toString() } degrees.`;
// logger.debug(j);
return resolve(j);
});
});
}

5838
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -17,21 +17,31 @@
"cloudant": "^1.8.0",
"dateformat": "^3.0.2",
"elapsed": "0.0.7",
"fecha": "^2.3.2",
"feedme": "^1.1.2",
"fitbit-oauth2": "0.0.1",
"forecast.io": "0.0.11",
"ftse": "^1.0.6",
"jsonfile": "^4.0.0",
"lodash": "^4.17.4",
"log4js": "^2.3.10",
"memwatch-next": "^0.3.0",
"node-cron": "^1.2.1",
"node-localstorage": "^1.3.0",
"pug": "^2.0.0-rc.4",
"pug": "^2.0.4",
"request": "^2.83.0",
"smtp-email-sender": "^1.0.0",
"string": "^3.3.3",
"sugar": "^2.0.4",
"sugar-date": "^2.0.4",
"ultrases": "^0.1.3",
"xmljson": "^0.2.0"
},
"engines" : { "node" : ">= 8.1.0" }
"engines": {
"node": ">= 8.1.0"
},
"devDependencies": {
"ava": "1.0.0-beta.8",
"pretty-ms": "^3.1.0"
}
}

227
test/f.json Normal file
View File

@ -0,0 +1,227 @@
{
"@lang": "en-US",
"ResultSet": {
"@version": "2.0",
"@lang": "en-US",
"Error": "0",
"ErrorMessage": "No error",
"Locale": "en-US",
"Result": {
"yahoo_driving_directions": {
"routeHandle": "0",
"address": [
{
"type": "Origin",
"lat": "55.858",
"lon": "-4.259",
"line1": "",
"line2": "",
"line3": "",
"line4": "",
"country": ""
},
{
"type": "Destination",
"lat": "55.943",
"lon": "-4.556",
"line1": "",
"line2": "",
"line3": "",
"line4": "",
"country": ""
}
],
"total_distance": "30527",
"total_time": "27.0",
"total_time_with_traffic": "44.0",
"boundingbox": {
"north": "55.942587",
"south": "55.84886",
"east": "-4.259552",
"west": "-4.556462"
},
"route_id": "AHgACAAAAB4AAABQAAAAlgAAAJYAAAB42mOYxsDAxMQABGZrftRULt6RzgAFdvOrxQoZF1kx/P8PEXhgz4AEuICYXXDpXiYGptmHag7LnIBrNIivEXNhwKvxewdLFSPQYrigwsovSb4ghhILQwIDB1AOABQ9GtcLA3Cp",
"directions": {
"route_leg": [
{
"@type": "PrivateRouteLeg",
"number": "1",
"lat": "55.858",
"lon": "-4.259734",
"distance": "65",
"description": "Head toward Hope Street on Oswald Street. Go for 65 m.",
"time": "0",
"time_with_traffic": "0",
"turn_angle": "0",
"exit_num": "",
"man_type": "0",
"street": ", OSWALD STREET"
},
{
"@type": "PrivateRouteLeg",
"number": "2",
"lat": "55.858569",
"lon": "-4.259552",
"distance": "374",
"description": "Turn left onto Argyle Street. Go for 374 m.",
"time": "1",
"time_with_traffic": "1",
"turn_angle": "-90",
"exit_num": "",
"man_type": "9",
"street": ", ARGYLE STREET"
},
{
"@type": "PrivateRouteLeg",
"number": "3",
"lat": "55.859127",
"lon": "-4.265453",
"distance": "186",
"description": "Turn right onto Douglas Street. Go for 186 m.",
"time": "1",
"time_with_traffic": "1",
"turn_angle": "90",
"exit_num": "",
"man_type": "13",
"street": ", DOUGLAS STREET"
},
{
"@type": "PrivateRouteLeg",
"number": "4",
"lat": "55.860769",
"lon": "-4.264906",
"distance": "30",
"description": "Turn left onto Waterloo Street. Go for 30 m.",
"time": "0",
"time_with_traffic": "0",
"turn_angle": "-90",
"exit_num": "",
"man_type": "9",
"street": ", WATERLOO STREET"
},
{
"@type": "PrivateRouteLeg",
"number": "5",
"lat": "55.860811",
"lon": "-4.265388",
"distance": "3583",
"description": "Take ramp onto M8 toward Greenock/Glasgow Airport/Kilmarnock/M77. Go for 3.6 km.",
"time": "4",
"time_with_traffic": "4",
"turn_angle": "-30",
"exit_num": "",
"man_type": "19",
"street": "M8, ",
"sign": "Glasgow Airport, Greenock, Kilmarnock, M77"
},
{
"@type": "PrivateRouteLeg",
"number": "6",
"lat": "55.84886",
"lon": "-4.306791",
"distance": "14959",
"description": "Keep right onto M8. Go for 15.0 km.",
"time": "10",
"time_with_traffic": "10",
"turn_angle": "30",
"exit_num": "",
"man_type": "23",
"street": "M8, "
},
{
"@type": "PrivateRouteLeg",
"number": "7",
"lat": "55.898191",
"lon": "-4.483388",
"distance": "1391",
"description": "Take exit 30 toward Erskine Bridge/Erskine/Bishopton onto M898. Go for 1.4 km.",
"time": "1",
"time_with_traffic": "1",
"turn_angle": "-30",
"exit_num": "",
"man_type": "17",
"street": "M898, ",
"sign": "Bishopton, Erskine, Erskine Bridge"
},
{
"@type": "PrivateRouteLeg",
"number": "8",
"lat": "55.90745",
"lon": "-4.473388",
"distance": "2509",
"description": "Keep right onto A898 toward Erskine Br./Glasgow/Crianlarich/(A82). Go for 2.5 km.",
"time": "2",
"time_with_traffic": "2",
"turn_angle": "30",
"exit_num": "",
"man_type": "23",
"street": "A898, ",
"sign": "(A82), Crianlarich, Erskine Br., Glasgow"
},
{
"@type": "PrivateRouteLeg",
"number": "9",
"lat": "55.923564",
"lon": "-4.450718",
"distance": "3714",
"description": "Take ramp onto Great Western Road (A82) toward Crianlarich. Go for 3.7 km.",
"time": "3",
"time_with_traffic": "3",
"turn_angle": "-30",
"exit_num": "",
"man_type": "19",
"street": "A82, GREAT WESTERN ROAD",
"sign": "Crianlarich"
},
{
"@type": "PrivateRouteLeg",
"number": "10",
"lat": "55.931783",
"lon": "-4.502968",
"distance": "1656",
"description": "Take the 2nd exit from Dunglass Roundabout roundabout onto Dumbarton Road (A82) toward Crianlarich/Dumbarton/(A814). Go for 1.7 km.",
"time": "2",
"time_with_traffic": "2",
"turn_angle": "-90",
"exit_num": "",
"man_type": "29",
"street": "A82, DUMBARTON ROAD",
"sign": "(A814), Crianlarich, Dumbarton"
},
{
"@type": "PrivateRouteLeg",
"number": "11",
"lat": "55.935098",
"lon": "-4.526989",
"distance": "2060",
"description": "Turn left onto Glasgow Road (A814) toward Dumbarton/Town Centre/H'burgh. Go for 2.1 km.",
"time": "3",
"time_with_traffic": "3",
"turn_angle": "-90",
"exit_num": "",
"man_type": "9",
"street": "A814, GLASGOW ROAD",
"sign": "Dumbarton, H'burgh, Town Centre"
},
{
"@type": "PrivateRouteLeg",
"number": "12",
"lat": "55.942587",
"lon": "-4.556462",
"distance": "0",
"description": "Arrive at Glasgow Road (A814). Your destination is on the right.",
"time": "0",
"time_with_traffic": "0",
"turn_angle": "0",
"exit_num": "",
"man_type": "2",
"street": ", "
}
]
},
"copy_right": "Copyright &copy; 2018 Yahoo! Inc. All rights reserved. &copy; Navteq"
},
"geocode_results": null
}
}
}

23
test/foursquare.js Normal file
View File

@ -0,0 +1,23 @@
const request = require('request');
request({
url: 'https://api.foursquare.com/v2/venues/explore',
method: 'GET',
qs: {
client_id: 'IXXFUGW3NC3DEVS2V5EU4NV4CL5E12AYGUPIR2D3U3B5DX4B',
client_secret: 'MZRIJDCEKUMVERA1OKVAIZI0TYAEBD3W2A2AGPTPI5TOLL1D',
ll: '55.95,-4.56667',
section: 'food',
v: '20170801',
limit: 10
}
}, function(err, res, body) {
if (err) {
console.error(err);
} else {
console.log(body);
}
});
// 4c7ffa99d4e23704d87b5088

11469
test/log.txt Normal file

File diff suppressed because it is too large Load Diff

12157
test/log2.txt Normal file

File diff suppressed because it is too large Load Diff

4511
test/log3.txt Normal file

File diff suppressed because it is too large Load Diff

75
test/notes.txt Normal file
View File

@ -0,0 +1,75 @@
02-15 17:28:55.681 4336-6409/? D/Volley: [631] o.b: 10328 ms: [ ] https://sgws2.maps.yahoo.com/Directions?appid=y-aviate&time=now&cache=n&flags=J&olat=55.858&olon=-4.259&dlat=55.943&dlon=-4.556 0x0 NORMAL 11
02-15 17:31:00.188 4336-6424/? E/VolleyRequestBuilder: Response Error: com.android.volley.l: java.net.ConnectException: Connection refused
02-15 17:31:00.189 4336-6424/? D/Volley: [636] o.b: 134921 ms: [ ] http://ec2-54-227-120-212.compute-1.amazonaws.com/api/2/ab_retention 0x675f7ed5 NORMAL 66
mode=11 for public transport
https://sgws2.maps.yahoo.com/Directions?time=now&cache=n&flags=J&olat=55.862&olon=-4.255&dlat=55.943&dlon=-4.556&mode=11
mode=6 for walking
https://sgws2.maps.yahoo.com/Directions?time=now&cache=n&flags=J&olat=55.862&olon=-4.255&dlat=55.943&dlon=-4.556&mode=6
https://any-api.com/
http://feeds.reuters.com/reuters/UKTopNews
7 Times of the Day
Here are 7 important times or parts of the day in English.
MIDNIGHT
This is the middle of the night (00:00 hours).
MIDDAY
This is the middle of the day, also called "NOON" (12:00 hours).
MORNING
This is the time from midnight to midday.
AFTERNOON
This is the time from midday (noon) to evening.
From 12:00 hours to approximately 18:00 hours.
EVENING
This is the time from the end of the afternoon to midnight.
From approximately 18:00 hours to 00:00 hours.
DAWN
This is the time when the sun rises or comes up (sunrise).
DUSK
This is the time when the sun sets or goes down (sunset).
https://ws.spotify.com/search/1/artist?q=Mot%C3%B6rhead
https://api.songkick.com/api/3.0/artists/mbid:57961a97-3796-4bf7-9f02-a985a8979ae9/calendar.json?apikey=5q0GSDHbXHHSYsTj
With yelp
02-16 00:37:04.613 2280-4687/? D/VolleyRequestBuilder: Status Code: 200
Response Body: {"response":{"result":[{"cards":[{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1518745024,"layout":{"template":"venue_info"},"data":{"name":"Delhi Darbar","category":"Indian Restaurant","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/indian_64.png","id":"4d2cbd3fae3a8cfaa6d9be70","provider":"foursquare","tips":[],"images":[],"telephone":"","address":"151 glasgow road","city":"Dumbarton","state":"West Dunbartonshire","zip":"G82 1RE","latitude":55.94228694015362,"longitude":-4.555356528181545,"twitter":{"handle":null,"viewIntent":null,"tweetIntent":null},"yelp":{"url":"https://m.yelp.com/biz/delhi-darbar-dumbarton?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA","rating":2.0,"reviewCount":1,"viewIntent":"https://m.yelp.com/biz/delhi-darbar-dumbarton?adjust_creative=ogmBMO91tbdmscbTIaQEdA&utm_campaign=yelp_api&utm_medium=api_v2_search&utm_source=ogmBMO91tbdmscbTIaQEdA#Intent;action=android.intent.action.VIEW;end"}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":55.94228694015362,"poi_longitude":-4.555356528181545,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.558437,"req_latitude":55.942837,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"STREAM_TYPE":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.0},"instrumentation":{"rid":"79o0u8pd8c9tf","bucket":"ga"}}]}],"error":null}}
with twitter
02-16 00:32:51.726 2280-4382/? D/VolleyRequestBuilder: Status Code: 200
Response Body: {"response":{"result":[{"cards":[{"card_id":"9953c0e9-5b77-3162-bcf4-e19fcd1f541f","rendering_engine":"custom","type":"VENUE_INFO","type_display_name":"Venues","ttl":1518744771,"layout":{"template":"venue_info"},"data":{"name":"Domino's Pizza","category":"Pizza Place","iconUrl":"https://ss3.4sqi.net/img/categories_v2/food/pizza_64.png","id":"4bd2ca67a8b3a5939481685f","provider":"foursquare","tips":["Could really go a pizza","Bad for you but tastes so good!","Buy one get one free on collection yum!","Buy lots of food.It's yummy."],"images":["https://igx.4sqi.net/img/general/720x960/43992547_D3zLGLyZqgoVeL5Xy2ILve8FLR0Up7W55WgNJCvxylA.jpg","https://igx.4sqi.net/img/general/720x960/43992547_Lh1JDRDPQaWV197w64T27RAcB5GVepqKmXex5RvQ0FM.jpg"],"telephone":"","address":"Unit 3 Glasgow Rd","city":"Dumbarton","state":"West Dunbartonshire","zip":"G82 1QZ","latitude":55.94369145249164,"longitude":-4.561211789354899,"twitter":{"handle":"dominos_uk","viewIntent":"https://twitter.com/dominos_uk#Intent;action=android.intent.action.VIEW;package=com.twitter.android;end","tweetIntent":"#Intent;action=android.intent.action.SEND;component=com.twitter.android/.PostActivity;S.android.intent.extra.TEXT=@dominos_uk;end"},"yelp":{"url":null,"rating":0.0,"reviewCount":0,"viewIntent":null}},"modules":{},"reason":"","notify":false,"ranking_arguments":{"poi_latitude":55.94369145249164,"poi_longitude":-4.561211789354899,"USER_LOCATION:OTHER":1.0,"req_longitude":-4.558437,"req_latitude":55.942837,"USER_LOCATION_POI_CONFIDENCE:HIGH":1.0,"STREAM_TYPE":1.0,"rule_score":0.0,"TIER":0.0,"USER_DIST_FROM_REQ_LOC:WITHIN_100M":1.0,"score":0.0},"instrumentation":{"rid":"adbqe4hd8c9lj","bucket":"ga"}}]}],"error":null}}
02-16 00:33:18.575 2280-2280/? D/VolleyRequestBuilder: Requesting: https://aviate-yql.media.yahoo.com/v1/nearby_venues?ll=55.942840576171875%2C-4.558436870574951&lang=en-US
$ adb install com.tul.aviate_v3.1.3-14787.apk version with lots but no debug
api.cinelist.co.uk/search/cinemas/postcode/:postcode
api.cinelist.co.uk/search/cinemas/location/:location
api.cinelist.co.uk/search/cinemas/coordinates/:latitude/:longitude
api.cinelist.co.uk/get/times/cinema/:venueID?day=<INT>
adb install com.tul.aviate-2.5.0.2-12176-minAPI15.apk is a weird hybrid early version then goes back to orinal style with the next build

View File

@ -26,6 +26,12 @@ const quotes = require('./lib/quotes');
// var db_name = 'silvrgit';
// var dbCloudant = nano.use(db_name);
const memwatch = require('memwatch-next');
memwatch.on('leak', (info) => {
console.error('Memory leak detected:\n', info);
});
/*
We've moved to cloudant through IBM Bluemix for the database
@ -50,10 +56,10 @@ const cloudant = Cloudant({ 'account': credentials.username, 'password': credent
const dbCloudant = cloudant.db.use(credentials.database);
String.prototype.hashCode = function () {
if (Array.prototype.reduce)
if (Array.prototype.reduce)
return this.split('').reduce(function (a, b) {
a = ((a << 5) - a) + b.charCodeAt(0);
return a & a;
}, 0);
else {
@ -64,7 +70,7 @@ String.prototype.hashCode = function () {
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
};
@ -105,11 +111,11 @@ function runable() {
const now = new Date().getTime();
console.log(todayCache.last);
console.log('last updated', ((now - todayCache.last) / 60000));
if (now - todayCache.last < 3600000)
if (now - todayCache.last < 3600000)
return false;
else {
todayCache.last = now;
return true;
}
}
@ -117,7 +123,7 @@ function runable() {
logger.debug('Creating new today object.. Lets go!');
todayCache = Object.assign({}, todayCacheSrc);
todayCache.last = new Date().getTime();
return true;
}
}
@ -129,9 +135,9 @@ function broadcastWeather() {
'summary': todayCache.data.weather.data.currently.summary
};
if (todayCache.data.weather.data.hasOwnProperty('alerts'))
if (todayCache.data.weather.data.hasOwnProperty('alerts'))
wData.alerts = todayCache.data.weather.data.alerts;
// eventEmitter.emit('sendSocket', { 'id': 'weather', 'data': wData });
}
@ -140,7 +146,7 @@ function loadData() {
// localStorage.removeItem('today');
try {
const tempCache = localStorage.getItem('today');
if (tempCache !== null)
if (tempCache !== null)
todayCache = JSON.parse(tempCache);
}
catch (e) {
@ -162,7 +168,7 @@ function saveToDB(data) {
if (err) {
logger.error('Error inserting into couch');
logger.error(err);
return;
}
});
@ -172,7 +178,7 @@ function nth(d) {
// If (d > 3 && d < 21) {return 'th';} // Thanks kennebec
// if (d % 10 === 1) {return 'st';} else if (d % 10 === 2) {return 'nd';} else if (d % 10 === 3) {return 'rd';} else {return 'th';}
const n = d;
return Math.floor(n / 10) === 1 ? 'th' : (n % 10 === 1 ? 'st' : (n % 10 === 2 ? 'nd' : (n % 10 === 3 ? 'rd' : 'th')));
}
@ -181,13 +187,13 @@ function dayNumber() {
const start = new Date(now.getFullYear(), 0, 0);
const diff = now - start;
const oneDay = 1000 * 60 * 60 * 24;
return Math.floor(diff / oneDay);
}
function breakDay() {
const now = new Date();
return {
'year': now.getFullYear(),
'month': parseInt(now.getMonth()) + 1,
@ -210,7 +216,7 @@ function reduceTrains(d) {
}*/
for (const item in d)
for (const item in d)
if (typeof item.title !== 'undefined') {
const hash = item.title.hashCode();
if (titles.indexOf(hash) === -1) {
@ -218,7 +224,7 @@ function reduceTrains(d) {
ta.push(item);
}
}
return ta;
}
@ -234,13 +240,13 @@ function DayDiff(CurrentDate) {
const d = new Date();
DayCount = d.daysSince('beginning of this year');
return (DayCount);
}
Array.prototype.indexOfOld = Array.prototype.indexOf;
Array.prototype.indexOf = function (e, fn) {
if (!fn)
if (!fn)
return this.indexOfOld(e);
else {
if (typeof fn === 'string') {
@ -249,7 +255,7 @@ Array.prototype.indexOf = function (e, fn) {
return e[att];
};
}
return this.map(fn).indexOfOld(e);
}
};
@ -327,14 +333,14 @@ module.exports = {
},
'preLoadToday': async function () {
function compare(a, b) {
if (a.ts < b.ts) {
return -1;
}
if (a.ts > b.ts) {
return 1;
}
return 0;
}
if (a.ts < b.ts)
return -1;
if (a.ts > b.ts)
return 1;
return 0;
}
module.exports.getTodayDate();
todayCache.data.cal = { 'today': [], 'tomorrow': [], 'week': [] };
@ -394,7 +400,7 @@ module.exports = {
logger.error(e);
});
/*for (let t = 0; t < calHandler.calendars.length; t++)
/* for (let t = 0; t < calHandler.calendars.length; t++)
calHandler.getAdvancedCalV3(calHandler.calendars[t])
.then((d) => {
'use strict';
@ -408,17 +414,16 @@ module.exports = {
});*/
for (const item of calHandler.calendars)
await calHandler.getAdvancedCalV3(item)
.then((d) => {
todayCache.data.cal.today = todayCache.data.cal.today.concat(d.today);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.concat(d.tomorrow);
todayCache.data.cal.week = todayCache.data.cal.week.concat(d.week);
})
.catch((e) => {
'use strict';
logger.error(e);
});
await calHandler.getAdvancedCalV3(item)
.then((d) => {
todayCache.data.cal.today = todayCache.data.cal.today.concat(d.today);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.concat(d.tomorrow);
todayCache.data.cal.week = todayCache.data.cal.week.concat(d.week);
})
.catch((e) => {
'use strict';
logger.error(e);
});
console.log('>> SORT!!');
todayCache.data.cal.today = todayCache.data.cal.today.sort(compare);
@ -436,7 +441,7 @@ module.exports = {
console.error(e);
});
/*mdFitbit.getYesterdayFitbit()
/* mdFitbit.getYesterdayFitbit()
.then((d) => {
todayCache.data.fitbit = d;
})
@ -462,15 +467,18 @@ module.exports = {
setTimeout(function () {
loadData();
if (runable())
if (runable())
module.exports.preLoadToday();
// Module.exports.preLoadToday();
}, 5000);
setTimeout(function () {
logger.debug('Going to do the email...');
mdMailer.sendEmailV1(todayCache, __dirname);
// mdMailer.sendEmailV1(todayCache, __dirname);
mdMailer.sendSMTP(todayCache, __dirname);
//
saveToDB(todayCache);
saveData();
}, 45000);

282
todayV3.js Normal file
View File

@ -0,0 +1,282 @@
const cron = require('node-cron');
const LocalStorage = require('node-localstorage').LocalStorage;
// var nano = require('nano')('http://martind2000:1V3D4m526i@localhost:5984');
const logger = require('log4js').getLogger();
const prettyMs = require('pretty-ms');
const Sugar = require('sugar');
const calHandler = require('./lib/calHandler');
const swedishWord = require('./lib/swedishword');
const weather = require('./lib/weather');
const trains = require('./lib/trains');
const history = require('./lib/history');
const mdMailer = require('./lib/mailer');
// const mdFitbit = require('./lib/fitbit');
const todayFTSE = require('./lib/todayftse');
const quotes = require('./lib/quotes');
const euronews = require('./lib/euronews');
// var db_name = 'silvrgit';
// var dbCloudant = nano.use(db_name);
const memwatch = require('memwatch-next');
memwatch.on('leak', (info) => {
console.error('Memory leak detected:\n', info);
});
localStorage = new LocalStorage('./scratch');
logger.level = 'trace';
const credentials = {
'username': '25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix',
'password': '8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c',
'host': '25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com',
'port': 443,
'url': 'https://25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix:8e417af1b0462ca55726848846cc6b8696fc76defe9d1864cbc334be59549e0c@25f854ee-1b51-49ff-acd9-5b0ff478d944-bluemix.cloudant.com',
'database': 'today'
};
const Cloudant = require('cloudant');
const cloudant = Cloudant({ 'account': credentials.username, 'password': credentials.password });
const dbCloudant = cloudant.db.use(credentials.database);
String.prototype.hashCode = function () {
if (Array.prototype.reduce)
return this.split('').reduce(function (a, b) {
a = ((a << 5) - a) + b.charCodeAt(0);
return a & a;
}, 0);
else {
let hash = 0, i, chr, len;
if (this.length === 0) return hash;
for (i = 0, len = this.length; i < len; i++) {
chr = this.charCodeAt(i);
hash = ((hash << 5) - hash) + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
};
const todayCacheSrc = {
'last': 0, 'data': {
'trains': { 'last': 0, 'data': [] },
'weather': {},
'history': [],
'today': '',
'tv': { 'entries': [] },
'cal': { 'today': [], 'tomorrow': [], 'week': [] },
'swedish': {},
'fitbit': {},
'ftse': {}
}, 'expire': ((60 * 1000) * 60)
};
let todayCache = {
'last': 0, 'data': {
'trains': { 'last': 0, 'data': [] },
'weather': {},
'history': [],
'today': '',
'tv': { 'entries': [] },
'cal': { 'today': [], 'tomorrow': [], 'week': [] },
'swedish': {},
'fitbit': {},
'ftse': {}
}, 'expire': ((60 * 1000) * 60)
};
function saveLastRunTime() {
const now = new Date();
logger.info('Logging last run...');
localStorage.setItem('lastRun', now.getTime());
}
function saveToday() {
logger.info('Saving today...');
localStorage.setItem('today', JSON.stringify(todayCache));
}
function runable() {
try {
const lastRun = localStorage.getItem('lastRun');
logger.debug('lastrun', lastRun);
const now = new Date().getTime();
const timeDiff = now - lastRun;
logger.info('last updated', prettyMs(timeDiff));
if (now - lastRun < 3600000)
return false;
else {
todayCache.last = now;
return true;
}
}
catch (e) {
logger.debug('Creating new today object.. Lets go!');
todayCache = Object.assign({}, todayCacheSrc);
todayCache.last = new Date().getTime();
return true;
}
}
async function testNews() {
await euronews.getEuroNews()
.then((d) => {
// todayCache.data.news = d;
// logger.debug(d);
// console.log();
h = euronews.render(d.items);
let html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
${h}
</body>
</html>`;
logger.debug(html);
})
.catch((e) => {
logger.error(e);
});
}
async function preLoadToday() {
function compare(a, b) {
if (a.ts < b.ts)
return -1;
if (a.ts > b.ts)
return 1;
return 0;
}
logger.debug('preload');
todayCache.data.cal = { 'today': [], 'tomorrow': [], 'week': [] };
await weather.newDoGetWeather()
.then((d) => {
todayCache.data.weather = d;
})
.catch((e) => {
logger.error(e);
});
await trains.updateTrains()
.then((d) => {
'use strict';
console.log('Trains: ', d);
todayCache.data.trains.data = d;
todayCache.data.trains.last = new Date();
})
.catch((e) => {
'use strict';
console.error(e);
});
await history.updateHistory()
.then((d) => {
'use strict';
console.log('History result: ', d);
todayCache.data.history = d;
})
.catch((e) => {
'use strict';
console.error(e);
});
await calHandler.getSimpleCalV3(
'http://www.pogdesign.co.uk/cat/download_ics/60cfdff469d0490545d33d7e3b5c0bcc')
.then((d) => {
'use strict';
todayCache.data.tv = d;
})
.catch((e) => {
'use strict';
logger.error(e);
});
await todayFTSE.getFTSE()
.then((d) => {
todayCache.data.ftse = d;
})
.catch((e) => {
logger.error(e);
});
await quotes.GetQuotes()
.then((d) => {
todayCache.data.quotes = d;
})
.catch((e) => {
logger.error(e);
});
for (const item of calHandler.calendars)
await calHandler.getAdvancedCalV3(item)
.then((d) => {
todayCache.data.cal.today = todayCache.data.cal.today.concat(d.today);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.concat(d.tomorrow);
todayCache.data.cal.week = todayCache.data.cal.week.concat(d.week);
})
.catch((e) => {
'use strict';
logger.error(e);
});
console.log('>> SORT!!');
todayCache.data.cal.today = todayCache.data.cal.today.sort(compare);
todayCache.data.cal.tomorrow = todayCache.data.cal.tomorrow.sort(compare);
todayCache.data.cal.week = todayCache.data.cal.week.sort(compare);
await swedishWord.getSwedishWord()
.then((d) => {
'use strict';
logger.debug('Got swedish...');
todayCache.data.swedish = d;
})
.catch((e) => {
'use strict';
logger.error(e);
});
await euronews.getEuroNews()
.then((d) => {
todayCache.data.news = d;
})
.catch((e) => {
logger.error(e);
});
await saveToday();
}
// saveLastRunTime();
setTimeout(function () {
// loadData();
if (runable()) {
logger.trace('Runable');
preLoadToday();
// testNews();
}
// Module.exports.preLoadToday();
}, 5000);