Esempio n. 1
0
 def run(self):
     """ run the case, after running, the statstical result is held in self.data as list
     """
     #CaseTemplate.LiveN += 1
     log.info("> " +self.Id+" begins TotalN/LiveN/SuccessN/ExistingN/FailN=%d/%d/%d/%d/%d" \
                   %(CaseTemplate.TotalN, CaseTemplate.LiveN, CaseTemplate.SuccessN, CaseTemplate.ExistingN, CaseTemplate.FailN))
     if not self.to_refresh():
         CaseTemplate.ExistingN += 1
         self.read()
         self.datas = self.datass[0]
         self.result = True
         pass
     else:    
         rst = self.underlying()
         if rst == True:
             CaseTemplate.SuccessN += 1
             self.result = True
             self.get_data()
             if not self.is_refresh:
                 self.write()
         else:
             log.error(self.Id+" return error" )
             if os.path.exists(self.cacheout):
                 os.remove(self.cacheout)
             CaseTemplate.FailN += 1
             self.result = False
             
     CaseTemplate.LiveN -= 1
     log.info("< " +self.Id+" ends TotalN/LiveN/SuccessN/ExistingN/FailN=%d/%d/%d/%d/%d" \
                   %(CaseTemplate.TotalN, CaseTemplate.LiveN, CaseTemplate.SuccessN, CaseTemplate.ExistingN, CaseTemplate.FailN))
Esempio n. 2
0
    def __init__(self, main_url, url="", obj_name=None, limit=0, html=True, floor=0):
        self.limit = limit
        self.counter = 0
        self.html = html
        self.floor = floor
        self.nowurl = url or main_url
        self.starturl = url or main_url
        self.page = 1

        # 天涯目前贴子分两类,处理规则各不相同
        # techforum、publicforum
        self.thread_type = self.starturl.split("/")[3]

        content = reconnecting_urlopen(main_url, retry=100).decode("gbk", "ignore")
        log.info("content %s %s %s" % (len(content), type(content), content[:100]))
        soup = BeautifulSoup(content)

        # 处理obj_name
        self.obj_name = obj_name
        if not self.obj_name:
            # 获得楼主昵称
            if self.thread_type == "techforum":
                self.obj_name = soup.find("div", {"class": "vcard"}).find("a", target="_blank").renderContents()
            else:
                self.obj_name = self.get_firstauthor(soup)
Esempio n. 3
0
    def express_interest(self, chunkinfo):
        """this method may express illegal Interest, thus, re_express_interest and first_express_interest are in charge of checking;
            even that, there may also illegal Interest, due to unknown final_byte, leading to useless chunkinfo in chunkinfos and illegal Data(Nack) or Interest timeout
                 (we do not use is_all to check, since final_byte is more accurate and is_all -> final_byte);
            thus, we need do_receiving_content to handle illegal Data
        """
        assert chunkinfo != None, "chunkinfo == None"
        assert chunkinfo.endT == None, "chunkinfo.endT != None"
        
        
        selector = pyndn.Interest()
        selector.answerOriginKind = 0#producer generate every time
        selector.childSelctor = 1
        selector.interestLifetime = self.rtoEstimator.get_rto()
        rst = self.handle.expressInterest(chunkinfo.ndn_name, self, selector)
        
        
        if rst != None and rst < 0:        
            log.info("fail to express interest=%s with result %s" %(chunkinfo.ndn_name, rst))
            self.window.update_nack(chunkinfo)
            chunkinfo.status = 0

        else:
            chunkinfo.retxN += 1    
            log.debug("express interest=%s" %(chunkinfo.ndn_name))
Esempio n. 4
0
    def stat(self):
        f = open(self.fpath)
        
        lastkind = "none"
        count = 0
        
        for line in f.readlines():
            parts = line.split(",")
            kind = parts[0].split("=")[1]
            index = parts[2].split("=")[1]
            
            if kind == lastkind:
                count += 1
            else:
                count = 1
                if kind == "loss":
                    count = -1 * count
                
                self.datas.append(count)
                

                #print "kind=%s, count=%s" %(kind, count)
                if kind != "loss" and kind != "fetch":
                    print "!!!!!!!!kind = %s" %(kind)
                
                count = 0
        
        log.info("datas contains %d elements" %(len(self.datas)))
Esempio n. 5
0
 def read(self):
     """read data from cache
     """
     log.info("!+ %s cache reading begins")
     fin = open(self.cacheout)
     if self.datass == None:
         self.datass = []
     if self.datass == []:
         pass
     else:
         assert isinstance(self.datass[0], list), "self.datass[0] is not a list"
         assert len(self.datass[0]) == 0, "self.datass[0] is not empty"
         
         self.datass = []
         
     for line in fin.readlines():
         line = line.strip()
         if line.startswith("#headers:"):
             cols = line[len("#headers:"):].strip().split("|")
             if self.headers == None:
                 for head in cols:
                     if header != "":
                         self.headers.append(col)
                         
         elif line.startswith("#command:") or line.startswith("#note:"):
             pass
         elif line != "":
             cols = line.split()
             li = [float(cols[i]) for i in range(len(cols))]
             self.datass.append(li)
             
     log.info("!- %s cache reading ends")     
Esempio n. 6
0
 def notify(self, way="email", **msg): #way="email"|"print"
     '''notify users about running result, currently there are two ways: email, print
     '''
     self.t1 = time.time()
     data = PAPER+" ends "+str(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(self.t1)))+ \
         " TotalN=" + str(CaseTemplate.TotalN) +" SuccessN="+str(CaseTemplate.SuccessN)+ \
         " ExistingN="+str(CaseTemplate.ExistingN) +" FailN="+str(CaseTemplate.FailN)
     data = msg.get("data", data)
     
     log.info(data)
     if way == "print":
         return
     
     TO = msg.get("to", ["*****@*****.**"])
     FROM = "*****@*****.**"
     SMTP_HOST = "smtp.163.com"
     user= "******"
     passwords="jiangxiaoke"
     mailb = ["paper ends", data]
     mailh = ["From: "+FROM, "To: [email protected]", "Subject: " +data]
     mailmsg = "\r\n\r\n".join(["\r\n".join(mailh), "\r\n".join(mailb)])
 
     send = SMTP(SMTP_HOST)
     send.login(user, passwords)
     rst = send.sendmail(FROM, TO, mailmsg)
     
     if rst != {}:
         self.log.warn("send mail error: "+str(rst))
     else:
         self.log.info("sending mail finished")
     send.close()
Esempio n. 7
0
def edit(page = 1):
    """
        进行互评
    """
    log.info("edit")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    schoolid,option = request.query.get('schoolid'),request.query.get('option')
    db.assessment.update({'schoolid':user['schoolid'],"pro-others.schoolid":schoolid},{"$set":{"pro-others.$.option":option}})
    pro_others = list(db.assessment.find_one({'schoolid':user['schoolid']},fields = ['pro-others'])['pro-others'])
    options = list(db.options.find())#因为返回的是一个类似的list的cursor,但是它只能循环一次,所以将它转换为list
    
    paginator = Paginator(pro_others,10,0)
    pro_others = paginator.page(page)
    page_range,num_pages = paginator.page_range,paginator.num_pages
    show_firstpage = False if int(page) == 1 else True
    show_lastpage = False if int(page) == num_pages else True
    show_pages = 5 
    if num_pages <= show_pages:#当数量不足时
        newpage_range = page_range
    else:
        newpage_range = list()
        if int(page) + show_pages > num_pages:#用于显示后几位
            for pagenum in range(num_pages - show_pages + 1,num_pages + 1):
                newpage_range.append(pagenum)
        else:
            for pagenum in range(int(page),int(page)+show_pages):
                newpage_range.append(pagenum)
    
    return jinja2_template('edit-assessment.html',user = user,app = chermongapp,
        options = options,pro_others = pro_others,show_firstpage=show_firstpage,
        show_lastpage=show_lastpage,num_pages=num_pages,page_range=newpage_range)
Esempio n. 8
0
    def upcall(self, kind, upcallInfo):
        if kind == pyndn.UPCALL_FINAL:#handler is about to be deregistered    
            return pyndn.RESULT_OK

        
        if kind in [pyndn.UPCALL_INTEREST, pyndn.UPCALL_CONSUMED_INTEREST, pyndn.UPCALL_CONTENT_UNVERIFIED, pyndn.UPCALL_CONTENT_BAD]:
            log.error("unexpected kind: %s" %kind)
            return pyndn.RESULT_OK
        
        if kind == pyndn.UPCALL_CONTENT:
            self.is_all = True
            log.info("get data back: %s" %(upcallInfo.Interest.name))
            self.stop()
            return pyndn.RESULT_OK
        elif kind == pyndn.UPCALL_INTEREST_TIMED_OUT:
            if self.turn == -1:
                return pyndn.RESULT_REEXPRESS
            else:
                if self.ist_sentN < self.turn:
                    log.debug("timeout: %s" %(upcallInfo.Interest.name))
                    self.ist_sentN += 1
                    return pyndn.RESULT_REEXPRESS
                else:
                    log.info("CANNOT get data back after %d trials: %s" %(self.turn, upcallInfo.Interest.name))
                    self.stop()
                    return pyndn.RESULT_OK
Esempio n. 9
0
def manage_user(page = 1):
    """
        查看用户信息
    """
    log.info("manage_user")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    users = list(db.user.find())
    paginator = Paginator(users,10,0)
    try:
        users = paginator.page(page)
    except (EmptyPage, InvalidPage):
        users = paginator.page(paginator.num_pages)
    page_range,num_pages = paginator.page_range,paginator.num_pages
    show_firstpage = False if int(page) == 1 else True
    show_lastpage = False if int(page) == num_pages else True
    show_pages = 5
    if num_pages <= show_pages:#当数量不足时
        newpage_range = page_range
    else:
        newpage_range = list()
        if int(page) + show_pages > num_pages:#用于显示后几位
            for pagenum in range(num_pages - show_pages + 1,num_pages + 1):
                newpage_range.append(pagenum)
        else:
            for pagenum in range(int(page),int(page)+show_pages):
                newpage_range.append(pagenum)

    return jinja2_template('manage-user.html',user = user,app = chermongapp,
        users = users,show_firstpage=show_firstpage,show_lastpage=show_lastpage,
        num_pages=num_pages,page_range=newpage_range)
Esempio n. 10
0
    def scatter(self):
        log.debug(self.Id+" begin to draw ")
        plt.clf()
        
        cans = []
        for line in self.lines:
            log.debug("line.xs="+str(line.xs))
            log.debug("line.ys="+str(line.ys))
            log.debug("plt atts="+str(line.plt))
            #can = plt.plot(line.xs, line.ys, line.plt.pop("style", "-"), **line.plt)
            can = plt.scatter(x=line.xs, y=line.ys, s=self.kwargs.get("c", 1), **line.plt)
            cans.append(can)

        plt.grid(True)        
        plt.xlabel(self.canvas.pop("xlabel", "X"))
        plt.ylabel(self.canvas.pop("ylabel", "X"))    
        plt.xlim(xmax=self.canvas.pop("xmax", None))
        
        self.extend(plt)#extend line
        
        plt.legend(**self.canvas)
        #loc='lower/upper left'
        
        if HOSTOS.startswith("Darwin"):
            pass
        
        plt.savefig(self.pngout)
        plt.savefig(self.pdfout)
        log.debug(self.Id+" fig save to "+self.pngout)
        plt.close()
        
        log.info(self.Id+" ends")
Esempio n. 11
0
def logout():
    '''
        退出
    '''
    log.info("logout")
    response.delete_cookie(key='user',secret='chermong')
    return jinja2_template('login.html')
Esempio n. 12
0
 def write(self):
     """write data to cache
     layout: horizontal, every sublist will be placed in one row
 
     """
     if self.datass == None:
         log.critical("datass is None")
         return
         
     
     fout = open(self.cacheout, "w") #write the data to result file
     if self.headers != None:
         line = ""
         for header in self.headers:
             line += "|" + header
         line.strip()
         line = "#headers: " + line+"\n"
         fout.write(line)
     #line = "#command: " + case.cmd + "\n"
     #fout.write(line)
     #assert self.datass != None, "self.datass == None"
     assert isinstance(self.datass, list), "self.datass is not a list, %s" %(self.datass)
     #assert isinstance(self.datass[0], list), "self.datass[0] is not a list, %s" %(self.datass[0])
     
     for li in self.datass:
         for val in li:
             fout.write("%s\t" %(val))
         fout.write("\n")
         
     fout.flush()
     fout.close()
     log.info("$ " + self.Id+" cache writing")
Esempio n. 13
0
 def stop(self):
     self.status = Controller.STATUS_OFF
     """this is important, since we don't want to call stop twice.
         stop is called implicitly in in_order_content when consuemr acquire all the contents
         thus, when upper layer application call stop, it won't cause any problem, like fout is closed
         
         meanwhile, we don't suggest upper layer applications change the status
     """
         
     if _pyndn.is_run_executing(self.handle.ndn_data):
         self.handle.setRunTimeout(1)
     
     
     if not self.fout.closed:
         self.mydata.endT = datetime.datetime.now()
         self.fout.flush()
         self.fout.close()
     
     
     
     if self.enable_monitor:
         if not self.event_log.closed:
             self.event_log.flush()
             self.event_log.close()
             
             
      
     if threading.currentThread().is_alive():
         log.info("%s stops!" %(self.Id))
         log.info("requestedChunkN=%s" %(len(self.chunkInfos)))
         log.info(str(self.mydata))
         log.info(str(self.window))
         log.info(str(self.chunkSizeEstimator))
     
     return 0
Esempio n. 14
0
def about_author():
    """
       关于作者
    """
    log.info("about_author")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    return jinja2_template('about-author.html',user = user,app = chermongapp)
Esempio n. 15
0
def user_lookup():
    """
        查看用户信息
    """
    log.info("user_lookup")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    return jinja2_template('userprofile.html',user = user,app = chermongapp)
Esempio n. 16
0
def changepasswd():
    """
       修改密码
    """
    log.info("changepasswd")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    return jinja2_template('changepasswd.html',user = user,app = chermongapp)
Esempio n. 17
0
def index():
    """
        显示主页面
    """
    log.info("index")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    return jinja2_template('index.html',user = user,app = chermongapp)
Esempio n. 18
0
def about_copyright():
    """
       关于版权声明
    """
    log.info("about_copyright")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    return jinja2_template('about-copyright.html',user = user,app = chermongapp)
Esempio n. 19
0
    def __init__(self, Id, name, fout=None, monitor_out_dir=settings.OUT_DATA_DIR, cache_data=True, 
                 enable_monitor=True, size_fix=None, window_fix=None, rtt_fix=None,
                  packet_max_data_size=ETH_MTU-IP_HEADER_SIZE-UDP_HEADER_SIZE):
        """
        """
        Controller.__init__(self)
        
        self.Id = Id
        if monitor_out_dir == None:
            monitor_out_dir = settings.OUT_DATA_DIR
        
        self.monitor_out_dir = monitor_out_dir
        print self.monitor_out_dir
        log.info("monitor file: %s" %(self.monitor_out_dir))
        
        if not os.path.exists(self.monitor_out_dir):
            os.makedirs(self.monitor_out_dir)
        
        global CHUNK_HEADER_SIZE, MAX_DATA_SIZE
        #if len(ndn_name) > 50:
        CHUNK_HEADER_SIZE += len(name)
        MAX_DATA_SIZE -= len(name) 
        
        if not name.startswith("ndnx:") and not name.startswith("/"):
            name = "/" + name
        
        self.ndn_name = pyndn.Name(name)
        """since there is a "name" field in threading.Thread, we name it as ndn_name
        """
        self.cache_data = cache_data
        self.enable_monitor = enable_monitor    
        if self.enable_monitor == True:
            self.event_log = os.path.join(self.monitor_out_dir, "upcall_events-%s.log" %(self.Id))
            self.event_log = open(self.event_log, "w")
    
        self.fout = fout
        if self.fout == None:
            self.fout = os.path.join(".", "acquire")
            if not os.path.exists(self.fout):
                os.makedirs(self.fout)
                
            self.fout = os.path.join(self.fout, name.replace("/", "-")[1:])
            #self.fout = os.path.join(self.fout, Id)
            self.fout = open(self.fout, "w")
        self.size_fix = size_fix
        self.window_fix = window_fix
        self.packet_max_data_size = packet_max_data_size
         
        self.is_all = False #already fetch all the chunks,
        
        self.handle = pyndn.NDN()
        
        self.chunkInfos = []#only insert new elements when first_express_interest

        self.mydata = MyData()
        self.chunkSizeEstimator = ChunkSizeEstimator(Id=self.Id, out_dir=self.monitor_out_dir, packet_max_data_size=self.packet_max_data_size, is_fix=size_fix, enable_monitor=enable_monitor)
        self.window = SlideWindow(Id=self.Id, out_dir=self.monitor_out_dir, is_fix=window_fix, enable_monitor=enable_monitor)
        self.rtoEstimator = RtoEstimator(Id=self.Id, out_dir=self.monitor_out_dir, is_fix=rtt_fix, enable_monitor=enable_monitor)
Esempio n. 20
0
def timbra_xml(xml):
    log.info('Enviando a timbrar...')
    id_timbrado = util.get_epoch()
    ok, data = util.timbra_xml(atributos['emisor']['rfc'], xml, id_timbrado)
    if ok:
        name = '{}/{}.xml'.format(PATH['TIMBRADAS'], data['UUID'])
        util.save_file(name, data['XML'])
        log.info('Factura timbrada correctamente: {}'.format(name))
    return
Esempio n. 21
0
def options():
    """
        查看互评选项
    """
    log.info("options")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    options = db.options.find()
    return jinja2_template('options.html',user = user,app = chermongapp,options = options)
Esempio n. 22
0
def manage_user():
    """
        查看用户信息
    """
    log.info("manage_user")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    users = db.user.find()
    return jinja2_template('manage-user.html',user = user,app = chermongapp,users = users)
Esempio n. 23
0
def login_form():
    '''
    显示login界面
    '''
    log.info("login_form")
    user = request.get_cookie('user',secret="chermong")
    if not db.user.find({'name':user}).count():
        return jinja2_template('login.html')
    else:
        return redirect(chermongapp+'lookup') 
Esempio n. 24
0
 def handle_args(id, *args, **kwargs):
     roomkey = request.get_cookie('roomkey', secret="colorfire")
     log.info('[room_verify][roomkey='+roomkey+'][id='+id+']')
     print "room_verify:", roomkey, id
     valid = False
     if roomkey == id:
         valid = True
     if not valid:
         return template('welcome.html')
     return func(id, *args, **kwargs)
Esempio n. 25
0
def create_super(user,password):
    try:
        encryptuser = encrypt(password)
        db.user.insert({'role':'superadmin','name':user,'passwd':encryptuser})
        log.info("create superuser success")
        return True
    except Exception,e:
        traceback.print_exc()
        log.error("create superuser error")
        return False
Esempio n. 26
0
def manage_users():
    """
        更改用户信息
    """
    log.info('manage_users')
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    if "update" in request.forms.keys():
        if request.forms.get("userid") == "":#添加用户
            role = [i for i in ['superadmin','statistician','student'] if i in request.forms.keys()][0]
            if role == "superadmin":
                db.user.insert({'role':role,'name':request.forms.get('admin-name'),'passwd':encrypt(request.forms.get('raw_passwd'))})
            elif role == "statistician":
                db.user.insert({'role':role,'name':request.forms.get('statistician-name'),'passwd':encrypt(request.forms.get('raw_passwd'))})
            elif role == "student":#同时更新assessment数据库中assessment collections的document
                db.user.insert({'role':role,'schoolid':request.forms.get('schoolid'),'name':request.forms.get('student-name'),'sex':request.forms.get('sex'),'passwd':encrypt(request.forms.get('raw_passwd'))})
    
                newbie = {"schoolid":request.forms.get('schoolid'),'name':request.forms.get('student-name'),"option":"",'sex':request.forms.get('sex')}
                pro_for_others = []
                allusers = list(db.user.find({'role':'student'}))#明白为什么要转化为list
                for i in allusers:
                    if i.get('schoolid') != request.forms.get('schoolid'):
                        db.assessment.update({'schoolid':i.get('schoolid')},{'$push':{'pro-others':newbie}})
                    item = {key:value for key,value in i.iteritems() if key != "passwd" and key != "role" and key != "_id"}
                    item.update({'option':""})#dict update
                    pro_for_others.append(item)
                db.assessment.insert({'schoolid':request.forms.get('schoolid'),'pro-others':pro_for_others})
        else:#修改用户
            role = [i for i in ['superadmin','statistician','student'] if i in request.forms.keys()][0]
            if role == "superadmin":
                db.user.update({'_id':ObjectId(request.forms.get('userid'))},{'$set':{'name':request.forms.get('admin-name')}})
            elif role == "statistician":
                db.user.update({'_id':ObjectId(request.forms.get('userid'))},{'$set':{'name':request.forms.get('statistician-name')}})
            elif role == "student":
                schoolid = db.user.find_one({'_id':ObjectId(request.forms.get('userid'))},fields = ['schoolid'])['schoolid']
                #pymongo update每次只能更新一条数据
                for i in db.assessment.find():
                    db.assessment.update({"schoolid":i.get('schoolid'),"pro-others.schoolid":schoolid},{'$set':{"pro-others.$.name":request.forms.get('student-name'),"pro-others.$.schoolid":request.forms.get('schoolid'),"pro-others.$.sex":request.forms.get('sex')}})#注意这个update方法如何更新数组中数据
                db.user.update({'_id':ObjectId(request.forms.get('userid'))},{'$set':{'name':request.forms.get('student-name'),'schoolid':request.forms.get('schoolid'),'sex':request.forms.get('sex')}})
    elif "delete" in request.forms.keys():#删除用户
        ids = [ObjectId(i) for i in request.forms.keys() if i != "delete" and i != 'all-select']
        
        deleteusers = []
        for id in ids:
            deleteusers.append(db.user.find_one({"_id":id}))

        for i in db.assessment.find():
            for j in deleteusers:#利用$pull删除数组中数据
                db.assessment.update({"schoolid":i.get('schoolid')},{'$pull':{"pro-others":{'schoolid':j.get('schoolid')}}},safe = True)

        db.user.remove({"_id":{"$in":ids}},safe=True)
        schoolids = [i.get('schoolid') for i in deleteusers]
        db.assessment.remove({"schoolid":{"$in":schoolids}},safe=True)
    users = db.user.find()
    return jinja2_template('manage-user.html',user = user,app = chermongapp,users = users)
Esempio n. 27
0
def signalHandler(signum, frame, server):
    log.info("Recv Signal - %s" % signum)
    try:
        if server.running:
            log.error("!!!!!! STOP")
            server.stop()
        else:
            log.error("!!!!!! KILL")
            gevent.kill(server)
    except Exception, e:
        pass
Esempio n. 28
0
def login_submit():
    """
        进行登录
    """
    log.info("login_submit")
    user = request.forms.get('user')
    user = db.user.find_one({'$or':[{'name':user},{'schoolid':user}]})
    if checkpassword(request.forms.get('pass'),user['passwd']):
        response.set_cookie('user',user['name'],secret='chermong')
        return redirect(chermongapp+'index') 
    else:
        return login_form()
Esempio n. 29
0
def edit():
    """
        进行互评
    """
    log.info("edit")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    schoolid,option = request.query.get('schoolid'),request.query.get('option')
    db.assessment.update({'schoolid':user['schoolid'],"pro-others.schoolid":schoolid},{"$set":{"pro-others.$.option":option}})
    pro_others = db.assessment.find_one({'schoolid':user['schoolid']},fields = ['pro-others'])
    options = list(db.options.find())#因为返回的是一个类似的list的cursor,但是它只能循环一次,所以将它转换为list
    return jinja2_template('edit-assessment.html',user = user,app = chermongapp,options = options,pro_others = pro_others['pro-others'])
Esempio n. 30
0
def manage_options():
    """
        更改互评选项
    """
    log.info("manage_options")
    user = request.get_cookie('user',secret="chermong")
    user = db.user.find_one({'name':user})
    addoption = request.forms.get('add-option')
    if addoption is None:
        #注意在pymongo中使用:ObjectId对象
        ids = [ObjectId(i) for i in request.forms.keys() if i != "submit"]
        db.options.remove({"_id":{"$in":ids}},safe=True)
    else:
        db.options.insert({'option':addoption})
    options = db.options.find()
    return jinja2_template('options.html',user = user,app = chermongapp,options = options)
Esempio n. 31
0
    def serverChan(self, text, status, desp):
        if Notify.SCKEY != '':
            url = 'https://sctapi.ftqq.com/{}.send'.format(Notify.SCKEY)
            data = {'title': '{} {}'.format(text, status), 'desp': desp}
            try:
                response = self.to_python(requests.post(url, data=data).text)
            except Exception as e:
                log.error(e)
                raise HTTPError


#             else:
#                 if response['errno'] == 0:
#                     log.info('Server酱推送成功')
#                 elif response['errno'] == 1024:
#                     # SCKEY错误或一分钟内发送相同内容
#                     log.error('Server酱推送失败:\n{}'.format(response['errmsg']))
#                 else:
#                     log.error('Server酱推送失败:\n{}'.format(response))
        else:
            log.info('您未配置Server酱推送所需的SCKEY,取消Server酱推送')
            pass
Esempio n. 32
0
 def pushPlus(self, text, status, desp):
     if Notify.PUSH_PLUS_TOKEN != '':
         url = 'https://pushplus.hxtrip.com/send'
         data = {
             'token': Notify.PUSH_PLUS_TOKEN,
             'title': '{} {}'.format(text, status),
             'content': desp,
             'topic': Notify.PUSH_PLUS_USER
         }
         try:
             response = self.to_python(requests.post(url, data=data).text)
         except Exception as e:
             log.error(e)
             raise HTTPError
         else:
             if response['code'] == 200:
                 log.info('pushplus推送成功')
             else:
                 log.error('pushplus推送失败:\n{}'.format(response))
     else:
         log.info('您未配置pushplus推送所需的PUSH_PLUS_TOKEN,取消pushplus推送')
         pass
    def send(self, **kwargs):
        app = '原神签到小助手'
        status = kwargs.get('status', '')
        msg = kwargs.get('msg', '')
        hide = kwargs.get('hide', '')
        if isinstance(msg, list) or isinstance(msg, dict):
            # msg = self.to_json(msg)
            msg = '\n\n'.join(msg)
        if not hide:
            log.info(f'签到结果: {status}\n\n{msg}')
        log.info('准备推送通知...')

        self.serverChan(app, status, msg)
        self.coolPush(app, status, msg)
        self.bark(app, status, msg)
        self.tgBot(app, status, msg)
        self.ddBot(app, status, msg)
        self.wwBot(app, status, msg)
        self.wwApp(app, status, msg)
        self.iGot(app, status, msg)
        self.pushPlus(app, status, msg)
        self.custPush(app, status, msg)
Esempio n. 34
0
    def custPush(self, text, status, desp):
        PUSH_CONFIG = self.PUSH_CONFIG
        if 'PUSH_CONFIG' in os.environ:
            PUSH_CONFIG = os.environ['PUSH_CONFIG']

        if not PUSH_CONFIG:
            return log.info(f'Custom Push 🚫')
        cust = req.to_python(PUSH_CONFIG)
        title = f'{text} {status}'
        if cust['show_title_and_desp']:
            title = f'{text} {status}\n\n{desp}'
        if cust['set_data_title'] and cust['set_data_sub_title']:
            cust['data'][cust['set_data_title']] = {
                cust['set_data_sub_title']: title
            }
        elif cust['set_data_title'] and cust['set_data_desp']:
            cust['data'][cust['set_data_title']] = title
            cust['data'][cust['set_data_desp']] = desp
        elif cust['set_data_title']:
            cust['data'][cust['set_data_title']] = title
        conf = [
            cust['url'], cust['data'], 'Custom push', cust['text'],
            cust['code']
        ]
        url, data, name, text, code = conf

        if cust['method'].upper() == 'GET':
            return self.pushTemplate('get',
                                     url,
                                     params=data,
                                     name=name,
                                     token='token',
                                     text=text,
                                     code=code)
        elif cust['method'].upper() == 'POST' and cust['data_type'].lower(
        ) == 'json':
            return self.pushTemplate('post',
                                     url,
                                     json=data,
                                     name=name,
                                     token='token',
                                     text=text,
                                     code=code)
        else:
            return self.pushTemplate('post',
                                     url,
                                     data=data,
                                     name=name,
                                     token='token',
                                     text=text,
                                     code=code)
Esempio n. 35
0
 def bar(self):
     log.debug(self.Id+" begin to draw ")
     plt.clf()
     
     self.bars = []
     Width = self.canvas.pop("width", 0.2)
     for i in range(len(self.lines)):
         line = self.lines[i]
     #for line in self.lines:
         print "line.xs=",line.xs
         print "line.ys=", line.ys
         xs = [x+i*Width for x in line.xs]
         bar = plt.bar(left=xs, height=line.ys, width=Width, bottom=0, align="center", **line.plt)
         self.bars.append(bar)
         
     #plt.legend( (p1[0], p2[0]), ('Men', 'Women') )
     
     plt.legend((self.bars[i][0] for i in range(len(self.lines))), (self.lines[i].plt["label"] for i in range(len(self.lines))))
     #for bar in self.bars:
     
     xs = [Width * len(self.lines)/2 + j for j in line.xs]
     plt.xticks(xs, line.xs)
             
     plt.grid(True)
     plt.xlabel(self.canvas.pop("xlabel", " "))
     plt.ylabel(self.canvas.pop("ylabel", " "))    
     plt.legend(**self.canvas)
     plt.title(self.canvas.pop("title", " "))
     #plt.xticklabels([]) 
     self.extend(plt)
     
     plt.legend(**self.canvas)
     plt.savefig(self.pngout)
     plt.savefig(self.pdfout)
     log.debug(self.Id+" fig save to "+self.pngout)
     plt.close()
     
     log.info(self.Id+" finishes")
Esempio n. 36
0
 def ddBot(self, text, status, desp):
     if Notify.DD_BOT_TOKEN != '':
         url = 'https://oapi.dingtalk.com/robot/send?access_token={}'.format(
             Notify.DD_BOT_TOKEN)
         data = {
             "msgtype": "text",
             "text": {
                 "content": "{} {}\n\n{}".format(text, status, desp)
             }
         }
         HEADERS = {"Content-Type": "application/json"}
         if Notify.DD_BOT_SECRET != '':
             secret = Notify.DD_BOT_SECRET
             timestamp = int(round(time.time() * 1000))
             secret_enc = secret.encode('utf-8')
             string_to_sign = '{}\n{}'.format(timestamp, secret)
             string_to_sign_enc = string_to_sign.encode('utf-8')
             hmac_code = hmac.new(secret_enc,
                                  string_to_sign_enc,
                                  digestmod=hashlib.sha256).digest()
             sign = parse.quote_plus(base64.b64encode(hmac_code))
             url = 'https://oapi.dingtalk.com/robot/send?access_token={}&timestamp={}&sign={}'.format(
                 Notify.DD_BOT_TOKEN, timestamp, sign)
         try:
             response = self.to_python(
                 requests.post(url, data=json.dumps(data),
                               headers=HEADERS).text)
         except Exception as e:
             log.error(e)
             raise HTTPError
         else:
             if response['errcode'] == 0:
                 log.info('钉钉推送成功')
             else:
                 log.error('钉钉推送失败:\n{}'.format(response))
     else:
         log.info('您未配置钉钉推送所需的DD_BOT_TOKEN或DD_BOT_SECRET,取消钉钉推送')
         pass
Esempio n. 37
0
    def __init__(self,
                 main_url,
                 url="",
                 obj_name=None,
                 limit=0,
                 html=False,
                 floor=0):
        self.limit = limit
        self.counter = 0
        self.html = html
        self.floor = floor
        self.nowurl = url or main_url
        self.starturl = url or main_url
        self.is_lzl = False

        # 处理页数
        #        self.page = 1
        #        if PN_RE.search(self.starturl):
        #            self.page = int(PN_RE.search(self.starturl).group(1)) / 30 + 1

        # 处理obj_name
        self.obj_name = obj_name
        content = reconnecting_urlopen(main_url,
                                       retry=100).decode("gbk", "ignore")
        soup = BeautifulSoup(content)
        log.info("content %s %s %s" %
                 (len(content), type(content), content[:100]))

        # 查看是不是新版本,楼中楼(2011.09.26)
        is_lzl_re = IS_LZL_RE.search(content)
        if is_lzl_re:
            self.is_lzl = bool(int(is_lzl_re.group(1)))

        if not self.obj_name:
            # 获得楼主昵称
            #            self.obj_name = self.get_username(soup.find("div", {"class": "post"}))
            self.obj_name = self.get_username(
                soup.find("div", {"class": "p_post"}))
Esempio n. 38
0
    def send(self, **kwargs):
        app = 'Genshin Impact Helper'
        status = kwargs.get('status', '')
        msg = kwargs.get('msg', '')
        hide = kwargs.get('hide', '')
        if isinstance(msg, list) or isinstance(msg, dict):
            # msg = self.to_json(msg)
            msg = '\n\n'.join(msg)
        if not hide:
            log.info(f'Result: {status}\n\n{msg}')
        log.info('Prepare push notification...')

        self.serverChan(app, status, msg)
        self.coolPush(app, status, msg)
        self.bark(app, status, msg)
        self.tgBot(app, status, msg)
        self.ddBot(app, status, msg)
        self.wwBot(app, status, msg)
        self.wwApp(app, status, msg)
        self.iGot(app, status, msg)
        self.pushPlus(app, status, msg)
        self.custPush(app, status, msg)
        self.discordWebhook(app, status, msg)
Esempio n. 39
0
 def wwBot(self, text, status, desp):
     if Notify.WW_BOT_KEY != '':
         url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}'.format(
             Notify.WW_BOT_KEY)
         data = {
             'msgtype': 'text',
             'text': {
                 'content': '{} {}\n\n{}'.format(text, status, desp)
             }
         }
         try:
             response = self.to_python(requests.post(url, data=data).text)
         except Exception as e:
             log.error(e)
             raise HTTPError
         else:
             if response['errcode'] == 0:
                 log.info('企业微信推送成功')
             else:
                 log.error('企业微信推送失败:\n{}'.format(response))
     else:
         log.info('您未配置企业微信推送所需的WW_BOT_KEY,取消企业微信推送')
         pass
Esempio n. 40
0
    async def get(self):

        log.info(f"List Games num={len(self.request.app.games)}")

        # filters
        fname = self.request.rel_url.query.get('name', '').lower()

        ret = []
        for game in self.request.app.games.values():
            if fname and game.game_name.lower() != fname:
                continue

            ret.append(game.game_msg().args())

        log.info(f"List Games, {len(ret)} returned out of {len(self.request.app.games)}")
        headers = {}
        if NEED_CORS and 'Origin' in self.request.headers:
            headers = {'Access-Control-Allow-Origin': self.request.headers['Origin']}

        return web.Response(
            content_type='application/json',
            text=json.dumps(ret, ensure_ascii=False),
            headers=headers)
    def get_roles(self, max_attempt_number: int = 4):
        log.info('准备获取账号信息...')
        error = None
        response = dict()

        for i in range(1, max_attempt_number):
            try:
                content = requests.Session().get(
                    CONFIG.ROLE_URL, headers=self.get_header()).text
                response = self.to_python(content)
            except HTTPError as error:
                log.error(
                    'HTTP error when get game roles, retry %s time(s)...' % i)
                log.error('error is %s' % error)
                continue
            except KeyError as error:
                log.error(
                    'Wrong response to get game roles, retry %s time(s)...' %
                    i)
                log.error('response is %s' % error)
                continue
            except Exception as error:
                log.error('Unknown error %s, die' % error)
                raise Exception(error)
            error = None
            break

        if error:
            log.error('Maximum retry times have been reached, error is %s ' %
                      error)
            raise Exception(error)
        if response.get('retcode', 1) != 0 or response.get('data',
                                                           None) is None:
            raise Exception(response['message'])

        log.info('账号信息获取完毕')
        return response
Esempio n. 42
0
    def get_id(self):
        log.info('准备获取活动信息...')
        id_list = []
        try:
            response = req.to_python(req.request(
                'get', CONFIG.YS_URL, headers=self.header).text)
            group = response['data']['cards'][3]['card_group'][0]['group']
        except Exception as e:
            log.error(f'活动信息获取失败:\n{e}')
        else:
            for ids in group:
                if '礼包' in ids.get('title_sub', ''):
                    id = re.findall('(?<=gift\/)(.*)\?channel', ids['scheme'])[0]
                    log.info(f'└─🎁 {ids["title_sub"]}')
                    id_list.append(id)
            if not id_list:
                log.info('原神超话暂无活动')

        log.info('活动信息获取完毕')
        return id_list
Esempio n. 43
0
File: weibo.py Progetto: fluteds/gih
    def get_id(self):
        log.info('Preparing to obtain activity information...')
        id_list = []
        try:
            response = req.to_python(
                req.request('get', CONFIG.YS_URL, headers=self.header).text)
            group = response['data']['cards'][3]['card_group'][0]['group']
        except Exception as e:
            log.error(f'Activity information failure:\n{e}')
        else:
            for ids in group:
                if 'Package' in ids.get('title_sub', ''):
                    id = re.findall('(?<=gift\/)(.*)\?channel',
                                    ids['scheme'])[0]
                    log.info(f'└─🎁 {ids["title_sub"]}')
                    id_list.append(id)
            if not id_list:
                log.info('Original God overtime')

        log.info('The activity information is acquired')
        return id_list
    def send(self, app='Genshin Impact Check-In Helper', status='', msg='', **kwargs):
        hide = kwargs.get('hide', '')
        if isinstance(msg, list) or isinstance(msg, dict):
            # msg = self.to_json(msg)
            msg = '\n\n'.join(msg)
        if not hide:
            log.info(f'Sign-In result: {status}\n\n{msg}')

        if self.PUSH_CONFIG or self.DISCORD_WEBHOOK:
            log.info('Sending push notifications...')
            self.custPush(app, status, msg)
            self.discordWebhook(app, status, msg)
        else:
            log.info('No social media notifications configured to be sent.')
Esempio n. 45
0
 async def get(self):
     log.info("rest api request ... /pingmodel from " +
              str(self.request.remote))
     if (self.request.headers.get("X-Auth-Token") !=
             config.AUTHENTICATION_TOKEN_PINGMODEL):
         log.info("error authentication token ... " + "from " +
                  str(self.request.remote) + " [" +
                  str(self.request.headers.get("X-Auth-Token")) + "]")
         return web.Response(status=401,
                             text=str('{"error":"authentication token"}'))
     log.info("authentication token ... " + "from " +
              str(self.request.remote) + " [Ok]")
     log.debug(str('{"status":"started"}'))
     text = '{"status":"started"}'
     return web.Response(text=text)
Esempio n. 46
0
def get_poster_for_title(title):
    """Returns a string path to the poster image for a given title"""
    # remove unneccasry brackets from title
    title = re.sub(r'\(.*?\)', '', title)
    log.info('Search for movies by title: {}...'.format(title))
    res = MOVIE_API.search(title)
    log.info('Found {} movies...'.format(len(res)))
    poster = None

    for movie in res:
        if movie.poster_path:
            poster = movie.poster_path
            break

    log.info('Poster: {}'.format(poster))
    return poster
Esempio n. 47
0
    def get_super_list(self):
        log.info('准备获取超话列表...')
        try:
            response = req.request('get', CONFIG.SUPER_URL,
                headers=self.get_header(), allow_redirects=False)
        except Exception as e:
            log.error(e)
        else:
            if response.status_code == 200:
                log.info('🥳 weibo: 超话列表获取成功')
                return req.to_python(response.text)
            elif response.status_code == 302:
                log.error('😳 weibo: 登录可能失效, 尝试重新登录')
            else:
                log.error('😳 weibo: 超话列表获取失败')

        log.info('超话列表获取完毕')
        return
Esempio n. 48
0
File: weibo.py Progetto: fluteds/gih
    def get_super_list(self):
        log.info('Prepare to get a list of devices...')
        try:
            response = req.request('get',
                                   CONFIG.SUPER_URL,
                                   headers=self.get_header(),
                                   allow_redirects=False)
        except Exception as e:
            log.error(e)
        else:
            if response.status_code == 200:
                log.info('🥳 weibo: Superphracted list acquisition success')
                return req.to_python(response.text)
            elif response.status_code == 302:
                log.error('😳 weibo: Login may fail, try to log in again')
            else:
                log.error('😳 weibo: Super words list acquisition failed')

        log.info('Super words list')
        return
Esempio n. 49
0
    async def name(self, ws, msg: message.Name):
        """Set my name - (re)login procedure on socket"""

        name = msg.name

        if name not in self.players_map:
            if self.state != HatGame.ST_SETUP:
                log.info(
                    f'Cannot login new user {name}({id(ws)}) while game in progress'
                )
                await self.send_json(
                    ws,
                    message.Error(
                        code=104,
                        message=
                        f"Can't login new user {name} while game in progress"))
                return

            log.info(f'Player {name}({id(ws)}) was added to game')
        else:
            log.info(
                f'Player {name}({id(ws)}) already in list, re-connect user')

        reconnect = not self.register_player(
            name=name, avatar=msg.avatar, socket=ws)

        await self.game(ws)
        await self.prepare(ws if reconnect else None
                           )  # on reconnect send to self only

        if self.state == HatGame.ST_PLAY and self.shlyapa:
            s = self.shlyapa
            await self.send_json(ws, message.Tour(tour=s.get_cur_tour()))
            if self.turn:
                await self.send_json(
                    ws,
                    message.Turn(turn=s.get_cur_turn(),
                                 explain=self.turn.explaining.name,
                                 guess=self.turn.guessing.name))
        if self.state == HatGame.ST_FINISH and self.results is not None:
            await self.send_json(ws, message.Finish(results=self.results))
Esempio n. 50
0
File: weibo.py Progetto: fluteds/gih
    def get_box_code(self):
        log.info(
            'Preparing to get the redemption code of "Personal Center"...')
        id_list = []
        code_list = []
        self.header['Referer'] = f'https://ka.sina.com.cn/html5/'
        self.header['Cookie'] = self.ka_cookie
        try:
            response = req.request('get',
                                   CONFIG.BOX_URL,
                                   headers=self.header,
                                   allow_redirects=False)
        except Exception as e:
            log.error(e)
        else:
            if response.status_code == 200:
                response.encoding = 'utf-8'
                soup = BeautifulSoup(response.text, 'html.parser')
                # print(soup.prettify())
                boxs = soup.find_all(class_='giftbag')
                for box in boxs:
                    item = {
                        'id': box.find(class_='deleBtn').get('data-itemid'),
                        'title': box.find(class_='title itemTitle').text,
                        'code': box.find('span').parent.contents[1]
                    }
                    log.info(f'└─☁️{item["title"]}')
                    id_list.append(item['id'])
                    code_list.append(item)
                code_list.insert(0, id_list)
            elif response.status_code == 302:
                log.error('😳 ka.sina: Login may fail, try to log in again')
            else:
                log.error('😳 ka.sina: Redemption code acquisition failed')

        # Print exchange code
        # print(req.to_json(code_list))

        log.info('"Personal Center" redemption code is acquired')
        return code_list
Esempio n. 51
0
 def pushTemplate(self,
                  method,
                  url,
                  params=None,
                  data=None,
                  json=None,
                  headers=None,
                  **kwargs):
     name = kwargs.get('name')
     # needs = kwargs.get('needs')
     token = kwargs.get('token')
     text = kwargs.get('text')
     code = kwargs.get('code')
     if not token:
         log.info(f'{name} 🚫')
         # log.info (f '{name} The {NEDS} needed to push the {NEEDS} is not set, you are skipping ...')
         return
     try:
         response = req.to_python(
             req.request(method, url, 2, params, data, json, headers).text)
         rspcode = response[text]
     except Exception as e:
         # 🚫:disabled; 🥳:success; 😳:fail;
         log.error(f'{name} 😳\n{e}')
     else:
         if rspcode == code:
             log.info(f'{name} 🥳')
         # Telegram Bot
         elif name == 'Telegram Bot' and rspcode:
             log.info(f'{name} 🥳')
         elif name == 'Telegram Bot' and response[code] == 400:
             log.error(
                 f'{name} 😳\n Please take the initiative bot, send a message and check if the TG_USER_ID is right or not'
             )
         elif name == 'Telegram Bot' and response[code] == 401:
             log.error(f'{name} 😳\nTG_BOT_TOKEN error')
         else:
             log.error(f'{name} 😳\n{response}')
Esempio n. 52
0
    def get_box_code(self):
        log.info('准备获取「个人中心」的兑换码...')
        id_list = []
        code_list = []
        self.header['Referer'] = f'https://ka.sina.com.cn/html5/'
        self.header['Cookie'] = self.ka_cookie
        try:
            response = req.request('get',
                CONFIG.BOX_URL, headers=self.header, allow_redirects=False)
        except Exception as e:
            log.error(e)
        else:
            if response.status_code == 200:
                response.encoding = 'utf-8'
                soup = BeautifulSoup(response.text, 'html.parser')
                # print(soup.prettify())
                boxs = soup.find_all(class_ = 'giftbag')
                for box in boxs:
                    item = {
                        'id': box.find(class_ = 'deleBtn').get('data-itemid'),
                        'title': box.find(class_ = 'title itemTitle').text,
                        'code': box.find('span').parent.contents[1]
                    }
                    log.info(f'└─☁️{item["title"]}')
                    id_list.append(item['id'])
                    code_list.append(item)
                code_list.insert(0, id_list)
            elif response.status_code == 302:
                log.error('😳 ka.sina: 登录可能失效, 尝试重新登录')
            else:
                log.error('😳 ka.sina: 兑换码获取失败')

        # 打印兑换码
        # print(req.to_json(code_list))

        log.info('「个人中心」的兑换码获取完毕')
        return code_list
Esempio n. 53
0
 def pushTemplate(self,
                  method,
                  url,
                  params=None,
                  data=None,
                  json=None,
                  headers=None,
                  **kwargs):
     name = kwargs.get('name')
     # needs = kwargs.get('needs')
     token = kwargs.get('token')
     text = kwargs.get('text')
     code = kwargs.get('code')
     if not token:
         log.info(f'{name} 🚫')
         # log.info(f'{name} 推送所需的 {needs} 未设置, 正在跳过...')
         return
     try:
         response = req.to_python(
             req.request(method, url, 2, params, data, json, headers).text)
         rspcode = response[text]
     except Exception as e:
         # 🚫: disabled; 🥳:success; 😳:fail
         log.error(f'{name} 😳\n{e}')
     else:
         if rspcode == code:
             log.info(f'{name} 🥳')
         # Telegram Bot
         elif name == 'Telegram Bot' and rspcode:
             log.info(f'{name} 🥳')
         elif name == 'Telegram Bot' and response[code] == 400:
             log.error(f'{name} 😳\n请主动给 bot 发送一条消息并检查 TG_USER_ID 是否正确')
         elif name == 'Telegram Bot' and response[code] == 401:
             log.error(f'{name} 😳\nTG_BOT_TOKEN 错误')
         else:
             log.error(f'{name} 😳\n{response}')
                message_list.append(response)
                continue
            message['total_sign_day'] = total_sign_day + 1
            message['status'] = response['message']
            message_list.append(self.message.format(**message))
        log.info('签到完毕')

        return ''.join(message_list)

    @property
    def message(self):
        return CONFIG.MESSGAE_TEMPLATE


if __name__ == '__main__':
    log.info(f'🌀原神签到小助手 {version()}')
    log.info('若签到失败, 请尝试更新!')
    log.info('任务开始')
    notify = Notify()
    msg_list = []
    ret = success_num = fail_num = 0
    """miHoYo BBS COOKIE
    :param COOKIE: 米游社的COOKIE.多个账号的COOKIE值之间用 # 号隔开,例如: 1#2#3#4
    """
    # Github Actions用户请到Repo的Settings->Secrets里设置变量,变量名字必须与上述参数变量名字完全一致,否则无效!!!
    # Name=<变量名字>,Value=<获取的值>
    COOKIE = ''

    if os.environ.get('COOKIE', '') != '':
        COOKIE = os.environ['COOKIE']
Esempio n. 55
0
File: weibo.py Progetto: fluteds/gih
    def super_sign(self):
        follow_list = self.resolve_data()
        if not follow_list:
            log.info('Remove Weibo Super Turial Sign')
            return
        for follow in follow_list:
            lv = f'[Lv.{follow["lv"]}]'
            name = follow['title_sub']
            log.info(f'Ready for {name} Overturning...')
            sleep(5)
            if follow['is_sign'] == 'Is signed in':
                log.info(f'👀 {lv}{name}: Has been signed in')
                continue
            elif follow['is_sign'] == 'Sign in':
                url = follow['sign_url']
                try:
                    response = req.to_python(
                        req.request('post', url,
                                    headers=self.get_header()).text)
                except Exception as e:
                    log.error(e)
                else:
                    if response['ok'] == 1:
                        log.info(f'🥳 {lv}{name}: Sign in success')
                    else:
                        log.info(f'😳 {lv}{name}: Sign in failure\n{response}')

        log.info('Weibo is completed')
        return
Esempio n. 56
0
File: weibo.py Progetto: fluteds/gih
                    code_list.append(item)
                code_list.insert(0, id_list)
            elif response.status_code == 302:
                log.error('😳 ka.sina: Login may fail, try to log in again')
            else:
                log.error('😳 ka.sina: Redemption code acquisition failed')

        # Print exchange code
        # print(req.to_json(code_list))

        log.info('"Personal Center" redemption code is acquired')
        return code_list


if __name__ == '__main__':
    log.info(f'🌀 Weibo Super Dismant Signing Assistant v{CONFIG.WBH_VERSION}')
    """Sina COOKIE
    :Param WB_Cookie: Cookie of Sina Weibo. Go to https://m.weibo.cn.
    :PARAM KA_COOKIE: Cookie of Sina Novice Card. Go to https://ka.sina.com.cn.
    """
    # GitHub Actions Users Please go to the settings-> secrets of the REPO to set the variable, the variable name must be completely consistent with the above parameter variable name, otherwise it will be invalid !!!
    # Name = <Variable Name>, value = <Get Value>
    WB_COOKIE = ''
    KA_COOKIE = ''

    if os.environ.get('WB_COOKIE', '') != '':
        WB_COOKIE = os.environ['WB_COOKIE']
    if os.environ.get('KA_COOKIE', '') != '':
        KA_COOKIE = os.environ['KA_COOKIE']

    if WB_COOKIE:
Esempio n. 57
0
    def getDataBatch(self, batch_size):
        """
        Generator
        - Get articles from source in batches of 10
        - check the articles for unknown field or values
        - transform the articles into flat dictionaries,
        (see description in README)
        - write the flat articles as json to a file
        with a recognizable name (see README)
        - yield the articles as batch, before the next
        batch is requested from the source
        """
        log.info("requested: {} batches"
                 .format(batch_size))

        for j in range(batch_size):

            # connect to source
            self._connect()

            # break if no hits could be found
            if self.hits == 0:
                log.info("No hits for this query!")
                break

            # now self.raw_articles is set as a list
            # of the articles, that have just been
            # fetched form the API

            # Next the articles are flattened,
            # the attributes are checked and
            # they are written to a batch
            batch = []
            for article in self.raw_articles:

                # Flatten articles
                flat_article = makeflat.make_flat_structure(article)

                # Check for unknown keys and values
                self._checkKeysAgainstSchema(flat_article)

                # Append flattened article to the batch that
                # will be served
                batch.append(flat_article)

            # The batch is written to a recognizable file
            # that is named with the query parameters

            # Derive the filename
            batchfilename = '.'.join([self.outputfilename,
                                      str(j), 'json'])
            batch_as_json = json.dumps(batch, indent=4)
            batchfile = os.path.join(
                JSON_DIR, batchfilename)

            # Then write the batch to the file
            with open(batchfile, 'w') as outfile:
                outfile.write(batch_as_json)

            # Log that the file has been writen
            log.info(u'wrote {1} articles to {0}'.format(
                batchfilename, len(batch)))

            # Log serving the batch
            log.info("Serving batch of {} articles".format(len(batch)))

            # Serve the batch
            yield batch

            # Break if all batches have been served
            if j > batch_size:
                log.info("Finished after requested batched have been served")
                break

            # Break if all query hits have been served
            if j >= self.pages:
                log.info("Finished after all hits have been served")
                break
Esempio n. 58
0
    def _connect(self):
        """connects to the API's url
        - get meta data (total hits)
        - get a batch of 10 articles
        - the program goes to sleep between API requests
        in order to avoid the API's rate limit
        """

        # sleep between request to avoid API Rate limit
        time.sleep(5)

        if hasattr(self, 'next_page'):
            self.config['page'] = self.next_page

        log.debug("next api request: {}"
                  .format(json.dumps(self.config,
                                     sort_keys=True,
                                     indent=4)))
        try:
            # connect to API
            urlparams = urllib.urlencode(self.config)
            response = urllib.urlopen(
                "https://api.nytimes.com/svc/search/v2/articlesearch.json?%s"
                % urlparams)

        except IOError:
            # connection failure
            raise IOError("Could not connect to the source.\n"
                          "Check for a working internet connection?")

        else:

            # load response into dict
            data = response.read()
            response_dict = json.loads(data)

            # check for errors
            if ('status' in response_dict and
                response_dict['status'] == 'ERROR'):

                # query field errors
                for error in response_dict['errors']:
                    log.error(" - field error: {}".format(error))
                raise IOError(
                    "A configuration error occured, see log for details")

            elif 'message' in response_dict:

                # message from API came back
                message = response_dict['message']
                log.error("An error occured: {}".format(message))
                raise IOError(message)

            elif ('status' in response_dict and
                  response_dict['status'] == 'OK'):
                # the okay case is taken care of below
                pass

            else:

                # something unknown happened
                raise IOError("4 An unknown error occured")

        # When the API call was okay:

        # The hits are only recorded at the first request
        if not hasattr(self, 'hits'):
            self.hits = response_dict['response']['meta']['hits']
            self.pages = self.hits / 10
            if self.hits:
                self.total_nr_batches = self.pages + 1
            else:
                self.total_nr_batches = 0

        # The page and content is updated on every request
        self.offset = response_dict['response']['meta']['offset']
        self.current_page = self.offset / 10
        self.next_page = self.current_page + 1
        self.raw_articles = response_dict['response']['docs']

        # The request is logged
        log.info("Successfully opened resource |"
                 " total number of hits: {} |"
                 " serving [page {}]"
                 .format(self.hits,
                         self.current_page))

        # In debug modus all found article ids are logged
        for article in self.raw_articles:
            log.debug("Article: {}"
                      .format(article['_id']))
Esempio n. 59
0
                message_list.append(response)
                return ''.join(message_list)
            message['total_sign_day'] = total_sign_day + 1
            message['status'] = response['message']
            message_list.append(self.message.format(**message))
        log.info('签到完毕')

        return ''.join(message_list)

    @property
    def message(self):
        return CONFIG.MESSAGE_TEMPLATE


if __name__ == '__main__':
    log.info(f'🌀Genshin Impact Helper v{CONFIG.GIH_VERSION}')
    log.info('If you fail to check in, please try to update!')
    log.info('任务开始')
    notify = Notify()
    msg_list = []
    ret = success_num = fail_num = 0
    """HoYoLAB Community's COOKIE
    :param OS_COOKIE: 米游社国际版的COOKIE.多个账号的COOKIE值之间用 # 号隔开,例如: 1#2#3#4
    """
    # Github Actions用户请到Repo的Settings->Secrets里设置变量,变量名字必须与上述参数变量名字完全一致,否则无效!!!
    # Name=<变量名字>,Value=<获取的值>
    OS_COOKIE = ''

    if os.environ.get('OS_COOKIE', '') != '':
        OS_COOKIE = os.environ['OS_COOKIE']
Esempio n. 60
0
def __add_msg(_reg, t, _user):
    log.info(_reg)
    if _reg.ars:
        return t.format(*[_user[v] for v in _reg.ars])
    return t