post_link now handled in module posts
This commit is contained in:
parent
c54e17d6db
commit
65aa7ed835
66
linkulator
66
linkulator
|
@ -6,14 +6,14 @@ from datetime import datetime
|
|||
import getpass
|
||||
import glob
|
||||
import os
|
||||
import readline
|
||||
import signal
|
||||
import stat
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from pathlib import Path
|
||||
from shutil import which
|
||||
import posts
|
||||
import time
|
||||
|
||||
username = getpass.getuser()
|
||||
|
||||
|
@ -257,64 +257,6 @@ def view_link_in_browser(url, post_id):
|
|||
view_thread(post_id)
|
||||
|
||||
|
||||
def rlinput(prompt, prefill=''):
|
||||
readline.set_startup_hook(lambda: readline.insert_text(prefill))
|
||||
try:
|
||||
return input(prompt)
|
||||
finally:
|
||||
readline.set_startup_hook()
|
||||
|
||||
|
||||
def post_link(url=""):
|
||||
print("Enter link information here. Leaving any field blank aborts post.")
|
||||
|
||||
link_url = ""
|
||||
|
||||
while link_url == "":
|
||||
link_url = rlinput("URL: ", url) #input("URL: ")
|
||||
if "|" in link_url:
|
||||
print(
|
||||
'Pipes, "|", are illegal characters in Linkulator. Please try again.'
|
||||
)
|
||||
link_url = ""
|
||||
elif link_url == "":
|
||||
graceful_exit()
|
||||
|
||||
link_category = ""
|
||||
while link_category == "":
|
||||
link_category = input("Category: ")
|
||||
if "|" in link_category:
|
||||
print(
|
||||
'Pipes, "|", are illegal characters in Linkulator. Please try again.'
|
||||
)
|
||||
link_category = ""
|
||||
elif link_category == "":
|
||||
graceful_exit()
|
||||
|
||||
link_title = ""
|
||||
while link_title == "":
|
||||
link_title = input("Title: ")
|
||||
if "|" in link_title:
|
||||
print(
|
||||
'Pipes, "|", are illegal characters in Linkulator. Please try again.'
|
||||
)
|
||||
link_title = ""
|
||||
elif link_title == "":
|
||||
graceful_exit()
|
||||
|
||||
timestamp = str(time.time())
|
||||
filename = "/home/" + username + "/.linkulator/linkulator.data"
|
||||
if os.path.exists(filename):
|
||||
append_write = "a" # append if already exists
|
||||
else:
|
||||
append_write = "w+" # make a new file if not
|
||||
with open(filename, append_write) as file:
|
||||
file.write(
|
||||
timestamp + "||" + link_category + "|" + link_url + "|" + link_title + "\n"
|
||||
)
|
||||
print("Link added!")
|
||||
graceful_exit()
|
||||
|
||||
|
||||
def reply(owner, tstamp, post_id):
|
||||
global username
|
||||
|
@ -426,9 +368,9 @@ def parse_command():
|
|||
elif args[0] in ["-h", "--help", "help"]:
|
||||
print(help_text)
|
||||
elif args[0] in ["-p", "--post", "-p"] and len(sys.argv) > 2:
|
||||
post_link(args[1])
|
||||
posts.post_link(args[1])
|
||||
elif args[0] in ["-p", "--post", "-p"]:
|
||||
post_link()
|
||||
posts.post_link()
|
||||
else:
|
||||
print("Unknown command: {}".format(args[0]))
|
||||
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
"""handle the linkulator post process"""
|
||||
|
||||
import time
|
||||
import readline
|
||||
import os
|
||||
import getpass
|
||||
import sys
|
||||
|
||||
USERNAME = getpass.getuser()
|
||||
|
||||
|
||||
class Link:
|
||||
"""represents a single link's data"""
|
||||
|
||||
url: str
|
||||
category: str
|
||||
title: str
|
||||
timestamp: str
|
||||
|
||||
|
||||
def rlinput(prompt, prefill=""):
|
||||
readline.set_startup_hook(lambda: readline.insert_text(prefill))
|
||||
try:
|
||||
return input(prompt)
|
||||
finally:
|
||||
readline.set_startup_hook()
|
||||
|
||||
|
||||
def is_valid(entry: str) -> bool:
|
||||
"""Determine validity of an input string
|
||||
|
||||
>>> is_valid("valid")
|
||||
True
|
||||
>>> is_valid("Not|valid")
|
||||
False
|
||||
"""
|
||||
if "|" in entry:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def get_input(item: str, prefill) -> str:
|
||||
"""Get user input with the specified prompt, validate and return it or exit"""
|
||||
while True:
|
||||
i: str = rlinput(item + ": ", prefill)
|
||||
if i == "":
|
||||
sys.exit(0)
|
||||
if is_valid(i):
|
||||
return i
|
||||
|
||||
print('Pipes, "|", are illegal characters in Linkulator. Please try again.')
|
||||
|
||||
|
||||
def save_link(link):
|
||||
filename = "/home/" + USERNAME + "/.linkulator/linkulator.data"
|
||||
if os.path.exists(filename):
|
||||
append_write = "a" # append if already exists
|
||||
else:
|
||||
append_write = "w+" # make a new file if not
|
||||
with open(filename, append_write) as file:
|
||||
file.write(
|
||||
link.timestamp
|
||||
+ "||"
|
||||
+ link.category
|
||||
+ "|"
|
||||
+ link.url
|
||||
+ "|"
|
||||
+ link.title
|
||||
+ "\n"
|
||||
)
|
||||
print("Link added!")
|
||||
|
||||
|
||||
def post_link(prefill: str = ""):
|
||||
print("Enter link information here. Leaving any field blank aborts post.")
|
||||
|
||||
link = Link()
|
||||
|
||||
link.url = get_input("URL", prefill)
|
||||
link.category = get_input("Category", "")
|
||||
link.title = get_input("Title", "")
|
||||
link.timestamp = str(time.time())
|
||||
|
||||
save_link(link)
|
Loading…
Reference in New Issue