moved to python modules from subprocess python script running

This commit is contained in:
JesseBot 2019-01-11 07:46:54 +00:00
parent 6357d1a4ed
commit b721b212eb
3 changed files with 35 additions and 40 deletions

View file

@ -2,42 +2,44 @@
# script by jessebot@linux.com to get the bands and do the things
# 1/7/19 -- 2019? D:
import argparse
import sqlite3
import datetime
import time
import random
import sqlite3
def adapt_datetime(ts):
"""
Internet says I need dis
"""
return time.mktime(ts.timetuple())
def generate_band_art(band):
categories = ['abstract', 'animals', 'business', 'cats', 'city', 'food',
'night', 'life', 'fashion', 'people', 'nature', 'sports',
'technics', 'transport']
category = random.choice(categories)
url = "http://lorempixel.com/400/400/{0}/{1}".format(category, band)
def add_new_band(band):
"""
Takes a str of a bands name and adds it to a sqlite3 db
returns True?
returns the string "Success" if all went well.
"""
now = str(datetime.datetime.now())
conn = sqlite3.connect('bands.db')
c = conn.cursor()
# Insert a row of data
insert = '''INSERT INTO bands VALUES ('{0}','{1}')'''.format(args.band,
now)
c.execute(insert)
# Save (commit) the changes
conn.commit()
insert = '''INSERT INTO bands VALUES ('{0}','{1}')'''.format(band, now)
try:
c.execute(insert)
# Save (commit) the changes
conn.commit()
except Exception as e:
log.error("Error: {0}".format(e))
return e
# close connection
conn.close()
# uh, idunno
return True
return "Success"
def get_all_bands():
@ -75,10 +77,10 @@ def main():
args = parser.parse_args()
if args.band:
add_new_band(args.band)
print add_new_band(args.band)
if args.get_all:
get_all_bands()
print get_all_bands()
if __name__ == "__main__":

View file

@ -1,15 +1,14 @@
#!/usr/bin/python
# Jesse Hitch - JesseBot@Linux.com
# 7/15/18 Production Web Routing File
# 1/10/19 Production Web Routing File -- Personal Routes
import bottle
from bottle import redirect, request, response, route
from bottle import run, static_file, template
import logging as log
import os
import subprocess
import sys
import yaml
import personal_routes
import band_names_db
def get_global_variables():
@ -60,14 +59,11 @@ def next_band():
log.info("oh hi, you must be here to see the name of my next band")
# Grab site specific information - YAML
globals = get_global_variables()
cmd = "./band_names.py --get-all"
# verify what we're running
log.info("Running command: {0}".format(cmd))
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
log.info("received cmd response: {0}".format(output))
return template('next-band', globals=globals, bands=output)
all_bands = band_names_db.get_all_bands()
log.info("received band.db response: {0}".format(all_bands))
return template('next-band', globals=globals, bands=all_bands)
@route('/next-band', method='POST')
@ -75,20 +71,17 @@ def next_band_submit():
# get band from post
inputBand = request.forms.get('inputBand')
log.info("received band name: {0}".format(inputBand))
cmd = "./band_names.py --band '{0}'".format(inputBand)
# verify what we're running
log.info("Running command: {0}".format(cmd))
process = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
log.info("received cmd response: {0}".format(output))
# return just the error. TODO: make legit error page or banner or somethin
if error:
return "<p>{0}</p>".format(error)
# add new band to db
add_new_band = band_names_db.add_new_band(inputBand)
# if success redirect back to main page
redirect("/next-band")
if add_new_band == "Success":
redirect("/next-band")
# else error
return "<p>THERE WAS AN ERROR</p>"
@route('/next-band-get-all')
def next_band_get_all():