# Path: src/presenters/models.py
# class Platform(models.Model):
#     name = models.CharField(max_length=50)
#     url = models.CharField(max_length=50)
#     intro = models.TextField()
#     logo = models.ImageField(upload_to='platform_logos', blank=True)
# 
#     login_param = models.TextField()
#     fetch_status = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Presenter(models.Model):
#     game = models.ForeignKey('Game', default=get_default_game)
#     platform = models.ForeignKey('Platform')
#     id_in_platform = models.IntegerField(default=0)
#     tag = models.ManyToManyField('Tag', blank=True)
#     nickname = models.CharField(max_length=50)
#     introduce = models.TextField()
#     room_url = models.CharField(max_length=255, blank=True)
#     gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
#     avatar_url = models.CharField(max_length=255, blank=True)
# 
#     join_date = models.DateField(auto_now_add=True)
# 
#     invalid = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.nickname
# 
# class Game(models.Model):
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name


from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Count
from .models import Platform, Presenter, Game
from haystack.query import SearchQuerySet
import urllib, urllib2, cookielib, json
import time
import logging
        result.append(item)

    logger.info("\tfetch presenter data from douyu successful")
    return result

def fetch_longzhu(request):
    # 龙珠直播
    # 发送json请求时会在Cookie里放入plu_id
    # plu_id采取登录后从Charles中抓取
    result = []
    platform = Platform.objects.get(name=u'龙珠')
    try:
        params = json.loads(platform.login_param)
    except:
        logger.error("\tfetch presenter data from longzhu failed, error message: {%s}", '无效的fetch参数')
        return result

    plu_id = params['plu_id']
    longzhu_url = 'http://star.api.plu.cn/RoomSubscription/UserSubsciptionListForAll?isLive=0&liveSource=0&pageIndex=1&pageSize=10'
    _headers = {
        "Cookie": 'p1u_id=%s' % (plu_id)
    }
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    req = urllib2.Request(longzhu_url, headers=_headers)
    response = opener.open(req)
    json_data = json.loads(response.read())

    for data in json_data['Data']:
        item = {}

# Path: src/presenters/models.py
# class Platform(models.Model):
#     name = models.CharField(max_length=50)
#     url = models.CharField(max_length=50)
#     intro = models.TextField()
#     logo = models.ImageField(upload_to='platform_logos', blank=True)
# 
#     login_param = models.TextField()
#     fetch_status = models.BooleanField(default=False)
# 
#     def __unicode__(self):
#         return self.name
# 
# class Presenter(models.Model):
#     game = models.ForeignKey('Game', default=get_default_game)
#     platform = models.ForeignKey('Platform')
#     id_in_platform = models.IntegerField(default=0)
#     tag = models.ManyToManyField('Tag', blank=True)
#     nickname = models.CharField(max_length=50)
#     introduce = models.TextField()
#     room_url = models.CharField(max_length=255, blank=True)
#     gender = models.CharField(max_length=1, choices=GENDER_CHOICES, default='M')
#     avatar_url = models.CharField(max_length=255, blank=True)
# 
#     join_date = models.DateField(auto_now_add=True)
# 
#     invalid = models.BooleanField(default=True)
# 
#     def __unicode__(self):
#         return self.nickname
# 
# class Game(models.Model):
#     name = models.CharField(max_length=50)
# 
#     def __unicode__(self):
#         return self.name


# Path: src/presenters/views.py
from django.shortcuts import render
from django.shortcuts import get_object_or_404
from django.http import HttpResponse, Http404
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Count
from .models import Platform, Presenter, Game
from haystack.query import SearchQuerySet
import urllib, urllib2, cookielib, json
import time
import logging

        result.append(item)

    logger.info("\tfetch presenter data from douyu successful")
    return result

def fetch_longzhu(request):
    # 龙珠直播
    # 发送json请求时会在Cookie里放入plu_id
    # plu_id采取登录后从Charles中抓取
    result = []
    platform = Platform.objects.get(name=u'龙珠')
    try:
        params = json.loads(platform.login_param)
    except:
        logger.error("\tfetch presenter data from longzhu failed, error message: {%s}", '无效的fetch参数')
        return result

    plu_id = params['plu_id']
    longzhu_url = 'http://star.api.plu.cn/RoomSubscription/UserSubsciptionListForAll?isLive=0&liveSource=0&pageIndex=1&pageSize=10'
    _headers = {
        "Cookie": 'p1u_id=%s' % (plu_id)
    }
    cj = cookielib.CookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    req = urllib2.Request(longzhu_url, headers=_headers)
    response = opener.open(req)
    json_data = json.loads(response.read())

    for data in json_data['Data']:
        item = {}

        item['platform'] = Platform.objects.get(name=u'龙珠')