Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const fs = require('fs');
- const csvSync = require('csv-parse/lib/sync');
- const firebase = require('firebase');
- const teamIDs = require ('./teamId.json');
- const teamNames = require ('./teamNames.json');
- const file = 'JL_MatchDay_UTF8.csv'
- let data = fs.readFileSync(file);
- let res = csvSync(data);
- function createMatch (array){
- const year = array[0].trim();
- const category = array[1].trim();
- const section = array[2].trim();
- const date = array[3].trim();
- const time = array[4].trim();
- const homeTeam = array[5].trim();
- const score = array[6].trim();
- const awayTeam = array[7].trim();
- const stadium = array[8].trim();
- const visitors = array[9].trim();
- const broadcast = array[10].trim();
- const sectionObject = createSection(section);
- const scoreObject = createScore(score);
- const home = createTeam(homeTeam);
- const away = createTeam(awayTeam);
- const teams = createTeams(home, away);
- const timeObject = createTimestamp(year, date, time);
- const visitorsSum = createVisitors(visitors);
- const matchId = `${year}${date.substr(0, 2)}${date.substr(3, 2)}-${category}${section}-${homeTeam}vs${awayTeam}`;
- const data = {
- time: timeObject,
- category,
- section: sectionObject,
- homeTeam: home,
- awayTeam: away,
- teams,
- score: scoreObject,
- stadium,
- visitors: visitorsSum,
- broadcast,
- };
- const match = {
- id: matchId,
- data,
- }
- return match;
- }
- function createSection (section){
- let sectionObject;
- if (section.length === 3) {
- sectionObject = {
- sectionFull: section,
- section,
- date: section,
- }
- } else {
- sectionObject = {
- sectionFull: section,
- section: section.substr(0, section.length-3),
- date: section.slice(-3),
- }
- }
- return sectionObject;
- }
- function createScore (scoreString){
- if (scoreString === 'None') { return null; }
- const score = {
- homeScore: Number(scoreString.substr(0, 1)),
- awayScore: Number(scoreString.slice(-1)),
- }
- return score;
- }
- function createTeams (home, away){
- const teams = {}
- if (home.id) { teams[home.id] = true; }
- if (away.id) { teams[away.id] = true; }
- return teams;
- }
- function createTeam (teamName){
- const fullname = teamNames[teamName];
- const id = teamIDs[fullname];
- const team = {
- name: teamName,
- fullname,
- id,
- }
- return team;
- }
- function createTimestamp (yearString, dateString, timeString){
- // const year = Number(yearString)
- // const month = Number(dateString.substr(0, 2) - 1)
- // const date = Number(dateString.substr(2, 2))
- const hour = Number(timeString.substr(0, 2))
- const minute = Number(timeString.substr(2, 2))
- const time = `${yearString}/${dateString.substr(0, 5)} ${timeString}`;
- const PSTTimestamp = new Date(time);
- const JSTTimestamp = new Date(PSTTimestamp.getTime() - 1000*60*60*16);
- const timeObject = {
- raw: {
- year: yearString,
- date: dateString,
- time: timeString,
- },
- timestamp: JSTTimestamp.getTime(),
- // time: JSTTimestamp,
- }
- return timeObject;
- }
- function createVisitors (visitors){
- if (visitors === 'None') {
- return null;
- }
- return Number(visitors);
- }
- var config = {
- apiKey: "",
- authDomain: "",
- databaseURL: "",
- projectId: "",
- storageBucket: "",
- messagingSenderId: ""
- };
- firebase.initializeApp(config);
- const db = firebase.firestore();
- for (var i = 1; i < res.length; i++) {
- const array = res[i];
- // };
- // res.forEach((array) => {
- const match = createMatch(array);
- const schedule = createScheduleId(match.data.time.timestamp);
- // console.log(match.id, match.data, match.data.time.timestamp);
- // console.log(match.id, schedule.id, schedule.timestamp);
- const matchRef = db.collection('matches').doc(match.id);
- matchRef.set(match.data)
- .then(function() {
- const scheduleRef = db.collection('schedules').doc(schedule.id);
- scheduleRef.set({
- timestamp: schedule.timestamp,
- leagues: { [`${match.data.category}`]: true },
- matches: { [`${match.id}`]: true },
- }, { merge: true });
- })
- .catch(function(error) {
- console.error("Error writing document: ", error);
- });
- // });
- };
- // for (var i = 1; i < res.length; i++) {
- // const array = res[i];
- // // };
- // // res.forEach((array) => {
- // const match = createMatch(array);
- // const schedule = createScheduleId(match.data.time.timestamp);
- // // console.log(match.id, match.data, match.data.time.timestamp);
- // // console.log(match.id, schedule.id, schedule.timestamp);
- //
- // const scheduleRef = db.collection('schedules').doc(schedule.id);
- // scheduleRef.set({
- // // [`leagues.${match.data.category}`]: true,
- // leagues: { [`${match.data.category}`]: true },
- // matches: { [`${match.id}`]: true },
- // }, { merge: true });
- // };
- function createScheduleId (timestamp){
- const time = new Date(timestamp - 1000*60*60*8);
- // const time = new Date(timestamp);
- const JSTTime = new Date(timestamp + 1000*60*60*9);
- const year = JSTTime.getFullYear().toString ();
- let month = (JSTTime.getMonth() + 1).toString ();
- let date = JSTTime.getDate().toString ();
- const timezone = 'JST';
- if (month.length === 1 ) {
- month = `0${month}`;
- }
- if (date.length === 1 ) {
- date = `0${date}`;
- }
- const scheduleDatestamp = new Date(time.getFullYear(), time.getMonth(), time.getDate());
- const scheduleId = `${year}${month}${date}${timezone}`;
- const schedule = {
- id: scheduleId,
- // date: scheduleDatestamp,
- timestamp: scheduleDatestamp.getTime(),
- }
- return schedule;
- }
Add Comment
Please, Sign In to add comment