示例#1
0
    def confirm_action(self):
        r = RedisClient()
        current_state = r.get(self._id + ":state")

        if (current_state != "start"):
            self.error_response("Sorry, what are you saying okay for?")
        else:
            articles = r.get(self._id + ":articles")
            self.respond_get_headlines(articles)
示例#2
0
    def confirm_action(self):
        r = RedisClient()
        current_state = r.get(self._id + ":state")

        if (current_state != "start"):
            self.error_response("Sorry, what are you saying okay for?")
        else:
            articles = r.get(self._id + ":articles")
            self.respond_get_headlines(articles)
示例#3
0
 def get_media(self):
     r = RedisClient()
     state = r.get(self._id + ":state")
     if state == "start":
         self.error_response(
             "Sorry, what kind of news would you like to hear about?")
     article = self.extract_article()
     if article:
         if article['multimedia']:
             r.set(self._id + ":selected", article)
             r.set(self._id + ":state", "selected")
             have_media = Media(self.name)
             self.payload = {
                 "read": have_media.get_phrase()  # usable url for html
             }
             self.pushlater = {
                 "type": "media",
                 "value": article['multimedia']
             }
             self.finish_response()
         else:
             self.error_response(
                 "Sorry I don't have images for that article")
     else:
         self.error_response(
             "Sorry I don't know what article you are talking about")
示例#4
0
class CmdLineApp(Cmd):
    def do_list(self, arg, opts=None):
        data = config.list_conns()
        header = ["ID", "Name", "Host", "Port", "DB", "comments"]
        # tabulate.WIDE_CHARS_MODE = True
        # print_table(header,data)
        print tabulate(data, headers=header)

    @options(
        [
            make_option("-p", "--piglatin", action="store_true", help="atinLay"),
            make_option("-s", "--shout", action="store_true", help="N00B EMULATION MODE"),
            make_option("-r", "--repeat", type="int", help="output [n] times"),
        ]
    )
    def do_conn(self, arg, opts=None):
        (host, port, db) = config.get_conn(int(arg))
        self.prompt = "%s:%s > " % (host, str(db))
        self.conn = RedisClient(host, port, db)

    # @options([make_option('-p', '--piglatin', action="store_true", help="atinLay"),
    #           make_option('-s', '--shout', action="store_true", help="N00B EMULATION MODE"),
    #           make_option('-r', '--repeat', type="int", help="output [n] times")
    #           ])
    def do_get(self, arg, opts=None):
        data = self.conn.get(arg)
        txt = highlight(unicode(data, "UTF-8"), JsonLexer(), pygments.formatters.TerminalFormatter(bg="dark"))
        print txt.rstrip("\r\n").decode("raw_unicode_escape")

    def do_keys(self, arg, opts=None):
        print self.conn.keys(arg)
示例#5
0
    def extract_article(self):
        r = RedisClient()
        articles = r.get(self._id + ":articles")
        selected = r.get(self._id + ":selected")

        article = None

        entities = self.outcome["entities"]
        if "ordinal" in entities:
            value = entities["ordinal"][0]["value"]
            if value < len(articles):
                article = articles[value - 1]
        if not article and "topic" in entities:
            topic = entities["topic"][0]["value"]
            is_current_context = False
            for word in ["it", "this", "article"]:
                if word in topic:
                    article = selected
                    is_current_context = True
                    break
            if not is_current_context:
                # fuzzy string matching
                result = process.extractOne(
                    topic, map(lambda x: x["headline"], articles))
                if (result[1] > 75):
                    for i, article_candidate in enumerate(articles):
                        if (article_candidate["headline"] == result[0]):
                            article = article_candidate
                            break
        if not article and selected and selected != "None":
            article = selected
        return article
示例#6
0
    def extract_article(self):
        r = RedisClient()
        articles = r.get(self._id + ":articles")
        selected = r.get(self._id + ":selected")

        article = None

        entities = self.outcome["entities"]
        if "ordinal" in entities:
            value = entities["ordinal"][0]["value"]
            if value < len(articles):
                article = articles[value - 1]
        if not article and "topic" in entities:
            topic = entities["topic"][0]["value"]
            is_current_context = False
            for word in ["it", "this", "article"]:
                if word in topic:
                    article = selected
                    is_current_context = True
                    break
            if not is_current_context:
                # fuzzy string matching
                result = process.extractOne(topic, map(lambda x: x["headline"], articles))
                if (result[1] > 75):
                    for i, article_candidate in enumerate(articles):
                        if (article_candidate["headline"] == result[0]):
                            article = article_candidate
                            break
        if not article and selected and selected != "None":
            article = selected
        return article
示例#7
0
class WeiboValidTester(object):
    def __init__(self, website='weibo'):
        self.website = website
        self.cookies_db = RedisClient('cookies', self.website)
        self.uid_db = RedisClient('uid', self.website)

    def test(self, username, cookies):
        print('正在测试Cookies', '用户名', username)
        try:
            cookies = json.loads(cookies)
        except TypeError:
            print('Cookies不合法', username)
            self.cookies_db.delete(username)
            print('删除Cookies', username)
            return
        try:
            test_url = TEST_URL_MAP[self.website]
            response = requests.get(test_url % self.uid_db.get(username),
                                    cookies=cookies,
                                    timeout=5,
                                    allow_redirects=False)
            if response.status_code == 200:
                print('Cookies有效', username)
            else:
                print(response.status_code, response.headers)
                print('Cookies失效', username)
                self.cookies_db.delete(username)
                print('删除Cookies', username)
        except ConnectionError as e:
            print('发生异常', e.args)

    def run(self):
        cookies_groups = self.cookies_db.all()
        for username, cookies in cookies_groups.items():
            self.test(username, cookies)
示例#8
0
 def post(self):
     payload = {}
     data = tornado.escape.json_decode(self.request.body)
     user_id = data["id"]
     r = RedisClient()
     name = r.get(user_id + ":name")
     if not name:
         payload = {"status": 500}
     else:
         payload = {"id": user_id, "name": name, "status": 200}
     self.write(tornado.escape.json_encode(payload))
示例#9
0
 def post(self):
   payload = {}
   data = tornado.escape.json_decode(self.request.body)
   user_id = data["id"]
   r = RedisClient()
   name = r.get(user_id + ":name")
   if not name:
     payload = {"status": 500}
   else:
     payload = {
       "id": user_id,
       "name": name,
       "status": 200
     }
   self.write(tornado.escape.json_encode(payload))
示例#10
0
class WeiboCookiesGenerator(object):
    def __init__(self, website='weibo'):
        """
        初始化一些对象
        :param website: 名称
        :param browser: 浏览器, 若不使用浏览器则可设置为 None
        """
        self.website = website
        self.cookies_db = RedisClient('cookies', self.website)
        self.accounts_db = RedisClient('accounts', self.website)
        self.uid_db = RedisClient('uid', self.website)

    def run(self):
        """
        运行, 得到所有账户, 然后逐个模拟登录获取cookies,以及uid
        :return:
        """
        accounts_usernames = self.accounts_db.usernames()
        cookies_usernames = self.cookies_db.usernames()

        for username in accounts_usernames:
            if not username in cookies_usernames:
                password = self.accounts_db.get(username)
                print('正在生成Cookies', '账号', username, '密码', password)
                result = self.new_cookies(username, password)
                # 成功获取
                if result.get('status') == 1:
                    cookies = result.get('content')  #cookies为dict类型
                    uid = result.get('uid')
                    print('成功获取到Cookies', cookies, '成功获取到uid', uid)
                    if self.cookies_db.set(username, json.dumps(cookies)):
                        print('成功保存Cookies')
                    if self.uid_db.set(username, uid):
                        print('成功保存uid')
                # 登录出错
                elif result.get('status') == 0:
                    print(result.get('content'))
        else:
            print('所有账号都已经成功获取Cookies')

    def new_cookies(self, username, password):
        """
        生成Cookies
        :param username: 用户名
        :param password: 密码
        :return: 用户名和Cookies
        """
        return WeiboCookies(username, password).main()
示例#11
0
 def save_headline(self):
     r = RedisClient()
     state = r.get(self._id + ":state")
     if state == "start":
         self.error_response("Sorry, I didn't get that.")
     article = self.extract_article()
     if article:
         confirmation = SaveConfirmation()
         self.payload = {"read": confirmation.get_phrase()}
         # Do some other stuff to actually save the article somewhere
         r.set(self._id + ":selected", article)
         r.set(self._id + ":state", "selected")
         self.finish_response()
     else:
         self.error_response(
             "I'm sorry. I don't know which article you want me to save.")
示例#12
0
 def save_headline(self):
   r = RedisClient()
   state = r.get(self._id + ":state")
   if state == "start":
     self.error_response("Sorry, I didn't get that.");
   article = self.extract_article()
   if article:
     confirmation = SaveConfirmation()
     self.payload = {
       "read": confirmation.get_phrase()
     }
     # Do some other stuff to actually save the article somewhere
     r.set(self._id + ":selected", article)
     r.set(self._id + ":state", "selected")
     self.finish_response()
   else:
     self.error_response("I'm sorry. I don't know which article you want me to save.")
示例#13
0
    def respond_start(self, articles):
        if not article:
            self.error_response("API error")
        r = RedisClient()
        keywords = r.get(self._id + ":keywords")

        if keywords:
            keyword_articles = NYTimes.check_keywords(articles, keywords)
            hasUpdate = len(keywords) > 0
            articles = keyword_articles
        else:
            hasUpdate = False

        start_state = UpdatesOrNoUpdates(hasUpdate, self.name)
        self.payload = {"read": start_state.get_phrase()}
        r.set(self._id + ":articles", articles)
        r.set(self._id + ":state", "start")
        self.finish_response()
示例#14
0
 def get_summary(self):
     r = RedisClient()
     state = r.get(self._id + ":state")
     if state == "start":
         self.error_response("Sorry, what kind of news would you like to hear about?")
     article = self.extract_article()
     if article:
         r.set(self._id + ":selected", article)
         r.set(self._id + ":state", "selected")
         print "**************"
         print type(article)
         print article
         print article is None
         self.payload = {
             "read": "%s. Would you like me to send the full article to your kindle?" % article["snippet"]
         }
         self.finish_response()
     else:
         self.error_response("Sorry I don't know what article you are talking about")
示例#15
0
    def respond_start(self, articles):
        if not article:
            self.error_response("API error")
        r = RedisClient()
        keywords = r.get(self._id + ":keywords")

        if keywords:
            keyword_articles = NYTimes.check_keywords(articles, keywords)
            hasUpdate = len(keywords) > 0
            articles = keyword_articles
        else:
            hasUpdate = False

        start_state = UpdatesOrNoUpdates(hasUpdate, self.name)
        self.payload = {
            "read": start_state.get_phrase()
        }
        r.set(self._id + ":articles", articles)
        r.set(self._id + ":state", "start")
        self.finish_response()
示例#16
0
 def get_media(self):
     r = RedisClient()
     state = r.get(self._id + ":state")
     if state == "start":
         self.error_response("Sorry, what kind of news would you like to hear about?")
     article = self.extract_article()
     if article:
         if article['multimedia']:
             r.set(self._id + ":selected", article)
             r.set(self._id + ":state", "selected")
             have_media = Media(self.name)
             self.payload = {
                 "read": have_media.get_phrase() # usable url for html
             }
             self.pushlater = {"type": "media", "value": article['multimedia']}
             self.finish_response()
         else:
             self.error_response("Sorry I don't have images for that article")
     else:
         self.error_response("Sorry I don't know what article you are talking about")
示例#17
0
    def post(self):
        self.pushlater = {}
        self.payload = {}
        self.outcome = tornado.escape.json_decode(self.request.body)
        intent = self.outcome["intent"]
        if ("id" in self.outcome):
            self._id = self.outcome["id"]
        else:
            self.error_response("No id passed in outcome")

        print "DEBUGGING RESPONSE"
        print self.outcome
        print self._id
        f = FirebaseDB()
        if ("user" in self.outcome):
            f.post(self._id, {"type": "user", "value": self.outcome["user"]})
        if ("reporta" in self.outcome):
            f.post(self._id, {
                "type": "reporta",
                "value": self.outcome["reporta"]
            })

        r = RedisClient()
        self.name = r.get(self._id + ":name")

        if (intent == "start"):
            self.start()
        elif (intent == "confirm_action"):
            self.confirm_action()
        elif (intent == "get_headlines"):
            self.get_headlines()
        elif (intent == "get_summary"):
            self.get_summary()
        elif (intent == "get_media"):
            self.get_media()
        elif (intent == "save_headline"):
            self.save_headline()
        else:
            error_messages = Error(self.name)
            self.error_response(error_messages.get_phrase())
示例#18
0
 def get_summary(self):
     r = RedisClient()
     state = r.get(self._id + ":state")
     if state == "start":
         self.error_response(
             "Sorry, what kind of news would you like to hear about?")
     article = self.extract_article()
     if article:
         r.set(self._id + ":selected", article)
         r.set(self._id + ":state", "selected")
         print "**************"
         print type(article)
         print article
         print article is None
         self.payload = {
             "read":
             "%s. Would you like me to send the full article to your kindle?"
             % article["snippet"]
         }
         self.finish_response()
     else:
         self.error_response(
             "Sorry I don't know what article you are talking about")
示例#19
0
    def post(self):
        self.pushlater = {}
        self.payload = {}
        self.outcome = tornado.escape.json_decode(self.request.body)
        intent = self.outcome["intent"]
        if ("id" in self.outcome):
            self._id = self.outcome["id"]
        else:
            self.error_response("No id passed in outcome")

        print "DEBUGGING RESPONSE"
        print self.outcome
        print self._id
        f = FirebaseDB()
        if ("user" in self.outcome):
            f.post(self._id, {"type": "user", "value": self.outcome["user"] })
        if ("reporta" in self.outcome):
            f.post(self._id, {"type": "reporta", "value": self.outcome["reporta"] })

        r = RedisClient()
        self.name = r.get(self._id + ":name")

        if (intent == "start"):
            self.start()
        elif (intent == "confirm_action"):
            self.confirm_action()
        elif (intent == "get_headlines"):
            self.get_headlines()
        elif (intent == "get_summary"):
            self.get_summary()
        elif (intent == "get_media"):
            self.get_media()
        elif (intent == "save_headline"):
            self.save_headline()
        else:
            error_messages = Error(self.name)
            self.error_response(error_messages.get_phrase())