· 7 years ago · Jan 02, 2019, 09:38 PM
1import sqlite3
2import re
3import urllib.request
4import sys
5
6#=====Tworzenie Tabeli======#
7steam_database = sqlite3.connect('steam_baza.db')
8steam_database.row_factory = sqlite3.Row
9cur = steam_database.cursor()
10steam_database.executescript('''
11 DROP TABLE IF EXISTS STEAM;
12 CREATE TABLE IF NOT EXISTS STEAM(
13 ID INT PRIMARY KEY ASC,
14 NAZWA CHAR(100) NOT NULL,
15 TERAZ CHAR(50) NOT NULL,
16 NAJW CHAR(50) NOT NULL
17 )''')
18#steam_database.close()
19#=====Tworzenie Tabeli======#
20
21#=====Wybor Strony======#
22url = "https://store.steampowered.com/stats/?l=polish"
23
24def doRequest(url):
25 return urllib.request.urlopen(url).read().decode()
26#=====Wybor Strony======#
27
28#=====Tablice=====#
29NazwaGry = []
30LiczbaTeraz = []
31NajwiekszaLiczba = []
32#=====Tablice=====#
33
34#=====Nazwa Gry=====#
35def nazwaGry(NazwaGry):
36 response = doRequest(url)
37 pattern = re.compile(r'<a class="gameLink"\s+.+>.+</a>')
38 temp = pattern.findall(response)
39
40 pattern2 = re.compile(r'href="https://store.steampowered.com/app/.+">.+</a>')
41
42 for i in range(0, 20):
43 nazwa = pattern2.findall(temp[i])
44 nazwa = re.sub('href="https://store.steampowered.com/app/.+">','',nazwa[0])
45 nazwa = re.sub('</a>', '', nazwa)
46 NazwaGry.append(nazwa)
47
48 #print(NazwaGry)
49 return NazwaGry
50#=====Nazwa Gry=====#
51
52#=====Gracze=====#
53def liczbaGraczy(LiczbaTeraz, NajwiekszaLiczba):
54
55 listaTemp = []
56
57 response = doRequest(url)
58 pattern = re.compile(r'<span class="currentServers">.+</span>')
59 temp = pattern.findall(response)
60 pattern2 = re.compile(r'<span class="currentServers">.+</span>')
61
62 for i in range(0, 40):
63 liczba = pattern2.findall(temp[i])
64 liczba = re.sub('<span class="currentServers">','',liczba[0])
65 liczba = re.sub('</span>', '', liczba)
66 listaTemp.append(liczba)
67
68 x = 0
69 while x < len(listaTemp):
70 LiczbaTeraz.append(listaTemp[x])
71 x += 1
72 NajwiekszaLiczba.append(listaTemp[x])
73 x += 1
74
75 #print(LiczbaTeraz)
76 #print(NajwiekszaLiczba)
77
78 return LiczbaTeraz, NajwiekszaLiczba
79#=====Gracze=====#
80
81#=====Baza Funkcjonalność=====#
82def dane():
83 #steam_database = sqlite3.connect('steam_baza.db')
84 for i in range(0, 20):
85 steam_database.execute("""INSERT INTO STEAM(ID, NAZWA, TERAZ, NAJW) VALUES (NULL,?,?,?)""", (NazwaGry[i], LiczbaTeraz[i], NajwiekszaLiczba[i]))
86 steam_database.commit()
87 #steam_database.close()
88
89def wyswietl():
90 #steam_database = sqlite3.connect('steam_baza.db')
91 cur.execute("""SELECT * FROM STEAM""")
92
93 print("NAZWA GRY, \t OBECNI ONLINE, \t NAJWIECEJ ONLINE")
94 for i in cur:
95 print(i[0], i[1], "\t", i[2], "\t", i[3])
96 #steam_database.close()
97
98def dodajdane():
99 #steam_database = sqlite3.connect('steam_baza.db')
100 cur.execute("""SELECT TERAZ NAJW FROM STEAM WHERE NAZWA=?""",(input("Podaj NazwÄ… Gry: "),))
101 cur.fetchall()
102
103 for i in cur:
104 print(i[0], "\t", i[1])
105 #steam_database.commit()
106 #steam_database.close()
107
108
109
110def main():
111
112 #nazwaGry(NazwaGry)
113 #liczbaGraczy(LiczbaTeraz, NajwiekszaLiczba)
114
115 nazwaGry(NazwaGry)
116 liczbaGraczy(LiczbaTeraz, NajwiekszaLiczba)
117 dane()
118
119 menu = 1
120
121 while (menu):
122 print("1.Wyswietl Dane w Tabeli")
123 print("2.Dodaj Grę Ręcznie")
124 print("0.Wyjście")
125
126 wybor = int(input("co wybierasz? "))
127
128 if wybor == 1:
129 wyswietl()
130 main()
131 elif wybor == 2:
132 dodajdane()
133 main()
134 elif wybor == 0:
135 menu = 0
136 sys.exit()
137 else:
138 print("blad")
139
140
141
142if __name__ == '__main__':
143 main()
144
145steam_database.close()