linkulator2/linkulator

109 lines
3.2 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
2019-11-16 12:21:31 +00:00
## --POST COMMAND SHOULD TAKE AN OPTIONAL LINK ARGUMENT
2019-11-15 19:45:01 +00:00
username = getpass.getuser()
help_text = "options: -h or --help for help, -p or --post to post a link, and no argument to browse links."
2019-11-16 12:21:31 +00:00
link_data = []
categories = []
2019-11-15 19:45:01 +00:00
def main_menu():
2019-11-16 12:21:31 +00:00
## WHENEVER THIS FUNCTION IS CALLED, THE DATA IS REFRESHED FROM FILES. SINCE
## DISK IO IS PROBABLY THE HEAVIEST PART OF THIS SCRIPT, DON'T DO THIS OFTEN.
2019-11-15 19:45:01 +00:00
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:
2019-11-16 12:21:31 +00:00
global link_data
2019-11-15 19:45:01 +00:00
linkulator_lines = []
2019-11-16 12:21:31 +00:00
for filename in linkulator_files:
with open(filename) as f:
file_owner = filename.split('/')[2]
2019-11-15 19:45:01 +00:00
for line in f:
2019-11-16 12:21:31 +00:00
line = line.rstrip('\n')
split_line = line.split('|')
split_line.insert(0,file_owner)
linkulator_lines.append(split_line) ## creating a list of lists
link_data = linkulator_lines
print_categories()
# for line in linkulator_lines:
# print(line)
def print_categories():
global categories
for line in link_data:
if line[3] not in categories:
categories.append(line[3])
print("Current link post categories include: ")
print(categories)
view_category_contents()
def view_category_contents():
view_cat = ""
while view_cat not in categories:
view_cat = input("Type a category to view its posts: ")
if view_cat in ["Q", "q", "quit", "QUIT"]:
exit()
if view_cat not in categories:
print("Sorry, that category does not exist. Try again, or type \"q\" to quit.")
for line in link_data:
if line[3] == view_cat:
2019-11-15 19:45:01 +00:00
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.')
2019-11-16 12:21:31 +00:00
def reply(owner, tstamp):
print("This doesn't work yet. It will allow someone to reply to a post identified by the combination of owner (post creator) username and timestamp of the original post.")
2019-11-15 19:45:01 +00:00
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()