|
| 1 | +import type { Context } from 'hono'; |
| 2 | +import { env } from '../config/env.js'; |
| 3 | +import getNews from './getNews.js'; |
| 4 | +import generateTanka from './gemini.js'; |
| 5 | +import createPostHandler from '../controllers/Post/createPostHandler.js'; |
| 6 | + |
| 7 | +const printLine = (): void => { |
| 8 | + console.log('--------------------------------'); |
| 9 | +}; |
| 10 | + |
| 11 | +const postNews = async (requestApiKey: string) => { |
| 12 | + if (requestApiKey !== env.NEWS_POST_API_KEY) { |
| 13 | + return { |
| 14 | + isSuccess: false, |
| 15 | + tanka: { |
| 16 | + line0: 'APIキーが間違っています', |
| 17 | + line1: '', |
| 18 | + line2: '', |
| 19 | + line3: '', |
| 20 | + line4: '', |
| 21 | + }, |
| 22 | + }; |
| 23 | + } |
| 24 | + |
| 25 | + const newsResponse = await getNews(); |
| 26 | + |
| 27 | + // ニュースの取得に失敗した場合 |
| 28 | + if (!newsResponse.isSuccess || !newsResponse.news) { |
| 29 | + return { |
| 30 | + isSuccess: false, |
| 31 | + tanka: { |
| 32 | + line0: 'ニュースの取得に失敗しました', |
| 33 | + line1: '', |
| 34 | + line2: '', |
| 35 | + line3: '', |
| 36 | + line4: '', |
| 37 | + }, |
| 38 | + }; |
| 39 | + } |
| 40 | + |
| 41 | + const news = newsResponse.news; |
| 42 | + |
| 43 | + // 投稿の原文 |
| 44 | + const originalText = `${news.title}\n${news.description}\n${news.url}`; |
| 45 | + |
| 46 | + console.log('originalText', originalText); |
| 47 | + |
| 48 | + try { |
| 49 | + const formData = new FormData(); |
| 50 | + formData.append('original', originalText); |
| 51 | + formData.append('user_icon', `https://avatars.githubusercontent.com/u/${env.NEWS_USER_ID}?v=4`); |
| 52 | + formData.append('user_name', '風聞'); |
| 53 | + |
| 54 | + const postResponse = await fetch(`http://localhost:8080/post`, { |
| 55 | + method: 'POST', |
| 56 | + body: formData, |
| 57 | + }); |
| 58 | + |
| 59 | + console.log('postResponse', postResponse); |
| 60 | + |
| 61 | + if (!postResponse.ok) { |
| 62 | + return { |
| 63 | + isSuccess: false, |
| 64 | + tanka: { |
| 65 | + line0: '', |
| 66 | + line1: '', |
| 67 | + line2: '', |
| 68 | + line3: '', |
| 69 | + line4: '', |
| 70 | + }, |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + const json = await postResponse.json(); |
| 75 | + |
| 76 | + return { |
| 77 | + isSuccess: true, |
| 78 | + tanka: json.tanka, |
| 79 | + }; |
| 80 | + } catch (error) { |
| 81 | + console.error(error); |
| 82 | + return { |
| 83 | + isSuccess: false, |
| 84 | + tanka: { |
| 85 | + line0: '投稿に失敗しました', |
| 86 | + line1: '', |
| 87 | + line2: '', |
| 88 | + line3: '', |
| 89 | + line4: '', |
| 90 | + }, |
| 91 | + }; |
| 92 | + } |
| 93 | +}; |
| 94 | + |
| 95 | +export default postNews; |
0 commit comments