-
Notifications
You must be signed in to change notification settings - Fork 1
/
officer.js
126 lines (113 loc) · 3.15 KB
/
officer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var topics = require("./.topics");
var fs = require("fs");
/*
Global variables
*/
var log_dir = "./log";
var tweet_file = "./log/tweet.log";
var soccer_file = "./log/soccer.log";
//===================================================================================
/*
---- Asynchronous -----
Test whether or not the given path exists by checking with the file system.
return true if the file exists, false otherwise
*/
var file_exists = function(path){
return fs.existsSync(path);
}
/*
---- Only use it for files with JSON content ----
Synchronously reads from file in the given path and returns an object,
if the file encoding is different from utf8 you need to specify it
*/
var read_json = function(path, encoding){
if(encoding){
return JSON.parse(fs.readFileSync(path, {encoding : encoding}));
}else{
return JSON.parse(fs.readFileSync(path, {encoding : "utf8"}));
}
}
/*
----- Only use it to save objects ----
Synchronously saves the given object in the given path, the default encoding used is utf8
returns undefined
*/
var write_obj = function(path, obj, encoding){
if(encoding){
fs.writeFileSync(path, JSON.stringify(obj), {encoding : encoding});
}else{
fs.writeFileSync(path, JSON.stringify(obj), {encoding : 'utf8'});
}
}
/*
---- Synchronous-----
Creates a new directory
*/
var mkdir = function(path){
fs.mkdirSync(path);
}
//===================================================================================
module.exports = exports = {
/*
verify if the topic is valid
*/
validate_topic : function(topic) {
var arr = topic.split("/");
if(arr[0] == ""){arr.splice(0,1);}
if(arr[arr.length-1] == ""){arr.splice(arr.length-1, 1);}
var index = topics;
for(var i in arr){
if(index.hasOwnProperty(arr[i])){
index = index[arr[i]];
continue;
}else{
return false;
}
}
return true;
},
/*
read the last saved tweets and return the object
if the file doesn't exist then returns undefined
*/
read_last_tweet : function(){
if(file_exists(log_dir)){
if(file_exists(tweet_file)){
return read_json(tweet_file);
}else{
return undefined;
}
}else{
mkdir(log_dir);
return undefined;
}
},
/*
saves the last treated tweet
*/
write_last_tweet : function(last_tweet){
write_obj(tweet_file, last_tweet);
},
/*
reads the last treated fixtures
return the object if the file exists, undefined otherwise
*/
read_last_soccer_teams_fixture : function(){
if(file_exists(log_dir)){
if(file_exists(soccer_file)){
return read_json(soccer_file);
}else{
return undefined;
}
}else{
mkdir(log_dir);
return undefined;
}
},
/*
saves the last treated fixtures
*/
write_last_soccer_teams_fixture : function(last_fixtures){
write_obj(soccer_file, last_fixtures);
}
};