Reformat: break out function to fetch URL.

Signed-off-by: Arun Prakash Jana <engineerarun@gmail.com>
This commit is contained in:
Arun Prakash Jana 2016-03-23 01:53:46 +05:30
parent 28c9b6f44c
commit 1d99c4ccb3

133
buku
View File

@ -107,77 +107,19 @@ def usage():
# Initialize the database connection
# Create bookmarks table is not existing
def initdb():
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
if not os.path.exists(dbpath):
os.makedirs(dbpath)
dbfile = os.path.join(dbpath, 'bookmarks.db')
encpath = os.path.join(dbpath, 'bookmarks.db.enc')
# Notify if DB file needs to be decrypted first
if os.path.exists(encpath) and not os.path.exists(dbfile):
print("Unlock database first")
sys.exit(1)
# Show info on first creation
if no_crypto == False and not os.path.exists(dbfile):
print("DB file is being created. You may want to encrypt it later.")
# Create a connection
conn = sqlite3.connect(dbfile)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute('''CREATE TABLE if not exists bookmarks \
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text, tags text)''')
conn.commit()
return (conn, cur)
# Add a new bookmark or update an existing record at index
def AddUpdateEntry(conn, cur, keywords, index):
global titleManual
global online
tags = ','
# Fetch title from URL
def fetchTitle(url):
meta = ''
url = keywords[0]
if len(keywords) > 1:
for tag in keywords[1:]:
if tag[-1] == ',':
tag = tag.strip(',') + ','
else:
tag = tag.strip(',')
if tag == ',':
continue
if tags[-1] == ',':
tags += tag
else:
tags += ' ' + tag
if tags[-1] != ',':
tags += ','
if titleManual != None:
meta = titleManual
elif online == True:
secure = True
if url.find("https://") >= 0:
server = url[8:]
elif url.find("http://") >= 0:
secure = False
server = url[7:]
else:
online = False
return meta
if online == True:
marker = server.find("/")
if marker > 0:
fetchurl = server[marker:]
@ -247,8 +189,73 @@ def AddUpdateEntry(conn, cur, keywords, index):
meta = ''
finally:
urlconn.close()
return meta.strip().replace("\n","")
meta = meta.strip().replace("\n","")
# Initialize the database connection
# Create bookmarks table is not existing
def initdb():
dbpath = os.path.join(os.environ.get('HOME'), '.cache', 'buku')
if not os.path.exists(dbpath):
os.makedirs(dbpath)
dbfile = os.path.join(dbpath, 'bookmarks.db')
encpath = os.path.join(dbpath, 'bookmarks.db.enc')
# Notify if DB file needs to be decrypted first
if os.path.exists(encpath) and not os.path.exists(dbfile):
print("Unlock database first")
sys.exit(1)
# Show info on first creation
if no_crypto == False and not os.path.exists(dbfile):
print("DB file is being created. You may want to encrypt it later.")
# Create a connection
conn = sqlite3.connect(dbfile)
cur = conn.cursor()
# Create table if it doesn't exist
cur.execute('''CREATE TABLE if not exists bookmarks \
(id integer PRIMARY KEY, URL text NOT NULL UNIQUE, metadata text, tags text)''')
conn.commit()
return (conn, cur)
# Add a new bookmark or update an existing record at index
def AddUpdateEntry(conn, cur, keywords, index):
global titleManual
global online
tags = ','
meta = ''
url = keywords[0]
# Cleanse and get the tags
if len(keywords) > 1:
for tag in keywords[1:]:
if tag[-1] == ',':
tag = tag.strip(',') + ','
else:
tag = tag.strip(',')
if tag == ',':
continue
if tags[-1] == ',':
tags += tag
else:
tags += ' ' + tag
if tags[-1] != ',':
tags += ','
if titleManual != None:
meta = titleManual
elif online == True:
meta = fetchTitle(url)
if meta == '':
print("\x1B[91mTitle: []\x1B[0m")
else: