-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
152 lines (142 loc) · 4.61 KB
/
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
from fastapi import FastAPI
from pydantic import BaseModel
import helpers.helper as helper
description = """
AmpBlock is an API that extracts AMP links from messages and retrieves each of their canonical URLs.
## Where's the documentation of the response data?
The response data model mirrors the response data model specified in the official documentation of [AmputatorBot](https://github.com/KilledMufasa/AmputatorBot) at the time of writing.
### Example response:
```json
[
{
"amp_canonical": {
"domain": "ampproject",
"is_alt": false,
"is_amp": true,
"is_cached": true,
"is_valid": true,
"type": "CANURL",
"url": "https://electrek-co.cdn.ampproject.org/c/s/electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/amp/?usqp=mq331AQIKAGwASCAAgM%3D",
"url_similarity": 0.7480314960629921
},
"canonical": {
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
},
"canonicals": [
{
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
},
{
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
}
],
"origin": {
"domain": "google",
"is_amp": true,
"is_cached": true,
"is_valid": true,
"url": "https://www.google.com/amp/s/electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/amp/"
}
},
{
"amp_canonical": {
"domain": "ampproject",
"is_alt": false,
"is_amp": true,
"is_cached": true,
"is_valid": true,
"type": "GOOGLE_JS_REDIRECT",
"url": "https://electrek-co.cdn.ampproject.org/c/s/electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/amp/?usqp=mq331AQIKAGwASCAAgM%3D",
"url_similarity": 0.7480314960629921
},
"canonical": {
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
},
"canonicals": [
{
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
},
{
"domain": "electrek",
"is_alt": false,
"is_amp": false,
"is_cached": null,
"is_valid": true,
"type": "REL",
"url": "https://electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/",
"url_similarity": 0.8900523560209425
}
],
"origin": {
"domain": "google",
"is_amp": true,
"is_cached": true,
"is_valid": true,
"url": "https://www.google.com/amp/s/electrek.co/2018/06/19/tesla-model-3-assembly-line-inside-tent-elon-musk/amp/"
}
}
]
```
This would be the final iteration of this API. For updated code, refer to [AmputatorBot](https://github.com/KilledMufasa/AmputatorBot) which is the project in which AmpBlock is based on.
[Link to repository](https://github.com/tropicbliss/AmpBlock)
"""
app = FastAPI(
title="AmpBlock",
description=description,
version="0.1.0",
contact={
"name": "tropicbliss",
"url": "https://www.tropicbliss.net/",
"email": "tropicbliss@protonmail.com"
},
license_info={
"name": "GNU GPL v3.0",
"url": "https://www.gnu.org/licenses/gpl-3.0-standalone.html",
},
)
class Msg(BaseModel):
url: str
@app.post("/")
async def root(inp: Msg):
body = inp.url
if helper.check_if_amp(body):
urls = helper.get_urls(body)
links = await helper.get_urls_info(urls)
if any(link.canonical for link in links) or any(link.amp_canonical for link in links):
return [link.__dict__ for link in links]
return None