Skip to content

Commit

Permalink
ref
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry committed Aug 14, 2024
1 parent fa3ebc8 commit 5c986bc
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 187 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
charset = utf-8
end_of_line = crlf
indent_style = tab
tab_width = 3
max_line_length = 80
insert_final_newline = true

[*.{json,yml}]
indent_style = space
indent_size = 2
13 changes: 2 additions & 11 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
{
"tabWidth": 3,
"singleQuote": false,
"semi": true,
"overrides": [
{
"files": ["*.yml", "*.json"],
"options": {
"tabWidth": 2
}
}
]
"semi": true,
"singleQuote": false
}
8 changes: 4 additions & 4 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { audioSocketHandler } from "./socket-handlers/audio.js";

export const main = (io) => {
const onConnection = (socket) => {
audioSocketHandler(socket);
};
const onConnection = (socket) => {
audioSocketHandler(socket);
};

io.on("connection", onConnection);
io.on("connection", onConnection);
};
88 changes: 44 additions & 44 deletions src/modules/emulate-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,71 +11,71 @@ let currentTime = 0;
let endingTime = 0;

const updateCurrentTime = (timeDiff) => {
// console.log(currentTime);
currentTime += timeDiff;
// console.log(currentTime);
currentTime += timeDiff;
};

const sleepTick = (duration) => {
const msDuration = duration * 1000;
return new Promise((resolve) => setTimeout(resolve, msDuration));
const msDuration = duration * 1000;
return new Promise((resolve) => setTimeout(resolve, msDuration));
};

const sleep = async (duration) => {
const timeDiff = 1; // seconds
const tickCount = Math.ceil(duration / timeDiff);
const timeDiff = 1; // seconds
const tickCount = Math.ceil(duration / timeDiff);

for (let i = 0; i <= tickCount; i++) {
await sleepTick(timeDiff);
updateCurrentTime(timeDiff);
}
for (let i = 0; i <= tickCount; i++) {
await sleepTick(timeDiff);
updateCurrentTime(timeDiff);
}
};

const requestDuration = async (url) => {
const response = got.stream(url);
const metadata = await parseStream(response);
const duration = Math.floor(metadata.format.duration);
return duration;
const response = got.stream(url);
const metadata = await parseStream(response);
const duration = Math.floor(metadata.format.duration);
return duration;
};

export const getAudioData = () => ({
url,
name,
authorName,
endingTime,
url,
name,
authorName,
endingTime,
});

export const getCurrentTime = () => {
return currentTime;
return currentTime;
};

const emulateStream = async () => {
if (isSong) {
const songData = await getSong();
url = songData.url;
name = songData.name;
authorName = songData.authorName;
currentTime = 0;
// endingTime = 10;
endingTime = await requestDuration(url);
isSong = false;
// console.log("\nRequest song");
} else {
const musicPauseData = await getMusicPause();
url = musicPauseData.url;
name = musicPauseData.name;
authorName = musicPauseData.authorName;
currentTime = 0;
// endingTime = 10;
endingTime = await requestDuration(url);
isSong = true;
// console.log("\nRequest music pause");
}
if (isSong) {
const songData = await getSong();
url = songData.url;
name = songData.name;
authorName = songData.authorName;
currentTime = 0;
// endingTime = 10;
endingTime = await requestDuration(url);
isSong = false;
// console.log("\nRequest song");
} else {
const musicPauseData = await getMusicPause();
url = musicPauseData.url;
name = musicPauseData.name;
authorName = musicPauseData.authorName;
currentTime = 0;
// endingTime = 10;
endingTime = await requestDuration(url);
isSong = true;
// console.log("\nRequest music pause");
}
};

const runEmulateStream = async () => {
await emulateStream();
await sleep(endingTime);
runEmulateStream();
await emulateStream();
await sleep(endingTime);
await runEmulateStream();
};

runEmulateStream();
await runEmulateStream();
6 changes: 3 additions & 3 deletions src/modules/get-audio-data.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs";

export const getAudioData = (filePath) => {
const data = fs.readFileSync(filePath).toString();
const dataJSON = JSON.parse(data);
return dataJSON;
const data = fs.readFileSync(filePath).toString();
const dataJSON = JSON.parse(data);
return dataJSON;
};
10 changes: 5 additions & 5 deletions src/modules/get-music-pause.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getAudioData } from "./get-audio-data.js";

export const getMusicPause = async () => {
const musicPauses = await getAudioData("./audio-data/music-pauses.json");
const musicPauses = await getAudioData("./audio-data/music-pauses.json");

const count = Object.keys(musicPauses).length;
const number = Math.floor(Math.random() * count);
const musicPause = musicPauses[number];
return musicPause;
const count = Object.keys(musicPauses).length;
const number = Math.floor(Math.random() * count);
const musicPause = musicPauses[number];
return musicPause;
};
34 changes: 17 additions & 17 deletions src/modules/get-song.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { getAudioData } from "./get-audio-data.js";

export const getSong = async () => {
const songs = await getAudioData("./audio-data/songs.json");
const songs = await getAudioData("./audio-data/songs.json");

const authors = Object.keys(songs);
const authorsCount = Object.keys(authors).length;
const authorNumber = Math.floor(Math.random() * authorsCount);
const authorName = authors[authorNumber];
const author = songs[authorName];
const authors = Object.keys(songs);
const authorsCount = Object.keys(authors).length;
const authorNumber = Math.floor(Math.random() * authorsCount);
const authorName = authors[authorNumber];
const author = songs[authorName];

const songsCount = author.length;
const songNumber = Math.floor(Math.random() * songsCount);
const song = author[songNumber];
const name = song.name;
const url = song.url;
const songsCount = author.length;
const songNumber = Math.floor(Math.random() * songsCount);
const song = author[songNumber];
const name = song.name;
const url = song.url;

const songData = {
url,
name,
authorName,
};
const songData = {
url,
name,
authorName,
};

return songData;
return songData;
};
12 changes: 6 additions & 6 deletions src/parsers/github-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ dotenv.config();
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;

export const request = async (url) => {
const response = await axios.get(url, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
});
return response.data;
const response = await axios.get(url, {
headers: {
Authorization: `token ${GITHUB_TOKEN}`,
},
});
return response.data;
};
42 changes: 21 additions & 21 deletions src/parsers/music-pauses-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ dotenv.config();
const AUDIO_ARCHIVE = process.env.AUDIO_ARCHIVE;

const parseMusicPauses = async () => {
const audioArchive = await request(AUDIO_ARCHIVE);
let musicPausesData = {};
const audioArchive = await request(AUDIO_ARCHIVE);
let musicPausesData = {};

for (const category of audioArchive) {
if (category.type === "dir" && category.name === "music-pauses") {
const musicPauses = await request(category.url);
for (const musicPause of musicPauses) {
const response = await request(musicPause.url);
const url = response.download_url.replace(/ /g, "%20");
const name = "Музыкальная пауза";
const authorName = "";
const musicPauseNumber = response.name.split(".mp3")[0];
const musicPauseData = {
url,
name,
authorName,
};
musicPausesData[musicPauseNumber] = musicPauseData;
return musicPausesData;
}
}
}
for (const category of audioArchive) {
if (category.type === "dir" && category.name === "music-pauses") {
const musicPauses = await request(category.url);
for (const musicPause of musicPauses) {
const response = await request(musicPause.url);
const url = response.download_url.replace(/ /g, "%20");
const name = "Музыкальная пауза";
const authorName = "";
const musicPauseNumber = response.name.split(".mp3")[0];
const musicPauseData = {
url,
name,
authorName,
};
musicPausesData[musicPauseNumber] = musicPauseData;
return musicPausesData;
}
}
}
};

const musicPauses = await parseMusicPauses();
Expand Down
80 changes: 40 additions & 40 deletions src/parsers/songs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,46 @@ dotenv.config();
const AUDIO_ARCHIVE = process.env.AUDIO_ARCHIVE;

const parseSongs = async () => {
const audioData = {};
const audioArchive = await request(AUDIO_ARCHIVE);

for (const category of audioArchive) {
if (category.type === "dir" && category.name === "songs") {
const authorData = await request(category.url);
for (const author of authorData) {
if (author.type === "dir") {
const playlists = await request(author.url);
let authorName = author.name;

if (authorName.indexOf("&") !== -1) {
const splitIndex = authorName.indexOf("&");
authorName =
authorName.slice(0, splitIndex) +
"/" +
authorName.slice(splitIndex + 1) +
" - сплит";
}

audioData[authorName] = [];

for (const playlist of playlists) {
const songs = await request(playlist.url);

for (const song of songs) {
const songData = {};
const songName = song.name.split(".mp3")[0];
songData.name = songName;
songData.url = song.download_url.replace(/ /g, "%20");
// или заменить blob на raw (вместо song.download_url)
audioData[authorName].push(songData);
}
}
}
}
}
}

return audioData;
const audioData = {};
const audioArchive = await request(AUDIO_ARCHIVE);

for (const category of audioArchive) {
if (category.type === "dir" && category.name === "songs") {
const authorData = await request(category.url);
for (const author of authorData) {
if (author.type === "dir") {
const playlists = await request(author.url);
let authorName = author.name;

if (authorName.indexOf("&") !== -1) {
const splitIndex = authorName.indexOf("&");
authorName =
authorName.slice(0, splitIndex) +
"/" +
authorName.slice(splitIndex + 1) +
" - сплит";
}

audioData[authorName] = [];

for (const playlist of playlists) {
const songs = await request(playlist.url);

for (const song of songs) {
const songData = {};
const songName = song.name.split(".mp3")[0];
songData.name = songName;
songData.url = song.download_url.replace(/ /g, "%20");
// или заменить blob на raw (вместо song.download_url)
audioData[authorName].push(songData);
}
}
}
}
}
}

return audioData;
};

const songs = await parseSongs();
Expand Down
Loading

0 comments on commit 5c986bc

Please sign in to comment.