Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- import sqlite3
- import re
- from urllib.request import Request, urlopen
- URL_source = "http://radiomap.eu/pl/bydgoszcz"
- # auxiliary function that retrieves the code of a given page
- def doRequest(url):
- return urllib.request.urlopen(url).read().decode()
- # auxiliary function that returns radio info
- def getInfo(radio_ID, url):
- req = Request(URL_source, headers={'User-Agent': 'Mozilla/5.0'})
- webpage = urlopen(req).read().decode()
- id = radio_ID
- # pattern for frequency
- pattern = re.compile(r'freq>[ ]*'+ radio_ID)
- info_full = pattern.findall(webpage)
- frequency = re.sub(r'freq>[ ]*', "Częstotliwość: ", info_full[0])
- # pattern for name
- pattern = re.compile(r'freq>[ ]*' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+[ ]*\w*[ a-zA-Z0-9]*[(.]*\w*\)*' )
- info_full = pattern.findall(webpage)
- radioName = re.sub(r'freq>[ ]*' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> ', "Nazwa stacji radiowej: ", info_full[0])
- # pattern for transmitter address
- pattern = re.compile(r'freq>[ ]*' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+[ ]*\w*[ a-zA-Z0-9]*[(.]*\w*\)*' + r'\s*\n\s+<img[\w\s\"=\_\.<>\/\n]+[dx]*fpre>[\w\s\,.]+')
- info_full = pattern.findall(webpage)
- transmitterAddress = re.sub(r'freq>[ ]*' + radio_ID + r'</td>\n\s+<td[\s\w\<>="\./();]+' + r'żywo">\n\s+<img[\w\s\=\_\./]+middle> \w+[ ]*\w*[ a-zA-Z0-9]*[(.]*\w*\)*' + r'\s*\n\s+<img[\w\s\"=\_\.<>\/\n]+[dx]*fpre>', "Lokalizacja nadajnika radiowego: ", info_full[0])
- dane = [frequency, radioName, transmitterAddress]
- return dane
- #getInfo('93.30',URL_source)
- #print(dane)
- import sqlite3
- moja_baza = sqlite3.connect('test3.db')
- moja_baza.row_factory = sqlite3.Row
- cur = moja_baza.cursor()
- print ("Opened database successfully")
- print ("********************************************")
- print ("")
- moja_baza.executescript("""
- DROP TABLE IF EXISTS radio;
- CREATE TABLE IF NOT EXISTS radio
- (id integer NOT NULL UNIQUE,
- radioid VARCHAR(25) NOT NULL);""")
- print ("Table created successfully")
- print ("********************************************")
- print ("")
- czestotliwosci = (
- (1,'87.70'), (2,'88.20'), (3,'88.50'), (4,'89.70'), (5,'90.50'),
- (6,'91.90'), (7,'92.10'), (8,'92.80'), (9,'93.30'), (10,'94.40'),
- (11,'95.10'), (12,'95.60'), (13,'96.20'), (14,'96.70'), (15,'97.60'),
- (16,'98.90'), (17,'99.30'), (18,'100.10'), (19,'100.60'), (20,'102.10'),
- (21,'102.60'), (22,'103.30'), (23,'103.50'), (24,'104.60'), (25,'105.10'),
- (26,'106.10'), (27,'106.60'), (28,'107.10'), (29,'107.50'),)
- cur.executemany('INSERT INTO radio VALUES(?,?)', czestotliwosci)
- moja_baza.commit()
- def displayStation(czestotliwosc):
- cur.execute('SELECT radio.radioid FROM radio WHERE radio.radioid = (?)', (czestotliwosc,))
- czestotliwosci = []
- czestotliwosci = cur.fetchall()
- for radio in czestotliwosci:
- id_list.append(radio['radioid'])
- url = URL_source
- full_info = getInfo(radio['radioid'], url)
- print(full_info)
- def displayData():
- cur.execute('SELECT radio.radioid FROM radio')
- czestotliwosci = []
- czestotliwosci = cur.fetchall()
- for radio in czestotliwosci:
- id_list.append(radio['radioid'])
- url = URL_source
- full_info = getInfo(radio['radioid'], url)
- print(full_info)
- def deleteStation(delete_id):
- cur.execute("DELETE FROM radio WHERE radioid = (?)", (delete_id,))
- id_list = []
- while(True):
- 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: ")
- if (option == '1'):
- frequency = input("\nPodaj częstotliwość na temat której chcesz uzyskać informacje: ")
- print("\n")
- displayStation(frequency)
- print("\n")
- continue
- elif (option == '2'):
- print("\n")
- displayData()
- print("\n")
- continue
- elif (option == '3'):
- delete_id = input("\nPodaj częstotliwość stacji, którą chciałbyś usunąć z listy: ")
- deleteStation(delete_id)
- moja_baza.commit()
- print("\nStacja o podanej częstotliwości została usunięta z listy.")
- print("\n")
- continue
- else:
- print("Spróbowałeś wywołać niezaimplementowaną metodę.")
- moja_baza.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement