26 lines
662 B
Python
26 lines
662 B
Python
"""Utility functions for handling links."""
|
|
import csv
|
|
from typing import List, NamedTuple, TextIO
|
|
|
|
|
|
class Link(NamedTuple):
|
|
"""A type for links - contains its url and date."""
|
|
|
|
url: str
|
|
date: str
|
|
|
|
|
|
def dump_links(links: List[Link], f: TextIO):
|
|
"""Dump links to a file in csv format."""
|
|
writer = csv.writer(f)
|
|
writer.writerow(["index", "link", "date"])
|
|
for i, link in enumerate(links):
|
|
writer.writerow([i, link[0], link[1]])
|
|
|
|
|
|
def read_links(f: TextIO) -> List[Link]:
|
|
"""Read links from a csv format."""
|
|
reader = csv.reader(f)
|
|
next(reader) # Skip the header
|
|
return [Link(link[1], link[2]) for link in reader]
|