· 7 years ago · Jan 15, 2019, 06:02 PM
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4import sqlite3
5import re
6from urllib.request import Request, urlopen
7
8URL_source = "http://radiomap.eu/pl/bydgoszcz"
9
10# auxiliary function that retrieves the code of a given page
11def doRequest(url):
12 return urllib.request.urlopen(url).read().decode()
13
14
15# auxiliary function that returns radio info
16def getInfo(radio_ID, url):
17
18 req = Request(URL_source, headers={'User-Agent': 'Mozilla/5.0'})
19 webpage = urlopen(req).read().decode()
20
21 id = radio_ID
22
23 # pattern for frequency
24 # <td class=freq> 93.30</td>
25 pattern = re.compile(r'freq[\w\&>]+; '+radio_ID)
26 info_full = pattern.findall(webpage)
27 frequency = re.sub(r'freq[\w\&>]+; ', "Częstotliwość: ", info_full[0])
28
29 # pattern for name
30 # <img src=../pl/images/rmf.gif width=21 height=21 align=absmiddle> RMF FM
31 pattern = re.compile(r'freq[\w\&>]+; ' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+\s*\w*' )
32 info_full = pattern.findall(webpage)
33 radioName = re.sub(r'freq[\w\&>]+; ' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> ', "Nazwa stacji radiowej: ", info_full[0])
34
35 # pattern for transmitter address
36 pattern = re.compile(r'freq[\w\&>]+; ' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+\s*\w*' + r'\s+<img[\w\s\"=\_\.<>/]+fpre>[\w\s]+')
37 info_full = pattern.findall(webpage)
38 transmitterAddress = re.sub(r'freq[\w\&>]+; ' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+\s*\w*' + r'\s+<img[\w\s\"=\_\.<>/]+fpre>', "Lokalizacja nadajnika radiowego: ", info_full[0])
39
40 dane = [frequency, radioName, transmitterAddress]
41 return dane
42
43#getInfo('93.30',URL_source)
44#print(dane)
45
46import sqlite3
47
48moja_baza = sqlite3.connect('test3.db')
49moja_baza.row_factory = sqlite3.Row
50cur = moja_baza.cursor()
51
52print ("Opened database successfully")
53print ("********************************************")
54print ("")
55
56moja_baza.executescript("""
57 DROP TABLE IF EXISTS radio;
58 CREATE TABLE IF NOT EXISTS radio
59 (id integer NOT NULL UNIQUE,
60 radioid VARCHAR(10) NOT NULL);""")
61
62print ("Table created successfully")
63print ("********************************************")
64print ("")
65
66czestotliwosci = (
67 (1,'87.70'), (2,'88.20'), (3,'88.50'), (4,'89.70'), (5,'90.50'),
68 (6,'91.90'), (7,'92.10'), (8,'92.80'), (9,'93.30'), (10,'94.40'),
69 (11,'95.10'), (12,'95.60'), (13,'96.20'), (14,'96.70'), (15,'97.60'),
70 (16,'98.90'), (17,'99.30'), (18,'100.10'), (19,'100.60'), (20,'102.10'),
71 (21,'102.60'), (22,'103.30'), (23,'103.50'), (24,'104.60'), (25,'105.10'),
72 (26,'106.10'), (27,'106.60'), (28,'107.10'), (29,'107.50'),)
73
74cur.executemany('INSERT INTO radio VALUES(?,?)', czestotliwosci)
75moja_baza.commit()
76
77def displayStation(czestotliwosc):
78 cur.execute('SELECT radioid FROM radio')
79 czestotliwosci = cur.fetchall()
80 for radio in czestotliwosci:
81 id_list.append(radio[radioid])
82 url = URL_source
83 full_info = getInfo(czestotliwosci[radioid], url)
84 print(full_info)
85
86def displayData():
87 cur.execute('SELECT radioid FROM radio')
88 czestotliwosci = cur.fetchall()
89 url = URL_source
90 full_info = getInfo(radio[radioid], url)
91 print(full_info)
92
93def deleteStation(delete_id):
94 cur.execute("DELETE FROM czestotliwosci WHERE radioid = (?)", (delete_id))
95
96id_list = []
97
98while(True):
99 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: ")
100
101 if (option == '1'):
102 frequency = input("\nPodaj częstotliwość na temat której chcesz uzyskać informacje: ")
103 print("\n")
104 displayStation(frequency)
105 print("\n")
106 continue
107
108 elif (option == '2'):
109 print("\n")
110 displayData()
111 print("\n")
112 continue
113
114 elif (option == '3'):
115 delete_id = input("\nPodaj częstotliwość stacji, którą chciałbyś usunąć z listy: ")
116 print("\n")
117 deleteStation(delete_id)
118 moja_baza.commit()
119 print("\n")
120 continue
121
122 else:
123 print("Spróbowałeś wywołać niezaimplementowaną metodę.")
124
125moja_baza.close()