From 5b0c9c5daa36878513bcc5edbe87a5fe52fdbb82 Mon Sep 17 00:00:00 2001 From: Matt Singleton Date: Mon, 17 Apr 2017 00:34:26 -0400 Subject: get it to run from the package --- .gitignore | 4 ++++ setup.py | 20 ++++++++++++++++++++ unbiased/__init__.py | 0 unbiased/main.py | 19 ++++++++++--------- unbiased/parser.py | 5 +++-- unbiased/spotCheck.py | 6 +++--- unbiased/unbiasedFunctions.py | 28 +++++++++++++++++----------- 7 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 setup.py create mode 100644 unbiased/__init__.py diff --git a/.gitignore b/.gitignore index 90bf98d..238da47 100644 --- a/.gitignore +++ b/.gitignore @@ -6,5 +6,9 @@ legacy_py/ unbiased.html html_template/Penguins.jpg html_template/BAK* +build/ +dist/ +venv/ +unbiased.egg-info/ #* .#* diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..8b73e6d --- /dev/null +++ b/setup.py @@ -0,0 +1,20 @@ +from setuptools import setup + +setup( + name="unbiased", + version="0", + packages=['unbiased'], + package_data={ + 'unbiased': [ + 'html_template/*.html', + 'html_template/*.css', + ], + }, + install_requires=[ + ], + entry_points={ + 'console_scripts': [ + 'unbiased = unbiased.main:main', + ], + }, +) diff --git a/unbiased/__init__.py b/unbiased/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/unbiased/main.py b/unbiased/main.py index f1c3317..b8bd4cb 100755 --- a/unbiased/main.py +++ b/unbiased/main.py @@ -1,26 +1,26 @@ #!/usr/bin/env python3 import argparse -import os - -from unbiasedObjects import * -from unbiasedFunctions import * -from parser import * import time +from unbiased.unbiasedObjects import * +from unbiased.unbiasedFunctions import * +from unbiased.parser import * + def main(): parser = argparse.ArgumentParser() parser.add_argument('-w', '--webroot', default='/var/www/ubiased', help='location to write the output html') + parser.add_argument('-s', '--scratch', default='/opt/unbiased/scratch', help='writable scratch workspace') args = parser.parse_args() while True: print('-----------------------') - run(args.webroot) + run(args.webroot, args.scratch) print('-----------------------') time.sleep(600) -def run(webroot): +def run(webroot, scratch): sourceList=[] ''' @@ -33,6 +33,7 @@ def run(webroot): ''' print('running with webroot="{}"'.format(webroot)) + print('running with scratch="{}"'.format(scratch)) ### These values have to be the second half of the function name @@ -51,7 +52,7 @@ def run(webroot): src=method() sourceList.append(src) break - except: + except Exception: print('Build error. Looping again: '+source) tries+=1 time.sleep(tries) @@ -63,7 +64,7 @@ def run(webroot): outputHTML=buildOutput(newsSourceArr) #print the output file HTML - printOutputHTML(outputHTML, os.path.join(webroot, 'index.html')) + printOutputHTML(outputHTML, webroot) if __name__=="__main__": diff --git a/unbiased/parser.py b/unbiased/parser.py index f69281b..1f9bc5c 100755 --- a/unbiased/parser.py +++ b/unbiased/parser.py @@ -1,10 +1,11 @@ #!/usr/bin/env python3 -from unbiasedObjects import * -from unbiasedFunctions import buildArticle import os import re +from unbiased.unbiasedObjects import * +from unbiased.unbiasedFunctions import buildArticle + ''' Takes in a URL, downloads the file to a temp file, diff --git a/unbiased/spotCheck.py b/unbiased/spotCheck.py index d1edda4..7ce50d3 100755 --- a/unbiased/spotCheck.py +++ b/unbiased/spotCheck.py @@ -1,10 +1,10 @@ #!/usr/bin/env python3 - -from parser import * -from unbiasedObjects import * import sys +from unbiased.parser import * +from unbiased.unbiasedObjects import * + def spotCheck(src): fns = {'hil' : buildTheHill, diff --git a/unbiased/unbiasedFunctions.py b/unbiased/unbiasedFunctions.py index 1a80d7a..3d3363b 100644 --- a/unbiased/unbiasedFunctions.py +++ b/unbiased/unbiasedFunctions.py @@ -1,8 +1,10 @@ -from unbiasedObjects import * import os +import pkgutil import random -import time import re +import time + +from unbiased.unbiasedObjects import * #take in a url and delimiters, return twitter card @@ -110,7 +112,7 @@ def buildArticle(url, sourceName, encoding=None):#, titleDelStart, titleDelEnd, a=Article(title, url, img, description, sourceName, author) return a - except: + except Exception: print('^^^^^^^^^^^^^^^^^^^^^^^^^') print('\tARTICLE PARSING ERROR') print('SOURCE: '+sourceName) @@ -121,9 +123,8 @@ def buildArticle(url, sourceName, encoding=None):#, titleDelStart, titleDelEnd, def buildOutput(newsSourceArr): #read in the template html file - f=open('html_template/template.html', 'r') - template=f.read() - f.close() + template=pkgutil.get_data('unbiased', 'html_template/template.html') + template = template.decode('utf8') #set the random order for sources h1RandomSources=[] @@ -201,13 +202,18 @@ def buildOutput(newsSourceArr): #return updated text return template -def printOutputHTML(outputHTML, outFile): +def printOutputHTML(outputHTML, outDir): timestamp=time.strftime("%a, %b %-d, %-I:%M%P %Z", time.localtime()) outputHTML=outputHTML.replace('xxTimexx', timestamp) - - f=open(outFile, 'w') - f.write(outputHTML) - f.close() + + with open(os.path.join(outDir, 'index.html'), 'w') as fp: + fp.write(outputHTML) + + # copy over the template css file + css = pkgutil.get_data('unbiased', 'html_template/unbiased.css') + css = css.decode('utf8') + with open(os.path.join(outDir, 'unbiased.css'), 'w') as fp: + fp.write(css) def buildNewsSourceArr(sourceList): -- cgit v1.2.3