# Path: moto/moe/utils/crawer.py
# def get_crawer(url):
#     parsed = urlparse(url)
#     netloc = parsed.netloc
#     mem = inspect.getmembers(sys.modules[__name__])
#     for name, obj in mem:
#         if inspect.isclass(obj) and issubclass(obj,BaseCrawler):
#             if obj.netloc == netloc:
#                 return obj
#     return BaseCrawler
# 
# Path: moto/moe/models.py
# IMAGE_SIZE = getattr(settings,  'IMAGE_SIZE', None)
# def uuid():
#     def __unicode__(self):
#     def get_image_url(self ):
#     def posted_datetime(self):
#     def admin_thumbnail(self):
#     def admin_thumbnail(self):
# class Post(models.Model):
#     class Meta:
# class Like(models.Model):
#     class Meta:
# class Board(models.Model):
# class User(AbstractUser):
# 
# Path: moto/moe/api/serializers.py
# class PostSerializer(PostBaseSerializer):
#     author = UserSerializer(read_only=True,default=CurrentUserDefault())
#     image_large_url = serializers.SerializerMethodField()
#     source = serializers.CharField(required=False,default='')
# 
#     source_host = serializers.SerializerMethodField()
#     file_name = serializers.SerializerMethodField()
#     comment_url  = serializers.SerializerMethodField()
#     class Meta:
#         model = models.Post
#         fields =['id','description','key','image','image_url','file_name','image_large_url','author','date_posted',\
#                  'date_posted2','source','source_host','liked','likes_count','comment_url']
#         depth = 2
# 
#     def get_image_large_url(self, obj):
#         if obj.image.startswith('http'):
#             return obj.image
#         return settings.MEDIA_URL+obj.image
# 
#     def get_source_host(self, obj):
#         parsed_uri = urlparse( obj.source )
#         host = '{uri.netloc}'.format(uri=parsed_uri)
#         return host
# 
#     def get_file_name(self,obj):
#         return os.path.basename(obj.image)
# 
#     def get_comment_url(self,obj):
#         return reverse('api_list_create',kwargs={
#                     'content_type':'post',
#                     'object_pk':obj.id
#                 })


from rest_framework.generics import (GenericAPIView)
from rest_framework import  status
from moto.moe.utils.crawer import get_crawer
from rest_framework.response import Response
from six.moves.urllib.parse import urlparse,urljoin,urlencode
from moto.moe import models
from moto.moe.api.serializers import PostSerializer
import signal
import asyncio
import json




class Catch_url(GenericAPIView):
    def post(self, request, *args, **kwargs):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        url = request.POST.get('url')
        c = get_crawer(url)(url,loop)
        if c.need_login:
            c.login()
        asyncio.Task(c.run())

        try:
            loop.add_signal_handler(signal.SIGINT, loop.stop)
        except RuntimeError:
            pass
        loop.run_forever()
        print('todo:', len(c.todo))
        print('busy:', len(c.busy))
        print('done:', len(c.done), '; ok:', sum(c.done.values()))
        print('tasks:', len(c.tasks))
        return Response({'urls':c.get_results()},status=status.HTTP_200_OK)

class Download(GenericAPIView):
    model = models.Post

# Path: moto/moe/utils/crawer.py
# def get_crawer(url):
#     parsed = urlparse(url)
#     netloc = parsed.netloc
#     mem = inspect.getmembers(sys.modules[__name__])
#     for name, obj in mem:
#         if inspect.isclass(obj) and issubclass(obj,BaseCrawler):
#             if obj.netloc == netloc:
#                 return obj
#     return BaseCrawler
# 
# Path: moto/moe/models.py
# IMAGE_SIZE = getattr(settings,  'IMAGE_SIZE', None)
# def uuid():
#     def __unicode__(self):
#     def get_image_url(self ):
#     def posted_datetime(self):
#     def admin_thumbnail(self):
#     def admin_thumbnail(self):
# class Post(models.Model):
#     class Meta:
# class Like(models.Model):
#     class Meta:
# class Board(models.Model):
# class User(AbstractUser):
# 
# Path: moto/moe/api/serializers.py
# class PostSerializer(PostBaseSerializer):
#     author = UserSerializer(read_only=True,default=CurrentUserDefault())
#     image_large_url = serializers.SerializerMethodField()
#     source = serializers.CharField(required=False,default='')
# 
#     source_host = serializers.SerializerMethodField()
#     file_name = serializers.SerializerMethodField()
#     comment_url  = serializers.SerializerMethodField()
#     class Meta:
#         model = models.Post
#         fields =['id','description','key','image','image_url','file_name','image_large_url','author','date_posted',\
#                  'date_posted2','source','source_host','liked','likes_count','comment_url']
#         depth = 2
# 
#     def get_image_large_url(self, obj):
#         if obj.image.startswith('http'):
#             return obj.image
#         return settings.MEDIA_URL+obj.image
# 
#     def get_source_host(self, obj):
#         parsed_uri = urlparse( obj.source )
#         host = '{uri.netloc}'.format(uri=parsed_uri)
#         return host
# 
#     def get_file_name(self,obj):
#         return os.path.basename(obj.image)
# 
#     def get_comment_url(self,obj):
#         return reverse('api_list_create',kwargs={
#                     'content_type':'post',
#                     'object_pk':obj.id
#                 })


# Path: moto/moe/api/views2.py
from rest_framework.generics import (GenericAPIView)
from rest_framework import  status
from moto.moe.utils.crawer import get_crawer
from rest_framework.response import Response
from six.moves.urllib.parse import urlparse,urljoin,urlencode
from moto.moe import models
from moto.moe.api.serializers import PostSerializer
import signal
import asyncio
import json





class Catch_url(GenericAPIView):
    def post(self, request, *args, **kwargs):
        loop = asyncio.new_event_loop()
        asyncio.set_event_loop(loop)
        url = request.POST.get('url')
        c = get_crawer(url)(url,loop)
        if c.need_login:
            c.login()
        asyncio.Task(c.run())

        try:
            loop.add_signal_handler(signal.SIGINT, loop.stop)
        except RuntimeError:
            pass
        loop.run_forever()
        print('todo:', len(c.todo))
        print('busy:', len(c.busy))
        print('done:', len(c.done), '; ok:', sum(c.done.values()))
        print('tasks:', len(c.tasks))
        return Response({'urls':c.get_results()},status=status.HTTP_200_OK)

class Download(GenericAPIView):
    model = models.Post

    queryset = models.Post.objects.all()