-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlibgen_content_csv_to_sql.py
More file actions
141 lines (132 loc) · 3.44 KB
/
libgen_content_csv_to_sql.py
File metadata and controls
141 lines (132 loc) · 3.44 KB
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
# Только не сломай...
# Не суди строго, код - говно, я знаю, но он работает, так что не трогай
# А, да, чуть не забыл - csv-шку можно взять на сайтах libgen.lc или зеркалах, там вообще-то была и просто sql база, но когда я увидел тот раздел - код для миграции с csv на sqlite\fts3 уже был написан
import sqlite3 as sqlite
import csv
ALL_FIELDS = [
"id",
"title",
"volumeinfo",
"series",
"periodical",
"author",
"year",
"edition",
"publisher",
"city",
"pages",
"pages2",
"language",
"topic",
"library",
"issue",
"identifier",
"issn",
"asin",
"udc",
"lbc",
"ddc",
"lcc",
"doi",
"googlebookid",
"openlibraryid",
"commentary",
"dpi",
"color",
"cleaned",
"orientation",
"paginated",
"scanned",
"bookmarked",
"searchable",
"filesize",
"extension",
"md5",
"generic",
"visible",
"locator",
"local",
"timeadded",
"timelastmodified",
"coverurl",
"tags",
"identifierwodash",
"pagesinfile",
"descr",
"toc",
"sha1",
"sha256",
"crc32",
"edonkey",
"aich",
"tth",
"btih",
"torrent"]
PATH_TO_CSV = "libgen_content.csv"
connection = sqlite.connect("lg_db_2.sql")
cursor = connection.cursor()
cursor.execute("""CREATE VIRTUAL TABLE if not exists libgen_book USING fts3
(tags TEXT,
language TEXT,
title TEXT,
volumeinfo TEXT,
edition TEXT,
series TEXT,
author TEXT,
year TEXT,
identifier TEXT,
publisher TEXT,
pages TEXT,
issn TEXT,
asin TEXT,
filesize TEXT,
extension TEXT,
md5 TEXT,
local TEXT,
coverurl TEXT,
identifierwodash TEXT,
#all_text TEXT,
tokenize=unicode61)""") # ЗАПОМНИ ТОКЕНАЙЗЕР, Я ПОЛ ЧАСА ГОНЯЛ ПО МАНАМ ПРЕЖДЕ ЧЕМ ЕГО НАЙТИ!!!
sql = """INSERT INTO libgen_book (rowid,
tags,
language,
title,
volumeinfo,
edition,
series,
author,
year,
identifier,
publisher,
pages,
issn,
asin,
filesize,
extension,
md5,
local,
coverurl,
identifierwodash)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"""
i = 0
with open(PATH_TO_CSV, encoding="utf-8") as file:
for row in csv.reader(file):
try:
book = dict(zip(ALL_FIELDS, row))
if book["language"] in "Russian" or book["language"] in "English":
pass
else:
continue
exi = cursor.execute(sql, (book["id"], book["tags"], book["language"].strip(), book["title"], book["volumeinfo"], book["edition"], book["series"], book["author"], book["year"].strip(), book["identifier"], book["publisher"], book["pages"], book["issn"], book["asin"], book["filesize"], book["extension"], book["md5"], book["local"], book["coverurl"], book["identifierwodash"],)) #all,))
print(str(i) + " " + str(exi.rowcount) + str(book))
i = i + 1
except Exception as e:
print(e)
continue
cursor.execute("COMMIT")
connection.commit()
cursor.execute("INSERT INTO libgen_book(libgen_book) VALUES('optimize')")
connection.commit()
print("=========================================")
print("=================ГОТОВО==================")
print("=========================================")