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