summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Singleton <matt@xcolour.net>2017-04-17 21:53:42 -0400
committerMatt Singleton <matt@xcolour.net>2017-04-17 21:53:42 -0400
commit8bce5c2280441760db850d92d651d2fb0f181c50 (patch)
treea9a7302d9aa8120b9f2b91c14f7be0c2b34ee1a2
parent26f93f9c17dcf81c69b814d9d402cd20ef32e1ef (diff)
pull the images locally and resize
-rw-r--r--setup.py1
-rwxr-xr-xunbiased/main.py2
-rw-r--r--unbiased/unbiasedFunctions.py27
3 files changed, 27 insertions, 3 deletions
diff --git a/setup.py b/setup.py
index 0b43b93..2755304 100644
--- a/setup.py
+++ b/setup.py
@@ -12,6 +12,7 @@ setup(
},
install_requires=[
'jinja2',
+ 'Pillow',
],
entry_points={
'console_scripts': [
diff --git a/unbiased/main.py b/unbiased/main.py
index 159a98b..88ceb7e 100755
--- a/unbiased/main.py
+++ b/unbiased/main.py
@@ -62,7 +62,7 @@ def run(webroot, scratch):
newsSourceArr=buildNewsSourceArr(sourceList, scratch)
#build the output file HTML
- outputHTML=buildOutput(newsSourceArr)
+ outputHTML=buildOutput(newsSourceArr, webroot)
#print the output file HTML
printOutputHTML(outputHTML, webroot)
diff --git a/unbiased/unbiasedFunctions.py b/unbiased/unbiasedFunctions.py
index 192de8c..16ea07d 100644
--- a/unbiased/unbiasedFunctions.py
+++ b/unbiased/unbiasedFunctions.py
@@ -7,6 +7,8 @@ import time
from unbiased.unbiasedObjects import *
+from PIL import Image
+
#take in a url and delimiters, return twitter card
def buildArticle(url, sourceName, scratchDir, encoding=None):#, titleDelStart, titleDelEnd, imgDelStart, imgDelEnd):
@@ -125,7 +127,7 @@ def buildArticle(url, sourceName, scratchDir, encoding=None):#, titleDelStart, t
return None
-def buildOutput(newsSourceArr):
+def buildOutput(newsSourceArr, webroot):
#read in the template html file
from jinja2 import Environment, PackageLoader, select_autoescape
env = Environment(
@@ -170,17 +172,25 @@ def buildOutput(newsSourceArr):
print('\n\n@@@@\nNo H3 stories in '+newsSourceArr[x].name+'\n@@@@\n\n')
# collect articles for each section
+ image_index = 0
+
top_stories = []
for i in range(len(h1RandomSources)):
source=newsSourceArr[h1RandomSources[i]]
randomArticle=random.sample(range(len(source.h1Arr)), 1)[0]
article=source.h1Arr[randomArticle]
+ img_name = pullImage(article.img, image_index, webroot, 350, 200)
+ image_index += 1
+ article.img = img_name
top_stories.append(article)
middle_stories = []
for i in range(len(h2RandomPairs)):
pair=h2RandomPairs[i]
article=newsSourceArr[pair[0]].h2Arr[pair[1]]
+ img_name = pullImage(article.img, image_index, webroot, 150, 100)
+ image_index += 1
+ article.img = img_name
middle_stories.append(article)
bottom_stories = []
@@ -189,7 +199,6 @@ def buildOutput(newsSourceArr):
article=newsSourceArr[pair[0]].h3Arr[pair[1]]
bottom_stories.append(article)
-
sourcesStr=''
for i in range(len(newsSourceArr)-1):
sourcesStr+=newsSourceArr[i].name+', '
@@ -274,3 +283,17 @@ def buildNewsSourceArr(sourceList, scratchDir):
#return the original sourceList,
#since everything should have been modified in place
return sourceList
+
+def pullImage(url, index, webroot, width=350, height=200):
+ extension = url.split('.')[-1].split('?')[0]
+ img_name = 'img{}.{}'.format(index, extension)
+ out_file = os.path.join(webroot, img_name)
+ try:
+ subprocess.check_call(['wget', '-q', '-O', out_file, '--no-check-certificate', url])
+ except Exception:
+ return ''
+ img = Image.open(out_file)
+ img.resize((width, height))
+ jpg_name = 'img{}.jpg'.format(index)
+ img.save(os.path.join(webroot, jpg_name), 'JPEG')
+ return jpg_name