linkulator2/linkulator

74 lines
2.0 KiB
Plaintext
Raw Normal View History

2019-11-15 19:45:01 +00:00
#!/usr/bin/env python3
## If this script contains bugs, blame cmccabe.
import glob
import getpass
import os
import time
import sys
## TO DO:
## PARSE/USE IGNORE FILE
## VALIDATE INPUT FROM linkulator.data FILE
## CREATE A POST_REPLY() FUNCTION
## TURN main_menu() INTO A REPL LOOP
username = getpass.getuser()
help_text = "options: -h or --help for help, -p or --post to post a link, and no argument to browse links."
def main_menu():
linkulator_files = glob.glob('/home/*/.linkulator/linkulator.data')
ignore_file = glob.glob('/home/' + username + '/.linkulator/ignore')
if len(linkulator_files) == 0:
print('It looks link there are no links yet.')
else:
linkulator_lines = []
for file in linkulator_files:
with open(file) as f:
file_owner = file.split('/')[2]
for line in f:
line = line.rstrip('\n')
split_line = line.split('|')
split_line.insert(0,file_owner)
linkulator_lines.append(split_line) ## creating a list of lists
for line in linkulator_lines:
print(line)
def post_link():
link_url = input("URL: ")
link_category = input("Category: ")
link_title = input("Title: ")
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)
print('Link added. Thank you for linkulating.')
def parse_command():
args = sys.argv[1:]
if not len(args):
main_menu()
elif args[0] in ["-h", "--help", "help"]:
print(help_text)
elif args[0] in ["-p", "--post", "-p"]:
post_link()
else:
print("Unknown command: {}".format(args[0]))
if __name__ == '__main__':
parse_command()