# Path: djangoblog/utils.py
# def get_sha256(str):
#     m = sha256(str.encode('utf-8'))
#     return m.hexdigest()
# 
# Path: servermanager/api/blogapi.py
# class BlogApi():
#     def __init__(self):
#         self.searchqueryset = SearchQuerySet()
#         self.searchqueryset.auto_query('')
#         self.__max_takecount__ = 8
# 
#     def search_articles(self, query):
#         sqs = self.searchqueryset.auto_query(query)
#         sqs = sqs.load_all()
#         return sqs[:self.__max_takecount__]
# 
#     def get_category_lists(self):
#         return Category.objects.all()
# 
#     def get_category_articles(self, categoryname):
#         articles = Article.objects.filter(category__name=categoryname)
#         if articles:
#             return articles[:self.__max_takecount__]
#         return None
# 
#     def get_recent_articles(self):
#         return Article.objects.all()[:self.__max_takecount__]
# 
# Path: servermanager/api/commonapi.py
# class TuLing:
#     def __init__(self):
#         self.__key__ = '2f1446eb0321804291b0a1e217c25bb5'
#         self.__appid__ = 137762
# 
#     def _build_req_url(self, content):
#         return 'http://www.tuling123.com/openapi/api?key=%s&info=%s&userid=%s' % (
#             self.__key__, content, self.__appid__)
# 
#     def UserAgent(self, url):
#         rsp = requests.get(url)
#         return rsp.content
# 
#     def getdata(self, content):
#         try:
#             requrl = self._build_req_url(content)
#             res = self.UserAgent(requrl).decode('utf-8')
# 
#             jsons = json.loads(res, encoding='utf-8')
#             if str(jsons["code"]) == '100000':
#                 return jsons["text"]
#         except Exception as e:
#             logger.error(e)
#         return "哎呀，出错啦。"
# 
# Path: servermanager/models.py
# class commands(models.Model):
#     title = models.CharField('命令标题', max_length=300)
#     command = models.CharField('命令', max_length=2000)
#     describe = models.CharField('命令描述', max_length=300)
#     created_time = models.DateTimeField('创建时间', auto_now_add=True)
#     last_mod_time = models.DateTimeField('修改时间', auto_now=True)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         verbose_name = '命令'
#         verbose_name_plural = verbose_name
# 
# Path: servermanager/MemcacheStorage.py
# class MemcacheStorage(SessionStorage):
#     def __init__(self, prefix='ws_'):
#         self.prefix = prefix
#         self.cache = cache
# 
#     @property
#     def is_available(self):
#         value = "1"
#         self.set('checkavaliable', value=value)
#         return value == self.get('checkavaliable')
# 
#     def key_name(self, s):
#         return '{prefix}{s}'.format(prefix=self.prefix, s=s)
# 
#     def get(self, id):
#         id = self.key_name(id)
#         session_json = self.cache.get(id) or '{}'
#         return json_loads(session_json)
# 
#     def set(self, id, value):
#         id = self.key_name(id)
#         self.cache.set(id, json_dumps(value))
# 
#     def delete(self, id):
#         id = self.key_name(id)
#         self.cache.delete(id)


import os
import re
import jsonpickle
    import os
from werobot import WeRoBot
from werobot.replies import ArticlesReply, Article
from django.conf import settings
from djangoblog.utils import get_sha256
from servermanager.api.blogapi import BlogApi
from servermanager.api.commonapi import TuLing
from servermanager.models import commands
from .MemcacheStorage import MemcacheStorage
    from werobot.session.filestorage import FileStorage
    from django.conf import settings
    from blog.templatetags.blog_tags import truncatechars_content

@robot.handler
def echo(message, session):
    handler = MessageHandler(message, session)
    return handler.handler()


class CommandHandler():
    def __init__(self):
        self.commands = commands.objects.all()

    def run(self, title):
        cmd = list(
            filter(
                lambda x: x.title.upper() == title.upper(),
                self.commands))
        if cmd:
            return self.__run_command__(cmd[0].command)
        else:
            return "未找到相关命令，请输入hepme获得帮助。"

    def __run_command__(self, cmd):
        try:
            str = os.popen(cmd).read()
            return str
        except BaseException:
            return '命令执行出错!'

    def get_help(self):
        rsp = ''

# Path: djangoblog/utils.py
# def get_sha256(str):
#     m = sha256(str.encode('utf-8'))
#     return m.hexdigest()
# 
# Path: servermanager/api/blogapi.py
# class BlogApi():
#     def __init__(self):
#         self.searchqueryset = SearchQuerySet()
#         self.searchqueryset.auto_query('')
#         self.__max_takecount__ = 8
# 
#     def search_articles(self, query):
#         sqs = self.searchqueryset.auto_query(query)
#         sqs = sqs.load_all()
#         return sqs[:self.__max_takecount__]
# 
#     def get_category_lists(self):
#         return Category.objects.all()
# 
#     def get_category_articles(self, categoryname):
#         articles = Article.objects.filter(category__name=categoryname)
#         if articles:
#             return articles[:self.__max_takecount__]
#         return None
# 
#     def get_recent_articles(self):
#         return Article.objects.all()[:self.__max_takecount__]
# 
# Path: servermanager/api/commonapi.py
# class TuLing:
#     def __init__(self):
#         self.__key__ = '2f1446eb0321804291b0a1e217c25bb5'
#         self.__appid__ = 137762
# 
#     def _build_req_url(self, content):
#         return 'http://www.tuling123.com/openapi/api?key=%s&info=%s&userid=%s' % (
#             self.__key__, content, self.__appid__)
# 
#     def UserAgent(self, url):
#         rsp = requests.get(url)
#         return rsp.content
# 
#     def getdata(self, content):
#         try:
#             requrl = self._build_req_url(content)
#             res = self.UserAgent(requrl).decode('utf-8')
# 
#             jsons = json.loads(res, encoding='utf-8')
#             if str(jsons["code"]) == '100000':
#                 return jsons["text"]
#         except Exception as e:
#             logger.error(e)
#         return "哎呀，出错啦。"
# 
# Path: servermanager/models.py
# class commands(models.Model):
#     title = models.CharField('命令标题', max_length=300)
#     command = models.CharField('命令', max_length=2000)
#     describe = models.CharField('命令描述', max_length=300)
#     created_time = models.DateTimeField('创建时间', auto_now_add=True)
#     last_mod_time = models.DateTimeField('修改时间', auto_now=True)
# 
#     def __str__(self):
#         return self.title
# 
#     class Meta:
#         verbose_name = '命令'
#         verbose_name_plural = verbose_name
# 
# Path: servermanager/MemcacheStorage.py
# class MemcacheStorage(SessionStorage):
#     def __init__(self, prefix='ws_'):
#         self.prefix = prefix
#         self.cache = cache
# 
#     @property
#     def is_available(self):
#         value = "1"
#         self.set('checkavaliable', value=value)
#         return value == self.get('checkavaliable')
# 
#     def key_name(self, s):
#         return '{prefix}{s}'.format(prefix=self.prefix, s=s)
# 
#     def get(self, id):
#         id = self.key_name(id)
#         session_json = self.cache.get(id) or '{}'
#         return json_loads(session_json)
# 
#     def set(self, id, value):
#         id = self.key_name(id)
#         self.cache.set(id, json_dumps(value))
# 
#     def delete(self, id):
#         id = self.key_name(id)
#         self.cache.delete(id)


# Path: servermanager/robot.py
import os
import re
import jsonpickle
    import os
from werobot import WeRoBot
from werobot.replies import ArticlesReply, Article
from django.conf import settings
from djangoblog.utils import get_sha256
from servermanager.api.blogapi import BlogApi
from servermanager.api.commonapi import TuLing
from servermanager.models import commands
from .MemcacheStorage import MemcacheStorage
    from werobot.session.filestorage import FileStorage
    from django.conf import settings
    from blog.templatetags.blog_tags import truncatechars_content


@robot.handler
def echo(message, session):
    handler = MessageHandler(message, session)
    return handler.handler()


class CommandHandler():
    def __init__(self):
        self.commands = commands.objects.all()

    def run(self, title):
        cmd = list(
            filter(
                lambda x: x.title.upper() == title.upper(),
                self.commands))
        if cmd:
            return self.__run_command__(cmd[0].command)
        else:
            return "未找到相关命令，请输入hepme获得帮助。"

    def __run_command__(self, cmd):
        try:
            str = os.popen(cmd).read()
            return str
        except BaseException:
            return '命令执行出错!'

    def get_help(self):
        rsp = ''

        for cmd in self.commands: