repl.it
@mat1/

repl talk comments

Python

No description

fork
loading
Files
  • main.py
  • templates
  • website
  • getpostlang.py
  • poetry.lock
  • pyproject.toml
  • repltalk.py
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from aiohttp import web
from jinja2 import Environment, FileSystemLoader, select_autoescape
import repltalk
from getpostlang import get_post_language
from datetime import datetime

jinja_env = Environment(
	loader=FileSystemLoader(searchpath='templates'),
	autoescape=select_autoescape(['html', 'xml']),
	enable_async=True
)

client = repltalk.Client()

class templates:
	template_dict = {}

async def load_template(filename, **kwargs):
	if filename in templates.template_dict:
		r =  await templates.template_dict[filename].render_async(**kwargs)
	else:
		print(f'Loading template {filename} for the first time')
		t = jinja_env.get_template(filename)
		templates.template_dict[filename] = t
		r = await t.render_async(**kwargs)
	return r

routes = web.RouteTableDef()

answerfinder_cache = {}


@routes.get('/api/post')
async def get_post_data(request):
	post_id = request.query.get('id')
	p = await client.get_post(post_id)
	resp = {
		'id': p.id,
		'title': p.title,
		'content': p.content,
		'url': p.url
	}
	return web.json_response(resp)


@routes.get('/api/posts')
async def get_posts(request):
	after_query = request.query.get('after')

	show_announcements = request.query.get('show_announcements', True)
	show_pinned = request.query.get('show_pinned', True)

	after_query = request.query.get('after')

	posts = await client.boards.ask.get_posts(sort='new', after=after_query)
	resp = {
		'after': posts.after
	}
	posts_list = []
	for p in posts:
		# if not p.can_answer:
		# 	continue
		if not show_announcements and p.is_announcement:
			continue
		if not show_pinned and p.pinned:
			continue
		# if p.language is not None:
		# 	post_lang = {
		# 		'name': p.language.name,
		# 		'display': p.language.display_name
		# 	}
		# else:
		# 	post_lang = None
		epoch = datetime(1970,1,1)
		if p.language is not None:
			icon = p.language.icon
		else:
			icon = None
		posts_list.append({
			'id': p.id,
			'title': p.title,
			'content': p.content,
			'answered': p.answered,
			'upvotes': p.votes,
			'time': (p.datetime-epoch).total_seconds(),
			'comments': p.comment_count,
			'language': get_post_language(p),
			'icon': icon
		})
	resp['posts'] = posts_list
	return web.json_response(resp)

@routes.get('/api/comments')
async def get_comments(request):
	after_query = request.query.get('after')

	show_pinned = request.query.get('show_pinned', True)

	after_query = request.query.get('after')

	comments = await client.boards.ask.get_posts(sort='new', after=after_query)
	resp = {
		'after': posts.after
	}
	posts_list = []
	for c in comments:
		# if not p.can_answer:
		# 	continue
		if not show_announcements and p.is_announcement:
			continue
		if not show_pinned and p.pinned:
			continue
		# if p.language is not None:
		# 	post_lang = {
		# 		'name': p.language.name,
		# 		'display': p.language.display_name
		# 	}
		# else:
		# 	post_lang = None
		epoch = datetime(1970,1,1)
		if p.language is not None:
			icon = p.language.icon
		else:
			icon = None
		posts_list.append({
			'id': p.id,
			'title': p.title,
			'content': p.content,
			'answered': p.answered,
			'upvotes': p.votes,
			'time': (p.datetime-epoch).total_seconds(),
			'comments': p.comment_count,
			'language': get_post_language(p),
			'icon': icon
		})
	resp['posts'] = posts_list
	return web.json_response(resp)



@routes.get('/')
async def index(request):
	return await load_template('index.html')

@routes.get('/post')
async def post(request):
	return await load_template('post.html')

@web.middleware
async def middleware(request, handler):
	r = await handler(request)
	if isinstance(r, str):
		r = web.Response(
			text=r,
			headers={
				'content-type': 'text/html'
			}
		)
	content_types = { # todo: add more things to this	
		'css': 'text/css',
		'js': 'text/javascript',
		'html': 'text/html'
	}
	if r.content_type == 'application/octet-stream':
		parts = request.url.parts
		last_path = parts[-1]
		ext = last_path.rsplit('.', 1)[-1]
		if ext in content_types:
			r.content_type = content_types[ext]
		else:
			r.content_type = 'text/plain'
	return r

app = web.Application(middlewares=[middleware])
app.add_routes(routes)
app.add_routes([web.static('/', 'website')])
web.run_app(app)