Change: project dir structure
This commit is contained in:
60
regex-sqlite/ejercicios/ej1/src/main.py
Normal file
60
regex-sqlite/ejercicios/ej1/src/main.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import requests
|
||||
import re
|
||||
|
||||
RSS_URL = "https://www.abc.es/rss/2.0/espana/andalucia/"
|
||||
ITEM_PATTERN = r"<item>(.*?)</item>"
|
||||
MONTHS = {
|
||||
"Jan": "01", "Feb": "02", "Mar": "03", "Apr": "04",
|
||||
"May": "05", "Jun": "06", "Jul": "07", "Aug": "08",
|
||||
"Sep": "09", "Oct": "10", "Nov": "11", "Dec": "12",
|
||||
}
|
||||
|
||||
def get_tag(text, tag):
|
||||
m = re.search(rf"<{tag}>(.*?)</{tag}>", text, re.DOTALL)
|
||||
return m.group(1).strip() if m else None
|
||||
|
||||
def format_date(raw):
|
||||
if not raw:
|
||||
return None
|
||||
|
||||
m = re.search(r"\w{3}, (\d{2}) (\w{3}) (\d{4})", raw)
|
||||
if not m:
|
||||
return None
|
||||
|
||||
d, mon, y = m.groups()
|
||||
return f"{d}/{MONTHS[mon]}/{y}"
|
||||
|
||||
def format_item(item):
|
||||
return (
|
||||
f"Título: {item['title']}\n"
|
||||
f"Link: {item['link']}\n"
|
||||
f"Fecha: {item['date']}\n"
|
||||
)
|
||||
|
||||
def get_raw():
|
||||
rss = requests.get(RSS_URL).text
|
||||
return re.findall(ITEM_PATTERN, rss, re.DOTALL)
|
||||
|
||||
def get_parsed():
|
||||
parsed = []
|
||||
|
||||
for item in get_raw():
|
||||
parsed.append({
|
||||
"title": get_tag(item, "title"),
|
||||
"link": get_tag(item, "link"),
|
||||
"date": format_date(get_tag(item, "pubDate")),
|
||||
})
|
||||
|
||||
return parsed
|
||||
|
||||
def main():
|
||||
user_month = input("Type a month (MM): ")
|
||||
user_day = input("Type a day (DD): ")
|
||||
d = f"{user_day}/{user_month}"
|
||||
|
||||
for item in get_parsed():
|
||||
if item["date"] and item["date"].startswith(d):
|
||||
print(format_item(item))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
BIN
regex-sqlite/ejercicios/ej2/data/books.bd
Normal file
BIN
regex-sqlite/ejercicios/ej2/data/books.bd
Normal file
Binary file not shown.
3203
regex-sqlite/ejercicios/ej2/data/books.csv
Normal file
3203
regex-sqlite/ejercicios/ej2/data/books.csv
Normal file
File diff suppressed because it is too large
Load Diff
39
regex-sqlite/ejercicios/ej2/src/db.py
Normal file
39
regex-sqlite/ejercicios/ej2/src/db.py
Normal file
@@ -0,0 +1,39 @@
|
||||
import sqlite3
|
||||
from pathlib import Path
|
||||
|
||||
class DBManager:
|
||||
def __init__(self, path):
|
||||
self.conn = sqlite3.connect(path)
|
||||
|
||||
def init(self):
|
||||
try:
|
||||
with self.conn:
|
||||
self.conn.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS books (
|
||||
isbn INTEGER PRIMARY KEY,
|
||||
title TEXT,
|
||||
author TEXT,
|
||||
year DATE,
|
||||
publisher TEXT
|
||||
);
|
||||
"""
|
||||
)
|
||||
except Exception as e:
|
||||
print("Error creating table:", e)
|
||||
|
||||
def insert(self, item):
|
||||
try:
|
||||
with self.conn:
|
||||
self.conn.execute(
|
||||
"""
|
||||
INSERT INTO books (isbn, title, author, year, publisher)
|
||||
VALUES (?, ?, ?, ?, ?);
|
||||
""",
|
||||
(item.isbn, item.title, item.author, item.year, item.publisher)
|
||||
)
|
||||
except Exception as e:
|
||||
print("Error inserting book:", e)
|
||||
|
||||
def close(self):
|
||||
self.conn.close()
|
||||
10
regex-sqlite/ejercicios/ej2/src/files.py
Normal file
10
regex-sqlite/ejercicios/ej2/src/files.py
Normal file
@@ -0,0 +1,10 @@
|
||||
import csv
|
||||
from collections import namedtuple
|
||||
|
||||
nt = namedtuple("Book", ["isbn", "title", "author", "year", "publisher"])
|
||||
|
||||
def read_file(file):
|
||||
with open(file, encoding="utf-8") as f:
|
||||
reader = csv.reader(f, delimiter=";")
|
||||
next(reader)
|
||||
return [nt(r[0], r[1], r[2], r[3], r[4]) for r in reader]
|
||||
16
regex-sqlite/ejercicios/ej2/src/main.py
Normal file
16
regex-sqlite/ejercicios/ej2/src/main.py
Normal file
@@ -0,0 +1,16 @@
|
||||
from files import read_file
|
||||
from db import DBManager
|
||||
from pathlib import Path
|
||||
|
||||
DATA = Path(__file__).parent.parent / "data"
|
||||
|
||||
def main():
|
||||
dbm = DBManager(DATA / "books.bd")
|
||||
dbm.init()
|
||||
|
||||
file_path = DATA / "books.csv"
|
||||
for book in read_file(file_path):
|
||||
dbm.insert(book)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user