#!/usr/bin/env python
# coding: utf-8

import os
import json
import logging
from datetime import datetime

from tweepy import Stream, StreamListener
from tweepy.models import Status

from yubari.lib.twitter import ttapi
from yubari.lib.qq import qqbot


logger = logging.getLogger(__name__)

TWITTER_maesan = '2381595966'
TAG_SAMIDARE = "毎日五月雨"
TWITTER_KanColle_STAFF = "294025417"
KanColle_PROFILE_IMAGE_TMP = '/tmp/twitter_kancolle_staff_profile_image'
kancolle_profile_image = ""


def load_profile_img():
    if not os.path.exists(KanColle_PROFILE_IMAGE_TMP):
        return
    global kancolle_profile_image
    with open(KanColle_PROFILE_IMAGE_TMP, 'r') as f:
        url = f.read()
        if url and url.startswith("https://"):
            kancolle_profile_image = url


def update_profile_img(img):
    global kancolle_profile_image
    if kancolle_profile_image == img:
        return
    kancolle_profile_image = img
    send_profile_image(img)
    logger.info("profile image changed to: %s", img)
    with open(KanColle_PROFILE_IMAGE_TMP, 'w') as f:
        f.write(img)


# TODO
def send_profile_image(img):
    qqbot.sendGroupMsg(img.replace("_normal", ""))


class MyStreamListener(StreamListener):
    def on_data(self, raw_data):
        data = json.loads(raw_data)
        if "event" in data:
            logger.debug("ignore event: %s", data["event"])
            return

        status = Status.parse(self.api, data)
        user = status.user
        if not user:
            logger.debug("empty user")
            return
        if user.id_str == TWITTER_KanColle_STAFF:
            self.proceed_kancolle(status)
        elif user.id_str == TWITTER_maesan:
            self.proceed_samidare(status)
        else:
            logger.debug("ignore user: %s", user.name)

    def proceed_kancolle(self, status):
        logger.info("kancolle: %s", status.text.replace("\n", " "))

        user = status.user

        global kancolle_profile_image
        update_profile_img(user.profile_image_url_https)

        msg = [user.name]
        timestamp = int(int(status.timestamp_ms) / 1000)
        msg.append(datetime.fromtimestamp(timestamp).ctime())
        msg.append(status.text)
        qqbot.sendGroupMsg('\n'.join(msg))

    def proceed_samidare(self, status):
        tags = status.entities.get("hashtags", [])
        if TAG_SAMIDARE not in tags:
            return

        logger.info("maesan: %s", status.text.replace("\n", " "))
        medias = status.entities.get("media", [])
        for media in medias:
            logger.info("samidare: %s", media.media_url_https)
            qqbot.sendSelfMsg(media.media_url_https)

    def on_error(self, code):
        logger.error("error: %s", code)


def run():
    load_profile_img()
    msl = Stream(auth=ttapi.auth, listener=MyStreamListener())
    #  msl.filter(track=[TAG_SAMIDARE], follow=[TWITTER_KanColle_STAFF, TWITTER_maesan])
    msl.filter(follow=[TWITTER_KanColle_STAFF, TWITTER_maesan])


if __name__ == '__main__':
    run()

@@ -1426,24 +1426,25 @@
 .api, data)%0A
+%0A
         user
@@ -1446,27 +1446,45 @@
  user = 
+getattr(
 status
-.
+, %22
 user
+%22, None)
 %0A       

fix if status has no user