-
Notifications
You must be signed in to change notification settings - Fork 0
/
hard-code.js
126 lines (95 loc) · 3.35 KB
/
hard-code.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
126
const crypto = require('crypto');
const axios = require('axios');
require('firebase/firestore');
const firebase = require('firebase/app');
const geo = require('./src/static/json/geography-map.json');
firebase.initializeApp({
apiKey: "AIzaSyCQc4tLTpdJOrUzXwqq04rXJZYvfkrWMm8",
authDomain: "sunny-box.firebaseapp.com",
databaseURL: "https://sunny-box.firebaseio.com",
projectId: "sunny-box",
storageBucket: "sunny-box.appspot.com",
messagingSenderId: "560323853096"
});
const firestore = firebase.firestore();
firestore.settings({timestampsInSnapshots: true});
const TOKEN = '2227bc9dbeeea2063129a72a3d'; // 146861067c97180b6e3fc79f34 - 2227bc9dbeeea2063129a72a3d
function data(from, to) {
const chunk = geo.objects.countries1.geometries.slice(from, to);
const times = to - from;
const countriesList = chunk.map(({properties}) => properties['Alpha-2']);
doRequest(times, countriesList)
}
function doRequest(times, countriesList) {
if (times < 0) return;
setTimeout(() => {
const countryCode = countriesList[times];
console.log(`Do request # ${times} country ${countryCode}`);
countryCode && getData(countryCode, 5);
doRequest(times - 1, countriesList);
}, 7000);
}
function getData(countryCode, pages) {
const country = geo.objects.countries1.geometries
.find(({properties}) => properties['Alpha-2'] === countryCode);
if (!country) {
console.log('hmm... country name not found');
return
}
const url = page => `http://api.dirble.com/v2/countries/${countryCode.toLowerCase()}/stations/?token=${TOKEN}&per_page=30&page=${page}`;
const api = Array.from(Array(pages)).map((x, i) => axios.get(url(i + 1)));
axios.all(api)
.then(axios.spread((...responses) => merge(responses)))
.then(data => createOurData(data))
.then(data => sendToFirestore(country.properties.name, data))
.catch(error => console.error('api', error));
}
function merge(responses) {
const merged = [];
responses.forEach(({data}) => merged.push(...data));
return merged;
}
function createOurData(stations) {
return stations
.map(s => ({
country: s.country,
genre: s.categories[0].slug,
image: s.image.thumb.url,
name: s.name,
src: s.streams.map(({stream}) => stream),
uid: s.id,
website: s.website,
}))
.reduce((acc, next) => {
acc[next.genre] = acc[next.genre]
? [...acc[next.genre], next]
: [next];
return acc
}, {});
}
function sendToFirestore(country, data) {
firestore.doc(`stations/${country}`).set(data, {merge: true})
.then(() => console.log('should be good 🎈 😈'))
.catch(error => console.error('Firebase:', error));
}
// data(160, 180); // max 177
const addStation = () => {
const country = 'Ukraine';
const station = {
country: 'UA',
genre: 'rock',
image: 'https://www.radioroks.ua/static/img/content/articles/269/s.jpg',
name: 'ROKS',
src: ['http://online-radioroks.tavrmedia.ua/RadioROKS'],
uid: crypto.randomBytes(16).toString('hex'),
website: 'https://www.radioroks.ua',
};
firestore.doc(`stations/${country}`).get()
.then(doc => {
const prevStations = doc.data()[station.genre];
const stationsByGenre = {[station.genre]: [...prevStations, station]};
sendToFirestore(country, stationsByGenre)
})
.catch(error => console.error('add-station', error));
};
addStation();