Skip to content

Instantly share code, notes, and snippets.

@Fitzy1293
Created September 12, 2021 16:24
Show Gist options
  • Save Fitzy1293/6240a29a0c8c7d423c235afc8dd3a40d to your computer and use it in GitHub Desktop.
Save Fitzy1293/6240a29a0c8c7d423c235afc8dd3a40d to your computer and use it in GitHub Desktop.
#-------------------------------------------------------------------------------------------------------------------------------------------------------
def current_http_pid():
previously_active_pid = os.popen('ps aux | grep "python.*http.server.*8000" | head -n 1 | awk \'{ print $2 }\'').read()
return previously_active_pid
#-------------------------------------------------------------------------------------------------------------------------------------------------------
if __name__ == '__main__':
markdown_string = markdown_blog_text()
run_html_update(markdown_string)
if INIT_FLAG:
sys.exit()
if ARGS.LOOP:
http_serving_dir = os.path.join(MAIN_DIR, TITLE)
port = 8000
previously_active_pid = current_http_pid()
print('\nkilling previous http.server pid:', previously_active_pid )
os.system(f'kill {previously_active_pid}') # In case I ran localhost:8000 in another process.
print('STARTING NEW SERVER ON PORT 8000')
os.system(f'python3 -m http.server -d {http_serving_dir} {8000} &')
'''
Do a checkum on the text from the markdown file to see if it has changed.
If it has, run a the xdotool to refresh firefox.
'''
checksum = md5(markdown_string.encode('utf-8')).hexdigest() # From hashlib
try:
while True:
if checksum != md5(markdown_blog_text().encode('utf-8')).hexdigest():
new_markdown_text = markdown_blog_text()
checksum = md5(new_markdown_text.encode('utf-8')).hexdigest()
run_html_update(new_markdown_text)
# enter a key for a specific window that you have to search for
# the current firefox tab that's open will refresh
# if you are in another tab it will refresh that one
os.system('xdotool key --window "$(xdotool search --class firefox | tail -n 1)" F5')
except KeyboardInterrupt:
os.system(f'kill {current_http_pid()}') # Don't leave it active.
finally:
sys.exit('\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment