Skip to content

Commit def3181

Browse files
authored
Add files via upload
1 parent 090ba35 commit def3181

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

api/index.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: UTF-8 -*-
2+
import requests
3+
import re
4+
import os
5+
import json
6+
from http.server import BaseHTTPRequestHandler, HTTPServer
7+
8+
def get_data(neo_type, neo_category):
9+
if neo_type not in ['wishlist', 'progress', 'complete']:
10+
raise ValueError('Invalid type parameter. Must be wishlist, progress, or complete')
11+
if neo_category not in ['book', 'movie', 'tv', 'music', 'game', 'podcast']:
12+
raise ValueError('Invalid category parameter. Must be book, movie, tv, music, game, or podcast')
13+
14+
url = f'https://neodb.social/api/me/shelf/{neo_type}?category={neo_category}'
15+
headers = {'Authorization': 'Bearer ' + os.environ.get('AUTHORIZATION'), 'Accept': 'application/json'}
16+
response = requests.get(url, headers=headers)
17+
json_data = json.loads(response.text)
18+
pages_value = json_data['pages']
19+
20+
all_results = []
21+
for page_num in range(1, pages_value+1):
22+
requests_path = f'{url}&page={page_num}'
23+
r = requests.get(requests_path, headers=headers)
24+
result = r.json()
25+
all_results.extend(result['data'])
26+
27+
return all_results
28+
29+
class Handler(BaseHTTPRequestHandler):
30+
def do_GET(self):
31+
path = self.path
32+
neo_type = re.findall(r'type=([^&]*)', path)[0]
33+
neo_category = re.findall(r'category=([^&]*)', path)[0]
34+
try:
35+
data = get_data(neo_type, neo_category)
36+
except Exception as e:
37+
self.send_error(500, str(e))
38+
return
39+
40+
self.send_response(200)
41+
self.send_header('Access-Control-Allow-Origin', '*')
42+
self.send_header('Content-type', 'application/json; charset=utf-8')
43+
self.end_headers()
44+
self.wfile.write(json.dumps(data, ensure_ascii=False).encode('utf-8'))
45+
return
46+
47+
# def run(server_class=HTTPServer, handler_class=Handler, port=8080):
48+
# server_address = ('', port)
49+
# httpd = server_class(server_address, handler_class)
50+
# print(f'Starting server on port {port}')
51+
# httpd.serve_forever()
52+
53+
# if __name__ == '__main__':
54+
# run()

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
requests

0 commit comments

Comments
 (0)