from collections import OrderedDict
from itertools import groupby

import os


class Post:
    def __init__(self, api_post, run_time):
        self.api_post = api_post
        self.run_time = run_time

    def serialize(self):
        return OrderedDict([
            ('run_time', self.run_time),
            ('headline', self.headline),
            ('post_type', self.post_type),
        ])

    @property
    def id(self):
        return self.api_post['id']

    @property
    def headline(self):
        return self.api_post['name']

    @property
    def post_type(self):
        return self.api_post['type']

class Insights:
    def __init__(self, post, api_insights):
        self.post = post
        self.api_insights = api_insights

    def serialize(self):
        return OrderedDict([
            ('shares', self.shares),
            ('likes', self.likes),
            ('comments', self.comments),
            ('people_reached', self.people_reached),
            ('photo_view_clicks', self.photo_view_clicks),
            ('link_clicks', self.link_clicks)
        ])

    def _insights(self):
        insights = {}

        for row in self.api_insights['data']:
            insights[row['name']] = row

        return insights

    def _post_stories_by_action_type(self, action_type):
        insight =  self._insights()['post_stories_by_action_type']
        value = insight['values'][0]['value']

        if not value:
            return 0

        if action_type not in value:
            return 0

        return value[action_type]

    def _post_consumptions_by_type(self, action_type):
        insight =  self._insights()['post_consumptions_by_type']
        value = insight['values'][0]['value']

        if not value:
            return 0

        if action_type not in value:
            return 0

        return value[action_type]

    @property
    def shares(self):
        return self._post_stories_by_action_type('share')

    @property
    def likes(self):
        return self._post_stories_by_action_type('like')

    @property
    def comments(self):
        return self._post_stories_by_action_type('comment')

    @property
    def people_reached(self):
        return self._insights()['post_impressions_unique']['values'][0]['value']

    @property
    def photo_view_clicks(self):
        return self._post_consumptions_by_type('photo view')

    @property
    def link_clicks(self):
        return self._post_consumptions_by_type('link clicks')

    @property
    def other_clicks(self):
        """
        Numbers for this metric don't seem to match what is displayed on the
        post insights popup. Leaving out of reporting.
        """
        return self._post_consumptions_by_type('other clicks')

@@ -370,24 +370,104 @@
 post_type),%0A
+            ('art_url', self.art_url),%0A            ('link_url', self.link_url),%0A
         %5D)%0A%0A
@@ -687,16 +687,171 @@
 type'%5D%0A%0A
+    @property%0A    def art_url(self):%0A        return self.api_post%5B'picture'%5D%0A%0A    @property%0A    def link_url(self):%0A        return self.api_post%5B'link'%5D%0A%0A%0A
 class In

add image and link urls to fb posts