Advertisement
Guest User

Untitled

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