Guest User

Untitled

a guest
Aug 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.45 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const fs = require('fs');
  4. const csvSync = require('csv-parse/lib/sync');
  5. const firebase = require('firebase');
  6.  
  7. const teamIDs = require ('./teamId.json');
  8. const teamNames = require ('./teamNames.json');
  9.  
  10. const file = 'JL_MatchDay_UTF8.csv'
  11. let data = fs.readFileSync(file);
  12.  
  13. let res = csvSync(data);
  14.  
  15. function createMatch (array){
  16. const year = array[0].trim();
  17. const category = array[1].trim();
  18. const section = array[2].trim();
  19. const date = array[3].trim();
  20. const time = array[4].trim();
  21. const homeTeam = array[5].trim();
  22. const score = array[6].trim();
  23. const awayTeam = array[7].trim();
  24. const stadium = array[8].trim();
  25. const visitors = array[9].trim();
  26. const broadcast = array[10].trim();
  27.  
  28. const sectionObject = createSection(section);
  29. const scoreObject = createScore(score);
  30.  
  31. const home = createTeam(homeTeam);
  32. const away = createTeam(awayTeam);
  33.  
  34. const teams = createTeams(home, away);
  35. const timeObject = createTimestamp(year, date, time);
  36.  
  37. const visitorsSum = createVisitors(visitors);
  38.  
  39. const matchId = `${year}${date.substr(0, 2)}${date.substr(3, 2)}-${category}${section}-${homeTeam}vs${awayTeam}`;
  40. const data = {
  41. time: timeObject,
  42. category,
  43. section: sectionObject,
  44. homeTeam: home,
  45. awayTeam: away,
  46. teams,
  47. score: scoreObject,
  48. stadium,
  49. visitors: visitorsSum,
  50. broadcast,
  51. };
  52. const match = {
  53. id: matchId,
  54. data,
  55. }
  56. return match;
  57. }
  58.  
  59. function createSection (section){
  60. let sectionObject;
  61. if (section.length === 3) {
  62. sectionObject = {
  63. sectionFull: section,
  64. section,
  65. date: section,
  66. }
  67. } else {
  68. sectionObject = {
  69. sectionFull: section,
  70. section: section.substr(0, section.length-3),
  71. date: section.slice(-3),
  72. }
  73. }
  74. return sectionObject;
  75. }
  76.  
  77. function createScore (scoreString){
  78. if (scoreString === 'None') { return null; }
  79.  
  80. const score = {
  81. homeScore: Number(scoreString.substr(0, 1)),
  82. awayScore: Number(scoreString.slice(-1)),
  83. }
  84. return score;
  85. }
  86.  
  87. function createTeams (home, away){
  88. const teams = {}
  89. if (home.id) { teams[home.id] = true; }
  90. if (away.id) { teams[away.id] = true; }
  91. return teams;
  92. }
  93.  
  94. function createTeam (teamName){
  95. const fullname = teamNames[teamName];
  96. const id = teamIDs[fullname];
  97. const team = {
  98. name: teamName,
  99. fullname,
  100. id,
  101. }
  102. return team;
  103. }
  104.  
  105. function createTimestamp (yearString, dateString, timeString){
  106. // const year = Number(yearString)
  107. // const month = Number(dateString.substr(0, 2) - 1)
  108. // const date = Number(dateString.substr(2, 2))
  109. const hour = Number(timeString.substr(0, 2))
  110. const minute = Number(timeString.substr(2, 2))
  111.  
  112. const time = `${yearString}/${dateString.substr(0, 5)} ${timeString}`;
  113.  
  114. const PSTTimestamp = new Date(time);
  115. const JSTTimestamp = new Date(PSTTimestamp.getTime() - 1000*60*60*16);
  116.  
  117. const timeObject = {
  118. raw: {
  119. year: yearString,
  120. date: dateString,
  121. time: timeString,
  122. },
  123. timestamp: JSTTimestamp.getTime(),
  124. // time: JSTTimestamp,
  125. }
  126.  
  127. return timeObject;
  128. }
  129.  
  130. function createVisitors (visitors){
  131. if (visitors === 'None') {
  132. return null;
  133. }
  134. return Number(visitors);
  135. }
  136.  
  137. var config = {
  138. apiKey: "",
  139. authDomain: "",
  140. databaseURL: "",
  141. projectId: "",
  142. storageBucket: "",
  143. messagingSenderId: ""
  144. };
  145. firebase.initializeApp(config);
  146. const db = firebase.firestore();
  147.  
  148. for (var i = 1; i < res.length; i++) {
  149. const array = res[i];
  150. // };
  151. // res.forEach((array) => {
  152. const match = createMatch(array);
  153. const schedule = createScheduleId(match.data.time.timestamp);
  154. // console.log(match.id, match.data, match.data.time.timestamp);
  155. // console.log(match.id, schedule.id, schedule.timestamp);
  156.  
  157. const matchRef = db.collection('matches').doc(match.id);
  158. matchRef.set(match.data)
  159. .then(function() {
  160. const scheduleRef = db.collection('schedules').doc(schedule.id);
  161. scheduleRef.set({
  162. timestamp: schedule.timestamp,
  163. leagues: { [`${match.data.category}`]: true },
  164. matches: { [`${match.id}`]: true },
  165. }, { merge: true });
  166. })
  167. .catch(function(error) {
  168. console.error("Error writing document: ", error);
  169. });
  170. // });
  171. };
  172.  
  173. // for (var i = 1; i < res.length; i++) {
  174. // const array = res[i];
  175. // // };
  176. // // res.forEach((array) => {
  177. // const match = createMatch(array);
  178. // const schedule = createScheduleId(match.data.time.timestamp);
  179. // // console.log(match.id, match.data, match.data.time.timestamp);
  180. // // console.log(match.id, schedule.id, schedule.timestamp);
  181. //
  182. // const scheduleRef = db.collection('schedules').doc(schedule.id);
  183. // scheduleRef.set({
  184. // // [`leagues.${match.data.category}`]: true,
  185. // leagues: { [`${match.data.category}`]: true },
  186. // matches: { [`${match.id}`]: true },
  187. // }, { merge: true });
  188. // };
  189.  
  190. function createScheduleId (timestamp){
  191. const time = new Date(timestamp - 1000*60*60*8);
  192. // const time = new Date(timestamp);
  193. const JSTTime = new Date(timestamp + 1000*60*60*9);
  194. const year = JSTTime.getFullYear().toString ();
  195. let month = (JSTTime.getMonth() + 1).toString ();
  196. let date = JSTTime.getDate().toString ();
  197. const timezone = 'JST';
  198.  
  199. if (month.length === 1 ) {
  200. month = `0${month}`;
  201. }
  202. if (date.length === 1 ) {
  203. date = `0${date}`;
  204. }
  205.  
  206. const scheduleDatestamp = new Date(time.getFullYear(), time.getMonth(), time.getDate());
  207. const scheduleId = `${year}${month}${date}${timezone}`;
  208. const schedule = {
  209. id: scheduleId,
  210. // date: scheduleDatestamp,
  211. timestamp: scheduleDatestamp.getTime(),
  212. }
  213.  
  214. return schedule;
  215. }
Add Comment
Please, Sign In to add comment