· 7 years ago · Jan 26, 2019, 09:40 AM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import sqlite3
5import re
6from urllib.request import Request, urlopen
7URL_Source = "https://www.billboard.com/charts/hot-100"
8
9def doRequest(url):
10 return urllib.request.urlopen(url).read().decode()
11
12def getInfo(n_ID,url):
13
14 req = Request(URL_Source, headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'})
15 webpage = urlopen(req).read().decode()
16 id=n_id
17 # wzorzec dla numeru
18 #data-rank="6" data-artist="Marshmello & Bastille" data-title="Happier" data-has-content="true"
19 #wzorzec = re.compile(r'data-rank="\d{1,3}\" data-artist="[a-zA-Z0-9& !,+'.()]*" data-title="[a-zA-Z0-9& !,+'.()-:]*"')
20 wzorzec = re.compile(r'data-rank="'+n_ID)
21 full_info = wzorzec.findall(webpage)
22 numer = re.sub(r'data-rank="',"Numer: ",full_info[0])
23 print(numer)
24 #print(full_info[0])
25
26 wzorzec = re.compile(r'data-rank="'+n_id+r'\" data-artist="[a-zA-Z0-9& !,+'.()]*')
27 full_info = wzorzec.findall(webpage)
28 artysta = re.sub(r'data-rank="\d{1,3}\" data-artist="',"Artysta: ",full_info[0])
29 print(artysta)
30 #print(full_info)
31
32 wzorzec = re.compile(r'data-rank="'+n_id+r'" data-artist="[a-zA-Z0-9& !,+'.()]*" data-title="[a-zA-Z0-9& !,+'.()-:]*')
33 full_info = wzorzec.findall(webpage)
34 piosenka = re.sub(r'data-rank="'+n_id+r'" data-artist="[a-zA-Z0-9& !,+'.()]*" data-title="',"Piosenka: ",full_info[0])
35 print(piosenka)
36
37 dane=[numer,artysta,piosenka]
38 return dane
39
40moja_baza = sqlite3.connect('test3.db')
41moja_baza.row_factory = sqlite3.Row
42cur = moja_baza.cursor()
43
44print ("Opened database successfully")
45print ("###############################################################")
46print ("")
47
48moja_baza.executescript("""
49 DROP TABLE IF EXISTS topchart;
50 CREATE TABLE IF NOT EXISTS topchart
51 (n_id integer NOT NULL UNIQUE,
52 topchartid VARCHAR(50) NOT NULL);""")
53
54print ("Table created successfully")
55print ("###############################################################")
56print ("")
57
58numer= ((2,'2'), (3,'3'), (4,'4'), (5,'5'), ( 6 ,'6'), (7,'7'), (8,'8'), (9,'9'), (10,'10'))
59'''( 11 ,' 11 '), ( 12 ,' 12 '), ( 13 ,' 13 '), ( 14 ,' 14 '), ( 15 ,' 15 '), ( 16 ,' 16 '), ( 17 ,' 17 '), ( 18 ,' 18 '), ( 19 ,' 19 '), ( 20 ,' 20 '),
60( 21 ,' 21 '), ( 22 ,' 22 '), ( 23 ,' 23 '), ( 24 ,' 24 '), ( 25 ,' 25 '), ( 26 ,' 26 '), ( 27 ,' 27 '), ( 28 ,' 28 '), ( 29 ,' 29 '), ( 30 ,' 30 '),
61( 31 ,' 31 '), ( 32 ,' 32 '), ( 33 ,' 33 '), ( 34 ,' 34 '), ( 35 ,' 35 '), ( 36 ,' 36 '), ( 37 ,' 37 '), ( 38 ,' 38 '), ( 39 ,' 39 '), ( 40 ,' 40 '),
62( 41 ,' 41 '), ( 42 ,' 42 '), ( 43 ,' 43 '), ( 44 ,' 44 '), ( 45 ,' 45 '), ( 46 ,' 46 '), ( 47 ,' 47 '), ( 48 ,' 48 '), ( 49 ,' 49 '), ( 50 ,' 50 '),
63( 51 ,' 51 '), ( 52 ,' 52 '), ( 53 ,' 53 '), ( 54 ,' 54 '), ( 55 ,' 55 '), ( 56 ,' 56 '), ( 57 ,' 57 '), ( 58 ,' 58 '), ( 59 ,' 59 '), ( 60 ,' 60 '),
64( 61 ,' 61 '), ( 62 ,' 62 '), ( 63 ,' 63 '), ( 64 ,' 64 '), ( 65 ,' 65 '), ( 66 ,' 66 '), ( 67 ,' 67 '), ( 68 ,' 68 '), ( 69 ,' 69 '), ( 70 ,' 70 '),
65( 71 ,' 71 '), ( 72 ,' 72 '), ( 73 ,' 73 '), ( 74 ,' 74 '), ( 75 ,' 75 '), ( 76 ,' 76 '), ( 77 ,' 77 '), ( 78 ,' 78 '), ( 79 ,' 79 '), ( 80 ,' 80 '),
66( 81 ,' 81 '), ( 82 ,' 82 '), ( 83 ,' 83 '), ( 84 ,' 84 '), ( 85 ,' 85 '), ( 86 ,' 86 '), ( 87 ,' 87 '), ( 88 ,' 88 '), ( 89 ,' 89 '), ( 90 ,' 90 '),
67( 91 ,' 91 '), ( 92 ,' 92 '), ( 93 ,' 93 '), ( 94 ,' 94 '), ( 95 ,' 95 '), ( 96 ,' 96 '), ( 97 ,' 97 '), ( 98 ,' 98 '), ( 99 ,' 99 '), ( 100 ,' 100 '))
68'''
69cur.executemany('INSERT INTO topchart VALUES(?,?)', numer)
70moja_baza.commit()
71
72def displayStation(czestotliwosc):
73 cur.execute('SELECT topchart.n_id FROM topchart WHERE topchart.n_id = (?)', (numer,))
74 numer = []
75 numer = cur.fetchall()
76 for topchart in numer:
77 id_list.append(topchart['n_id'])
78 url = URL_source
79 full_info = getInfo(radio['n_id'], url)
80 print(full_info)
81
82def displayData():
83 cur.execute('SELECT topchart.n_id FROM topchart')
84 numer = []
85 numer = cur.fetchall()
86 for topchart in numer:
87 id_list.append(topchart['n_id'])
88 url = URL_source
89 full_info = getInfo(radio['n_id'], url)
90 print(full_info)
91
92def deleteStation(delete_id):
93 cur.execute("DELETE FROM topchart WHERE n_id = (?)", (delete_id,))
94
95id_list = []
96
97while(True):
98 option = input("\n### Stacje radiowe w okolicy miasta Bydgoszcz ###\n\n\t1. Wyszukaj informacje na temat danej częstotliwości,\n\t2. Wyświetl listę stacji radiowych,\n\t3. Usuń stację radiową z listy.\nWybrana opcja: ")
99
100 if (option == '1'):
101 frequency = input("\nPodaj częstotliwość na temat której chcesz uzyskać informacje: ")
102 print("\n")
103 displayStation(numer)
104 print("\n")
105 continue
106
107 elif (option == '2'):
108 print("\n")
109 displayData()
110 print("\n")
111 continue
112
113 elif (option == '3'):
114 delete_id = input("\nPodaj częstotliwość stacji, którą chciałbyś usunąć z listy: ")
115 deleteStation(delete_id)
116 moja_baza.commit()
117 print("\nStacja o podanej częstotliwości została usunięta z listy.")
118 print("\n")
119 continue
120
121 else:
122 print("Spróbowałeś wywołać niezaimplementowaną metodę.")
123
124moja_baza.close()