# -*- coding: utf-8 -*-

import requests
from bs4 import BeautifulSoup
import processor
from datetime import datetime

def parse( url ):

	r = requests.get( url )
	if r.status_code == 404:
		return processor.create_dictionary('', url, r.status_code, [u''], [u''], u'', u'', u'', u'', [u''], [u''])

	r.encoding = 'UTF-8'
	soup = BeautifulSoup( r.text, "html.parser" )

	article = soup.find( class_ = 'article__full' )
	if article == None:
		return processor.create_dictionary('', url, r.status_code, [u''], [u''], u'', u'', u'', u'', [u''], [u''])

	processor.decompose_all( article.find_all( 'script' ) )

	categories = processor.collect_categories( article.find_all( class_ = 'article__meta__category' ) )
	datetime_list = processor.collect_datetime( soup.find( class_ = 'teaser__meta__timestamp' ) )
	author = processor.collect_text( article.find( class_ = 'author__name' ) )
	title = processor.collect_text( article.find( class_ = 'medium-title' ) )
	ingress = processor.collect_text( article.find( class_ = 'lead') )

	text = u''
	for string in article.find_all( 'p' ):
		text += ' ' + processor.collect_text( string )
	text = text.strip()

	images = processor.collect_images( article.find_all( 'img' ), 'src', 'http://www.ilkka.fi' )

	captions = []
	for caption_element in article.find_all( lambda tag: tag.name == 'a' and 'data-caption' in tag.attrs):
		captions.append( caption_element['data-caption'] )

	return processor.create_dictionary('Ilkka', url, r.status_code, categories, datetime_list, author, title, ingress, text, images, captions)

if __name__ == '__main__':

	parse( 'https://www.ilkka.fi/uutiset/talous/kauhajokelainen-serres-osti-puolet-oululaisesta-firmasta-1.2211765' )

@@ -1024,17 +1024,16 @@
 %09text = 
-u
 ''%0A%09for 

Fix ilkka parser