-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
235 lines (191 loc) · 7.79 KB
/
api.py
File metadata and controls
235 lines (191 loc) · 7.79 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from fastapi import FastAPI
import sqlite3
import uvicorn
from sqlite3 import connect
from bs4 import BeautifulSoup
import urllib.request
import json
import sys
import pandas as pd
import os
from fastapi.responses import JSONResponse
app = FastAPI()
# Connection a la base sqlite
conn = sqlite3.connect('scrap_game.db')
c = conn.cursor()
# Selection de la base de donnée
# Selection de la collection == SQL Table
@app.get("/games")
async def all_game ():
c.execute("SELECT title, type, release_date,platform,price, publisher FROM games JOIN release_details ON games.id = release_details.game_id JOIN games_details ON games.id = games_details.game_id;")
game = c.fetchall()
conn.commit()
return game
@app.get("/games/count")
async def count_platform():
df = pd.read_sql_query("SELECT platform, COUNT(platform) FROM release_details GROUP BY platform;", conn)
df = df.rename(columns={'COUNT(platform)': 'pourcent', "platform" : "name"})
d = df.to_dict('records')
headers = {"Access-Control-Allow-Origin":"*"}
conn.commit()
return JSONResponse(content=d, headers=headers)
@app.get("/games/type")
async def diferents_types():
df = pd.read_sql_query("SELECT type, COUNT(type) FROM games GROUP BY type;", conn)
df = df.rename(columns={'COUNT(type)': 'nombres', "type" : "genre"})
d = df.to_dict('records')
headers = {"Access-Control-Allow-Origin":"*"}
conn.commit()
return JSONResponse(content=d, headers=headers)
@app.get("/admin2468/start_scrap")
async def start_scrapper():
inc = 0
nomdomaine = 'https://www.gamecash.fr'
urlpage = 'https://www.gamecash.fr/prochaines-sorties.html?o=t&s=a'
links = []
print('Lancement du scraper ...')
os.remove("scrap_game.db")
conn = connect('scrap_game.db')
curs = conn.cursor()
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
def create_table():
curs.execute('''CREATE TABLE IF NOT EXISTS games (
id INT,
title TEXT,
type TEXT,
release_date TEXT,
price TEXT)'''
);
curs.execute('''CREATE TABLE IF NOT EXISTS release_details (
id INTEGER PRIMARY KEY AUTOINCREMENT,
game_id TEXT,
platform TEXT)'''
);
curs.execute('''CREATE TABLE IF NOT EXISTS games_details (
id INTEGER PRIMARY KEY AUTOINCREMENT,
game_id TEXT,
publisher TEXT,
description TEXT)'''
);
conn.commit()
class Jeux :
game_id = 0
titre_precedent = ""
def __init__(self, titre:str, genre:str, prix:str,plateforme:str, date_de_sortie:str,editeur:str,description:str) :
self.titre = titre
self.genre = genre
self.prix = prix
self.plateforme = plateforme
self.date_de_sortie = date_de_sortie
self.editeur = editeur
self.description = description
def insert_values_db(self, titre:str, genre:str, prix:str,plateforme:str, date_de_sortie:str,editeur:str,description:str) :
if Jeux.titre_precedent != self.titre :
Jeux.game_id += 1
curs.execute("INSERT INTO games (id, title, type, price, release_date) VALUES (?, ?, ?, ?, ?);", (Jeux.game_id, self.titre, self.genre, self.prix, self.date_de_sortie))
curs.execute("INSERT INTO games_details (game_id, publisher, description) VALUES (?, ?, ?);", (Jeux.game_id, self.editeur, self.description))
curs.execute("INSERT INTO release_details (game_id, platform) VALUES (?, ?);", (Jeux.game_id, self.plateforme))
conn.commit()
Jeux.titre_precedent = self.titre
def console_logs(self, titre:str, genre:str, prix:str,plateforme:str, date_de_sortie:str,editeur:str,description:str) :
print("Game_ID >",Jeux.game_id,"\nTitre >",self.titre,"\nGenre >",self.genre,"\nPrix >",self.prix,"\nPlateforme >",self.plateforme,"\nDate de sortie >",self.date_de_sortie,"\nEditeur >",self.editeur,"\nDescription >",self.description)
#---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
create_table()
#PARSER RECUPERATION LINKS
page = urllib.request.urlopen(urlpage)
soup = BeautifulSoup(page, 'html.parser')
# On cherche et on append les liens
table = soup.find('table', attrs={'class': 'table'})
results = table.find_all('tr')
for result in results:
data = result.find_all('td')
if len(data) == 0:
continue
data1 = data[1]
a = data1.find('a')
href = a['href']
links.append(nomdomaine + href)
print("Nombre d'URL stockées dans notre liste >",len(links))
for link in links :
#PARSER SUR CHAQUE PAGE
pagelink = urllib.request.urlopen(link)
soup2 = BeautifulSoup(pagelink, 'html.parser')
inc += 1
titre = "Null"
prix = "Null"
plateforme = "Null"
genre = "Null"
editeur = "Null"
date_de_sortie = "Null"
description = "Null"
print("-----------------------------------------------------------------------------------\n>>>>>>> Link",inc,"/",len(links)," >",link,"\n")
#On cherche le prix
scrap_price = soup2.find('meta', attrs={'itemprop': 'price'})
prix = scrap_price['content']
#On cherche le titre
scrap_title = soup2.find('h1', attrs={'itemprop': 'name'})
titre = scrap_title.text
titre = titre.strip()
#On cherche la desc
scrap_desc = soup2.find('div', attrs={'itemprop': 'description'})
try :
description = scrap_desc.text
description = description.strip()
except :
description = "Null"
pass
#On cherche les infos pas importantes (Editeur ...)
table2 = soup2.find_all('ul')
table2_3 = table2[3]
i = -1
for ahah in table2_3 :
i += 1
if len(ahah) == 4 :
sPaN = ahah.find('span', attrs={'class': 'value'})
sPaN = sPaN.text
if i == 0 :
plateforme = sPaN
elif i == 2:
genre = sPaN
elif i == 4 :
editeur = sPaN
elif i == 6 :
if len(sPaN) == 10 :
date_de_sortie = sPaN
elif i == 10 :
if len(sPaN) == 10 :
date_de_sortie = sPaN
elif i == 12 :
date_de_sortie = sPaN
if len(date_de_sortie) != 10 :
date_de_sortie = "Null"
df2 = pd.read_sql_query("SELECT title,release_date FROM games ;", conn)
df2['release_date'] = pd.to_datetime(df2['release_date'], format='%d/%m/%Y', errors='coerce')
jeux_objet = Jeux(titre,genre,prix,plateforme,date_de_sortie,editeur,description)
jeux_objet.insert_values_db(titre,genre,prix,plateforme,date_de_sortie,editeur,description)
jeux_objet.console_logs(titre,genre,prix,plateforme,date_de_sortie,editeur,description)
conn.close()
return {'response' : 'ok'}
# return {"student_id": student_id}
@app.get("/games/count/publisher")
async def get_count_publisher():
df = pd.read_sql_query("SELECT publisher, COUNT(publisher) FROM games_details GROUP BY publisher;", conn)
df = df.rename(columns={'COUNT(publisher)': 'nombre', "publisher" : "name"})
d = df.to_dict('records')
headers = {"Access-Control-Allow-Origin":"*"}
conn.commit()
return JSONResponse(content=d, headers=headers)
# @app.post("/students")
# async def create_student(student: Student):
# student_id = str(COLLECTION_STUDENTS.insert_one(student.dict()).inserted_id)
# # TODO : Envoyer un mail de confirmation
# return {"student_id": student_id}
# @app.put("/students")
# async def update_student():
# return "Not yet implemented"
#@app.delete("/students/id/{id}")
#async def delete_student_by_id(id):
# return {"id": id}
#@app.delete("/students/name/{lastname}")
#async def delete_student_by_name(lastname):
# return {"lastname": lastname}