Advertisement
Guest User

Untitled

a guest
Apr 29th, 2023
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.54 KB | None | 0 0
  1. const html = `<!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  6. <title>查询OpenAI-API密钥信息</title>
  7. <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
  8. </head>
  9. <style>
  10. table th {
  11. width: 30%;
  12. white-space: nowrap;
  13. }
  14. </style>
  15. <body>
  16. <div class="container">
  17. <div class="row justify-content-center">
  18. <div class="col-md-6">
  19. <h1 class="text-center mt-5">查询API密钥信息</h1>
  20. <form class="mt-4">
  21. <div class="form-group mb-3">
  22. <label for="api-key">API密钥:</label>
  23. <input type="text" id="api-key" name="api-key" class="form-control" required autocomplete="off">
  24. </div>
  25. <div class="d-grid">
  26. <input type="submit" value="查询" class="btn btn-primary">
  27. </div>
  28. </form>
  29. <br>
  30. <div id="error-message" class="alert alert-danger d-none"></div>
  31. <br>
  32. <table id="result" class="table table-bordered d-none">
  33. <tbody>
  34. <tr>
  35. <th>账户名称</th>
  36. <td id="account_name"></td>
  37. </tr>
  38. <tr>
  39. <th>是否绑卡</th>
  40. <td id="payment_method_status"></td>
  41. </tr>
  42. <tr>
  43. <th>已消费额度</th>
  44. <td id="used"></td>
  45. </tr>
  46. <tr>
  47. <th>账户额度</th>
  48. <td id="subscription"></td>
  49. </tr>
  50. <tr>
  51. <th>额度上限</th>
  52. <td id="system_hard_limit_usd"></td>
  53. </tr>
  54. </tbody>
  55. </table>
  56. <script>
  57. const form = document.querySelector('form')
  58. const apiKeyInput = document.querySelector('#api-key')
  59. const resultTable = document.querySelector('#result')
  60.  
  61. form.addEventListener('submit', async (event) => {
  62. event.preventDefault()
  63.  
  64. const apiKey = apiKeyInput.value
  65. const response = await fetch('/api?key=' + apiKey)
  66. const data = await response.json()
  67.  
  68. if (data.error) {
  69. const errorMessage = document.querySelector('#error-message')
  70. errorMessage.innerText = data.error.message || '出错了'
  71. errorMessage.classList.remove('d-none')
  72. resultTable.classList.add('d-none')
  73. } else {
  74. const errorMessage = document.querySelector('#error-message')
  75. errorMessage.classList.add('d-none')
  76. document.querySelector('#account_name').innerText = data.account_name
  77. document.querySelector('#payment_method_status').innerText = data.has_payment_method ? '已绑卡' : '未绑卡';
  78. document.querySelector('#system_hard_limit_usd').innerText = data.system_hard_limit_usd
  79. document.querySelector('#used').innerText = data.used
  80. document.querySelector('#subscription').innerText = data.subscription
  81. resultTable.classList.remove('d-none')
  82. }
  83. })
  84.  
  85. </script>
  86. </div>
  87. </div>
  88. </div>
  89. </body>
  90. </html>
  91.  
  92. `
  93.  
  94. addEventListener('fetch', event => {
  95. event.respondWith(handleRequest(event.request))
  96. })
  97.  
  98. async function handleRequest(request) {
  99. const url = new URL(request.url)
  100. if (url.pathname === '/api') {
  101. const apiKey = url.searchParams.get('key')
  102. if (!apiKey) {
  103. return new Response('缺少API密钥', { status: 400 })
  104. }
  105.  
  106. const queryUrl = 'https://api.openai.com/dashboard/billing/subscription'
  107. const headers = {
  108. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36 Edg/112.0.1722.64',
  109. 'Authorization': `Bearer ${apiKey}`,
  110. 'Accept': '*/*',
  111. 'Host': 'api.openai.com',
  112. 'Connection': 'keep-alive'
  113. }
  114.  
  115. try {
  116. const response = await fetch(queryUrl, { headers })
  117. const data = await response.json()
  118. const formatDate = (d) =>
  119. `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d
  120. .getDate()
  121. .toString()
  122. .padStart(2, '0')}`;
  123. const ONE_DAY = 24 * 60 * 60 * 1000;
  124. const now = new Date(Date.now() + ONE_DAY);
  125. const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
  126. const startDate = formatDate(startOfMonth);
  127. const endDate = formatDate(now);
  128.  
  129. const usageResponse = await fetch(`https://api.openai.com/dashboard/billing/usage?start_date=${startDate}&end_date=${endDate}`, { headers })
  130. const usageData = await usageResponse.json()
  131.  
  132. const used = usageData.total_usage ? Math.round(usageData.total_usage) / 100 : 0
  133. const subscription = data.hard_limit_usd ? Math.round(data.hard_limit_usd * 100) / 100 : 0
  134. data.used = used
  135. data.subscription = subscription
  136.  
  137. return new Response(JSON.stringify(data, null, 2), { status: 200 })
  138. } catch (err) {
  139. if (data.error) {
  140. return new Response(JSON.stringify({ error: '出错了' }), { status: 500 })
  141. }
  142.  
  143. }
  144. }
  145.  
  146. return new Response(html, {
  147. headers: {
  148. 'content-type': 'text/html;charset=UTF-8',
  149. },
  150. })
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement