Skip to content

Commit

Permalink
Figure out image path if it exists and put in article dictionary.
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaeckroth committed Aug 14, 2012
1 parent 93daa62 commit ccaf0d1
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions AINewsPublisher.py
Expand Up @@ -137,7 +137,7 @@ def filter_and_process(self):

for urlid in self.articles:
# grab and convert article image (if it exists)
self.grab_convert_image(urlid, self.articles[urlid]['image_url'])
self.grab_convert_image(self.articles[urlid])

# update article in database
self.update_db(self.articles[urlid])
Expand Down Expand Up @@ -183,22 +183,24 @@ def filter_and_process(self):
# record that these articles are publishable
self.corpus.mark_publishable(self.publishable_articles)

def grab_convert_image(self, urlid, image_url):
if len(image_url) == 0:
def grab_convert_image(self, article):
if len(article['image_url']) == 0:
article['image_path'] = ''
return
try:
f = urllib2.urlopen(image_url)
img = open("%s%s" % (paths['ainews.image_dir'], str(urlid)), 'w')
f = urllib2.urlopen(article['image_url'])
img = open("%s%s" % (paths['ainews.image_dir'], str(article['urlid'])), 'w')
img.write(f.read())
img.close()
# produces [urlid].jpg
Popen("%s -format jpg -gravity Center -thumbnail 100x100 %s%s" % \
(paths['imagemagick.mogrify'], paths['ainews.image_dir'], str(urlid)),
(paths['imagemagick.mogrify'], paths['ainews.image_dir'], str(article['urlid'])),
shell = True).communicate()
# remove [urlid] file (with no extension)
remove("%s%s" % (paths['ainews.image_dir'], str(urlid)))
remove("%s%s" % (paths['ainews.image_dir'], str(article['urlid'])))
article['image_path'] = "public://newsfinder_images/%s.jpg" % article['urlid']
except:
pass
article['image_path'] = ''

def update_db(self, article):
self.db.execute("delete from categories where urlid = %s", article['urlid'])
Expand Down

0 comments on commit ccaf0d1

Please sign in to comment.