0
0
mirror of https://github.com/yude-jp/yude.jp synced 2024-12-22 20:20:09 +09:00

Compare commits

..

No commits in common. "c856a34867c3d6b57b2284ea2ad88286b40d6758" and "6eb140fc0a6a28892bf37452c361c7b3b1bba61c" have entirely different histories.

13 changed files with 254 additions and 8 deletions

View File

@ -1,6 +1,3 @@
{
"extends": "next/core-web-vitals",
"rules": {
"@next/next/no-document-import-in-page": "off"
}
"extends": "next/core-web-vitals"
}

2
.gitignore vendored
View File

@ -11,5 +11,5 @@ yarn-error.log
.vimrc~
..vimrc.un~
.env.local
out
.vercel

View File

@ -0,0 +1,44 @@
// React
import { format } from 'date-fns'
const {
FITBIT_TOKEN: bearer
} = process.env;
const today = format(new Date(), 'yyyy-MM-dd')
export const getName = async (props) => {
const uuid = props;
return fetch(
'https://api.fitbit.com/1/user/-/activities/heart/date/today/1d/1sec/time/00:00/23:59.json',
{
headers: {
Authorization: `Bearer ${bearer}`,
},
}
);
};
const FitbitHeartrate = async (req, res) => {
const { uuid } = req.query
const response = await getName(uuid);
const data = await response.json();
if (response.status === 204 || response.status > 400) {
return res.status(200).send("404");
}
// const heartrate = data.map((item, i) => {
// if (item.dateTime = today) {
// return item
// } else {
// return "Failed to retrieve data."
// }
// }
// )
const array = data["activities-heart-intraday"].dataset
const heartrate = array[array.length - 1]
return res.status(200).json({
heartrate,
});
};
export default FitbitHeartrate

35
pages/api/Fitbit/Sleep.js Normal file
View File

@ -0,0 +1,35 @@
// React
import { format } from 'date-fns'
const {
FITBIT_TOKEN: bearer
} = process.env;
const today = format(new Date(), 'yyyy-MM-dd')
export const getName = async (props) => {
const uuid = props;
return fetch(
'https://api.fitbit.com/1.2/user/-/sleep/date/' + today + '.json',
{
headers: {
Authorization: `Bearer ${bearer}`,
},
}
);
};
const FitbitSleep = async (req, res) => {
const { uuid } = req.query
const response = await getName(uuid);
const data = await response.json();
if (response.status === 204 || response.status > 400) {
return res.status(200).send("404");
}
const duration = data.summary.totalMinutesAsleep
return res.status(200).json({
duration,
});
};
export default FitbitSleep

View File

@ -0,0 +1,23 @@
// React
import React, { useState, useEffect } from 'react';
import axios from 'axios';
export const getName = async (props) => {
const uuid = props;
return fetch('https://api.ashcon.app/mojang/v2/user/' + uuid);
};
const RawPlayerName = async (req, res) => {
const { uuid } = req.query
const response = await getName(uuid);
const data = await response.json();
if (response.status === 204 || response.status > 400) {
return res.status(200).send("404");
}
const username = data.username;
return res.status(200).json({
username,
});
};
export default RawPlayerName

60
pages/api/Spotify.js Normal file
View File

@ -0,0 +1,60 @@
import querystring from 'querystring';
const {
SPOTIFY_CLIENT_ID: client_id,
SPOTIFY_CLIENT_SECRET: client_secret,
SPOTIFY_REFRESH_TOKEN: refresh_token,
} = process.env;
const basic = Buffer.from(`${client_id}:${client_secret}`).toString('base64');
const NOW_PLAYING_ENDPOINT = `https://api.spotify.com/v1/me/player/currently-playing`;
const TOKEN_ENDPOINT = `https://accounts.spotify.com/api/token`;
const getAccessToken = async () => {
const response = await fetch(TOKEN_ENDPOINT, {
method: 'POST',
headers: {
Authorization: `Basic ${basic}`,
'Content-Type': 'application/x-www-form-urlencoded',
},
body: querystring.stringify({
grant_type: 'refresh_token',
refresh_token,
}),
});
return response.json();
};
export const getNowPlaying = async () => {
const { access_token } = await getAccessToken();
return fetch(NOW_PLAYING_ENDPOINT, {
headers: {
Authorization: `Bearer ${access_token}`,
},
});
};
const Spotify = async (_, res) => {
const response = await getNowPlaying();
if (response.status === 204 || response.status > 400) {
return res.status(200).json({ isPlaying: false });
}
const song = await response.json();
const isPlaying = song.is_playing;
const title = song.item.name;
const artist = song.item.artists.map((_artist) => _artist.name).join(', ');
const album = song.item.album.name;
return res.status(200).json({
album,
artist,
isPlaying,
title,
});
};
export default Spotify

View File

@ -0,0 +1,38 @@
// Data fetching
import useSwr from 'swr'
// Font Awesome
import { faHeartbeat } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
// Data fetching implements
const fetcher = (url) => fetch(url).then((res) => res.json())
function App (props) {
const { data, error } = useSwr(
'/api/Fitbit/Heartrate',
fetcher
)
if (error) {
return (
<>エラーが発生しました</>
)
} else {
if (!data) {
return (
<>読み込み中...</>
)
} else {
return (
<>
<FontAwesomeIcon icon={faHeartbeat} className="w-5 h-5 inline ml-3"/>
&nbsp;
{data.heartrate.value}
</>
)
}
}
}
export default App;

View File

@ -0,0 +1,44 @@
// Data fetching
import useSwr from 'swr'
// Font Awesome
import { faBed } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
// Data fetching implements
const fetcher = (url) => fetch(url).then((res) => res.json())
function App (props) {
const { data, error } = useSwr(
'/api/Fitbit/Sleep',
fetcher
)
if (error) {
return (
<>エラーが発生しました</>
)
} else {
if (!data) {
return (
<>読み込み中...</>
)
} else {
const duration = data.duration
const hours = Math.floor(duration / 60)
let minutes = duration % 60
if (minutes <= 9) {
minutes = '0' + minutes
}
return (
<>
<FontAwesomeIcon icon={faBed} className="w-5 h-5 inline"/>
&nbsp;
{hours}:{minutes}
</>
)
}
}
}
export default App;

View File

@ -9,7 +9,7 @@ const fetcher = (url) => fetch(url).then((res) => res.json())
function App (props) {
const uuid = props;
const { data, error } = useSwr(
uuid.uuid ? `https://api.ashcon.app/mojang/v2/user/${uuid.uuid}` : null,
uuid.uuid ? `/api/PlayerName/${uuid.uuid}` : null,
fetcher
)

View File

@ -11,7 +11,7 @@ function App () {
useEffect(() => {
const fetchData = async () => {
const result = await axios(
'https://vercel-spotify-api.vercel.app/api/Spotify',
'/api/Spotify',
);
setData(result.data);
};

View File

@ -5,6 +5,7 @@
import { useRouter } from 'next/router'
// Data fetching
import Players from '../../components/Minecraft/Players'
import useSwr from 'swr'
const fetcher = (url) => fetch(url).then((res) => res.json())

View File

@ -16,7 +16,7 @@ export default function UUID() {
const router = useRouter()
const { uuid } = router.query
const { data, error } = useSwr(
uuid ? `https://api.ashcon.app/mojang/v2/user/${uuid}` : null,
uuid ? `/api/PlayerName/${uuid}` : null,
fetcher
)
if (error) {

View File

@ -25,6 +25,8 @@ import PublicKeys from './components/Profile/PublicKeys'
import Button from './components/Profile/Button'
import Contact from './components/Profile/Contact'
import NintendoSW from "./components/Profile/NintendoSW"
import FitbitSleep from "./components/Fitbit/Sleep"
import FitbitHeartrate from "./components/Fitbit/Heartrate"
// next-seo
import { NextSeo } from 'next-seo';
@ -61,6 +63,8 @@ export default function Profile(props) {
<div>
<DiscordPlaying />
<Spotify />
{/* <FitbitSleep />
<FitbitHeartrate /> */}
</div>
</div>