################################################################################################
#
#	A script that randomly selects a link from 'links.txt' and opens it in the default browser.
#
#	NOTE: The file 'links.txt' must be in the same directory as lniks.py in order for this
#	script to run.
#
#	OPTIONS: 'd' - deletes the current link and moves to the next link
#			 's' - saves the current link and moves to the next link
#			 'q' - deletes the current link and quits out of the script, saving all changes.
#			 'sq'- saves the current link and quits out of the script, saving all changes
#
#	DMD, 230812-10-51

import random
import sys
import os

def get_link(links):
	lin = random.randint(0, len(links) - 1)

	cur_link = links.pop(lin)

	return cur_link, links

ofile = open('links.txt')

links = []

for line in ofile:
	links.append(line.rstrip())

ofile.close()

if len(links) == 0:
	print "There are no links for you to read. Go outside or something!\n"
	sys.exit()

resp = ""

num_read = 0

while resp != "q" and resp != "sq":
	
	print "There are {0} links remaining. You have read {1} links this session.\n".format(str(len(links)), str(num_read))
	
	cur_link, links = get_link(links)

	print cur_link + '\n'
	
	# cmd = "echo '{0}' | pbcopy".format(cur_link)
	cmd = "open '{0}'".format(cur_link)
	os.system(cmd)
	
	resp=raw_input("Please enter the appropriate letter (d, s, q, sq): ")
	
	if resp == "d":
		print "I'm deleting that bookmark!\n"
		
		num_read = num_read + 1
		
	elif resp == "s":
		print "I'm saving the bookmark.\n"
		links.append(cur_link)
		
	elif resp == "q":
		print "Saving the bookmark edits and quitting.\n"
	
	elif resp == "sq":
		print "Saving the previous bookmark and quitting.\n"
		links.append(cur_link)
		
	else:
		print "Please enter a correct response.\n"
		links.append(cur_link)
		
	if len(links) == 0:
		print "You have reached the end of your links. Good work!"
		break

ofile = open('/Users/daviddarmon/Documents/Reference/L/lniks/links.txt', 'w')

for link in links:
	ofile.write(link + '\n')

ofile.close()