-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze_delicious.py
222 lines (158 loc) · 6.22 KB
/
analyze_delicious.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# # -*- coding: utf-8 -*-
# import argparse
# import logging
# import logging.config
# import pathlib
# from collections import defaultdict
# from datetime import datetime
# from urllib.parse import urlparse
# from typing import List
# from dataclasses import dataclass
# import csv
# # from bs4 import BeautifulSoup
# # from jinja2 import Environment, FileSystemLoader
# logging.config.fileConfig("logging.conf")
# logger = logging.getLogger("root")
# # env = Environment(loader=FileSystemLoader("templates"), autoescape=True)
# @dataclass
# class Url:
# """Wrapper for URL"""
# addr: str
# def get_domain(self) -> str:
# """Returns a domain for the URL
# :returns: a string with domain
# :rtype: str
# """
# return urlparse(self.addr).netloc
# def __str__(self):
# return self.addr
# @dataclass
# class LinkInfo:
# """Represents information for a link extracted from export file"""
# info: dict
# @property
# def is_private(self) -> bool:
# """Checks if link in LinkInfo is marked as private.
# :returns: True if private or False overwise
# :rtype: bool
# """
# return True if self.info["private"] == "1" else False
# @property
# def href(self) -> str:
# """Returns 'href' attribute
# :returns: link from href
# :rtype: str
# """
# return self.info["href"]
# @property
# def text(self) -> str:
# """Returns text description for a link
# Returns text in 'text' attribute if set. If 'text' is empty of None returns a domain.
# :returns: Text description or domain
# :rtype: str
# """
# if self.info["text"] in ["", "None"]:
# return Url(self.info["href"]).get_domain()
# return self.info["text"]
# @property
# def date(self) -> str:
# """Returns link adding date in human-readable view
# :returns: string with date
# :rtype: str
# """
# if len(self.info["add_date"]) == 4:
# return self.info["add_date"]
# t_date = datetime.fromtimestamp(int(self.info["add_date"]))
# return "{:%Y-%m-%d}".format(t_date)
# @property
# def timestamp(self) -> int:
# """Returns timestamp of adding date
# :returns: date timestamp or 0 if date is invalid
# :rtype: int
# """
# if len(self.info["add_date"]) == 4:
# return 0
# else:
# return int(self.info["add_date"])
# def __str__(self) -> str:
# if not self.is_private:
# return f"LinkInfo: {Url(self.info['href']).get_domain()}"
# else:
# return "LinkInfo is private."
# def get_tags(self) -> List[str]:
# """Gets a list of tags from 'tags' attribute
# :returns: list with tags or empty list
# :rtype: list
# """
# return self.info["tags"].split(",")
# def get_domain(self):
# """Gets a domain for a 'href' attribute
# :returns: domain address
# :rtype: str
# """
# return Url(self.info["href"]).get_domain()
# class Stats:
# def __init__(self):
# self.privacy = defaultdict(int)
# self.tags = defaultdict(int)
# def update_stats(self, link) -> None:
# if link.is_private:
# self.privacy["private"] += 1
# if not link.is_private:
# self.privacy["public"] += 1
# for tag in link.get_tags():
# self.tags[tag] += 1
# def get_links(filename, private=False):
# with open(filename, encoding="utf-8") as f:
# soup = BeautifulSoup(f.read(), "html.parser")
# for link in soup.find_all("a"):
# temp = LinkInfo({**link.attrs, **{"text": link.text}})
# if private:
# yield temp
# else:
# if not temp.is_private:
# yield temp
# def export_to_csv(filename, links):
# with open(filename, encoding="utf-8", mode="w") as f:
# writer = csv.writer(f, dialect="unix")
# writer.writerow(("Timestamp", "Href"))
# writer.writerows(((link.timestamp, link.href) for link in links))
# f.flush()
# if __name__ == "__main__":
# parser = argparse.ArgumentParser(description="Analyze your Delicious bookmarks.")
# parser.add_argument("-f", action="store", dest="filename", help="Path to file with bookmarks.")
# parser.add_argument("-o", action="store", dest="output_file", help="Path to output file", default="report.html")
# parser.add_argument(
# "--private", action="store_true", dest="process_private", default=False, help="Process private links"
# )
# parser.add_argument("-csv", action="store", dest="csv_file", help="Path to CSV file for export", default=None)
# results = parser.parse_args()
# logger.info("Starting with %s" % results)
# template = env.get_template("template.html")
# try:
# input_file = pathlib.Path(results.filename)
# with open(results.output_file, "wt") as f:
# f.write(template.render(links=(link for link in get_links(input_file, results.process_private))))
# if results.csv_file:
# export_to_csv(results.csv_file, get_links(input_file, results.process_private))
# except TypeError:
# logger.exception("No file is provided", exc_info=True)
# except FileNotFoundError:
# logger.exception("No such file", exc_info=True)
# except PermissionError:
# logger.exception("Permission problem", exc_info=True)
# finally:
# exit()
# # db_filename = "links_storage.db"
# # schema_filename = "links_schema.sql"
# # db_is_new = not os.path.exists(db_filename)
# # with sqlite3.connect(db_filename) as conn:
# # if db_is_new:
# # with open(schema_filename) as f:
# # conn.executescript(f.read())
# # cursor = conn.cursor()
# # for link in get_links(results.filename, results.process_private):
# # cursor.execute(
# # "INSERT INTO links VALUES (?, ?, ?, ?, ?)",
# # (None, str(link.url), int(link.is_private), link.timestamp, 0),
# # )