File size: 2,049 Bytes
e1a32de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
 This file is part of Wayrunku. Copyright Rodrigo Garcia

 Wayrunku is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

 Wayrunku is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

 You should have received a copy of the GNU General Public License along with Wayrunku. If not, see <https://www.gnu.org/licenses/>.
"""
import csv
from os.path import exists


def csv_write_headers(filename="bbc.csv", headers=["title", "content", "date", "categories", "url"],
                  replace_file=False):
    replace_flag = "w" if replace_file else "a"
    with open(filename, replace_flag, encoding="UTF-8", newline="") as f:
        writer = csv.writer(f)
        writer.writerow(headers)


def csv_columnnames_exist(
        filename="bbc.csv",
        headers=["title", "content", "date", "categories", "url"]):
    file_exists = exists(filename)
    if not file_exists:
        return False

    with open(filename, "r", encoding="UTF-8", newline="") as f:
        firstline = f.readline().rstrip()
        print(f"firstline of '{filename}': {firstline}")
        return firstline == ",".join(headers)


def append_to_csv(article, keys=["title", "content", "date", "categories", "url"], filename="bbc.csv"):
    '''Agrega el contenido de un artículo como nueva fila del archivo dado, seleccionado
    las propiedades del articulo.
    :article (dictionary) con title, categories, content, date and url
    :
    '''
    with open(filename, 'a', encoding='UTF-8', newline='') as f:
        writer = csv.writer(f, delimiter=',')

        if "categories" in keys:
            article["categories"] = ",".join(article["categories"])

        row = [article[k] for k in keys]
        writer.writerow(row)