Package: bagbag
bagbagAn all in one python libraryInstallpip3installbagbag--upgradeDockerdockerrun--rm--namebagbag-v/path/to/file/run.py:/app/run.pydarren2046/bagbag:latest
dockerrun--rm--namebagbag-v/path/to/file/run.py:/app/run.pydarren2046/bagbag-gui:latest# xvfb running so can use gui application such as chromedriver with seleniumdockerrun--rm--namebagbag-v/path/to/file/run.py:/app/run.pydarren2046/bagbag-gui-debug:latest# HTTP Server serving vnc desktop runing on port 80LibraryCryptoAES(key:str, mode:str="cfb")Encrypt(raw:str) -> strDecrypt(enc:str) -> strFile(path:str)Write(data:str)Append(data:str)Lg 日志模块Lg.SetLevel(level:日志级别:str)Lg.SetFile(path:日志路径:str, size:文件大小，MB:int, during:日志保留时间，天:int, color:是否带ANSI颜色:bool=True, json:是否格式化为json:bool=False)Lg.Debug(message:str)Lg.Trace(message:str)Lg.Info(message:str)Lg.Warn(message:str)Lg.Error(message:str)String(string:str) 一些字符串处理函数HasChinese() -> bool 是否包含中文Language() -> str 语言Repr() -> strSimplifiedChineseToTraditional() -> strTraditionalChineseToSimplified() -> strOmmit(length:int) -> strFilter(chars:str="1234567890qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM") -> strLen() -> intIsIPAddress() -> boolTime 时间Strftime(timestamp:float|int, format:str="%Y-%m-%d %H:%M:%S") -> strStrptime(timestring:str, format:str=None) -> intBase64Encode(s:str|bytes) -> strDecode(s:str) -> str|bytesJsonDumps(obj, indent=4, ensure_ascii=False) -> strLoads(s:str) -> list | dictExtraValueByKey(obj:list|dict, key:str) -> listHashMd5sum(string:str) -> strMd5sumFile(fpath:str, block_size=2**20) -> strSha256sum(data:str|bytes) -> strSha256sumFile(fpath:str, block_size=2**20) -> strOsExit(num:int=0)Mkdir(path:str)Getenv(varname:str, defaultValue:str=None) -> str | NoneListDir(path:str) -> list[str]Unlink(path:str)Move(src:str, dst:str, force:bool=True)Copy(src:str, dst:str, force:bool=True)PathBasedir(path:str) -> strJoin(*path) -> strExists(path:str) -> boolUniquify(path:str) -> strIsDir(path:str) -> boolBasename(path:str) -> strHttpHead(url:str, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)Get(url:str, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None,  TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)PostRaw(url:str, Data:str, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)PostJson(url:str, Json:dict,Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)PostForm(url:str, Data:dict, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)Delete(url:str, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)PutForm(url:str, Data:dict,Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)PutRaw(url:str, Data:str, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False, Debug:bool=False)PutJson(url:str, Json:dict, Timeout:str=None, ReadBodySize:int=None, FollowRedirect:bool=True, HttpProxy:str=None, TimeoutRetryTimes:int=0, InsecureSkipVerify:int=False,Debug:bool=False)SocketTCPListen(host:str, port:int, waitQueue:int=5)Accept() -> Chan[StreamConnection]AcceptOne() -> StreamConnectionConnect(host:str, port:int) -> StreamConnectionPeerAddress() -> TCPPeerAddressSend(data:str)SendBytes(data:bytes)Recv(length:int) -> strRecvBytes(length:int) -> bytesClose()RandomInt(min:int, max:int) -> intChoice(obj:list|str) -> AnyString(length:int, charset:str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -> strShuffle(li:list) -> listFuncsMarkdown2Html(text:str) -> strWget(url:str, dest:str=None, override=True)IP2Int(ip:str) -> intInt2IP(intip:int) -> strResizeImage(src:str, dst:str, width:int, quality:int=95)UUID() -> strCutSentence(sentence:str, filter:bool=True) -> list[str]Tools 一些工具CacheLRU(size:int) -> dictFIFO(size:int) -> dictLFU(size:int) -> dictMRU(size:int) -> dictRR(size:int) -> dictTTL(size:int) -> dictOCR(server:str)Recognition(fpath:str, lang:str="ch") -> ocrResultSaveImage(fpath:str)WebCrawler()Run(self, url:str) -> typing.Iterable[WebCrawlerResult]JavaScriptEval(code:str)BlockChainTronTronClient(fullNodeServer:str)Block(blockNumber:int) -> tronBlockTranscations() -> list[tronTranscation]TronContract(address:str)Info()Address()TronAsset(name:str)Info()Name()BinanceOfficalAccountVertifyTwitter(account:str, waiteOnRateLimit:bool=True) -> boolGetPrice(pair:str|list=None) -> CoinsPairPrice | list[CoinsPairPrice]TwitterEssential(bearerToken:str)Search(keyword:str, sinceID:int=None, tweetPerRequest:int=10) -> typing.Iterable[twitterTweet]Timeline(screename:str, sinceID:int=None, tweetPerRequest:int=10) -> typing.Iterable[twitterTweet]Elevated(consumer_key:str, consumer_secret:str)Search(keyword:str, days:int=7) -> typing.Iterable[twitterTweet]Timeline(screename:str) -> typing.Iterable[twitterTweet]Followers(screename:str) -> typing.Iterable[twitterUser]Nslookup(server:list[str]=["8.8.8.8", "1.1.1.1", "8.8.4.4"], tcp:bool=False)A(domain:str) -> list[str]AAAA(domain:str) -> list[str]MatrixBot(apiserver:str, password:str="")SetRoom(room:str) -> MatrixBotSend(message:str)SendImage(path:str)GetMessage(num:int=10) -> list[MatrixBotMessage]Reply(message:str)ReplyImage(path:str)RSSOpml(opmlurl:str) -> list[RSSFeed]Feed(feedurl:str) -> list[RSSPage]Queue(server:str, name:str, length:int=0, timeout:int=300)QueueConfirm(name:str, length:int=0, timeout:int=300) -> queueQueueConfirmPut(item:typing.Any, force:bool=False)Get(self) -> typing.Tuple[str, typing.Any]Done(tid:str)Size(self) -> intKafka(topic:str, servers:str|list)Producer(value_serializer=lambda m: json.dumps(m).encode()) -> KafkaProducerSend(data:dict)Consumer(group_id:str=None, auto_offset_reset:str='earliest') -> KafkaConsumerGet() -> dictGithub(token:str, ratelimit:str="30/m")Search(pattern:str) -> GithubSearchResultsGet() -> GithubSearchResult | NoneSSH(host:str, port:int=None, user:str=None, password:str=None, pkey:str=None)GetOutput(command:str) -> strClose()Upload(localpath:str, remotepath:str=None)Download(remotepath:str, localpath:str=None)FileInfo(filepath:str)ListDir(dirpath:str=".") -> dictTranslaterBaidu(appid:str, secretkey:str)SetLang(To:str="zh", From:str="auto") -> BaiduTranslate(text:str) -> dictGoogle(httpProxy:str=None)SetLang(To:str="zh-CN", From:str="auto") -> GoogleTranslate(text:str, format:str="html") -> strXPath(html:str)Find(xpath:str) -> XPath | NoneAttribute(name:str) -> str | NoneText() -> strHtml() -> strWaitGroup()Add()Done()Wait()Crontab()Every(interval: int = 1) -> CrontabSecond() -> CrontabMinute() -> CrontabHour() -> CrontabDay() -> CrontabWeek() -> CrontabAt(time: str) -> CrontabDo(job_func, *args, **kwargs)Monday()Tuesday()Wednesday()Thursday()Friday()Saturday()Sunday()Elasticsearch(url:str)Delete(IndexName:str)Collection(IndexName:str)Index(id:int, data:dict, refresh:bool=False, Timeout:int=15)Refresh(Timeout:int=15)Delete(id:int)Search(key:str, value:str, page:int=1, pagesize:int=50, OrderByKey:str=None, OrderByOrder:str="ase", Highlight:str=None, mustIncludeAllWords:bool=True)CSVReader(fpath:str)Read() -> dictClose()Writer(fpath:str, mode:str="w")SetHeaders(*headers)Write(row:dict[str])Close()Flush()XlsxReader(fpath:str)Read() -> dictClose()Writer(fpath:str, mode:str="w")SetHeaders(*headers)Write(row:dict[str])Close()Flush()WebServer(name:str=None) # 例子见源码文件Web.py的后半部分Run(host:str, port:int, block:bool=True) # 监听HTTP服务Route: (path:str, methods:list=["GET", "HEAD", "OPTIONS"]) # 例子见Web.py文件, 是一个装饰器Request()Method() -> str # 请求的HTTP方法Json() -> dict | list # 格式化请求的post内容为jsonData() -> str # post的http的bodyForm()Get(name:str, default:str="") -> str | None # 获取表单的数据Args()Get(name:str, default:str="") -> str | None # 获取URL的参数Chan() 内存队列, 跟go的chan一样RateLimit(rate:str, sleep:bool=True) rate可以是 次数/时间区间, 时间可以是s, m, h, d, 即秒,分,时,天. 例如一分钟限制五次: 5/m. 在低速率的时候能限制准确, 例如低于1秒10次. 高速率例如每秒50次以上, 实际速率会降低, 速率越高降低越多.Take() sleep=True的时候会添加一个sleep, 可以把请求平均在时间段内. 在低速率的时候能限制准确. 高速率例如每秒50次以上, 实际速率会降低, 速率越高降低越多. sleep=False的时候没有sleep, 会全在一开始扔出去, 然后block住, 等下一个周期, 在需要速率很高的时候可以这样, 例如发包的时候, 一秒限制2000个包这样.URL(url:str)Parse() -> URLParseResultEncode() -> strDecode() -> strPrometheusMetricServer(listen:str="0.0.0.0", port:int=9105)PushGateway(address:str, job:str, pushinterval:int=15, instance:str=None)NewCounter(name:str, help:str) -> prometheusCounterAdd(num:int|float=1)NewCounterWithLabel(name:str, labels:list[str], help:str) -> prometheusCounterVecAdd(labels:dict|list, num:int|float=1)NewGauge(name:str, help:str) -> prometheusGaugeSet(num:int|float)NewGaugeWithLabel(name:str, labels:list[str], help:str) -> prometheusGaugeVecSet(labels:dict|list, num:int|float=1)SeleniumFirefox(seleniumServer:str=None, PACFileURL:str=None, sessionID:str=None)Chrome(seleniumServer:str=None, httpProxy:str=None, sessionID=None)Except(*xpath:str, timeout:int=30) -> int | NoneResizeWindow(width:int, height:int)ScrollRight(pixel:int)ScrollLeft(pixel:int)ScrollUp(pixel:int)ScrollDown(pixel:int)Url() -> strCookie() -> list[dict]SetCookie(cookie_dict:dict)Refresh()GetSession() -> strGet(url:str)PageSource() -> strTitle() -> strClose()SwitchTabByID(number:int)SwitchTabByIdent(ident:str)Tabs() -> list[str]NewTab() -> strFind(xpath:str, timeout:int=60, scrollIntoElement:bool=True) -> SeleniumElementClear() -> SeleniumElementClick() -> SeleniumElementText() -> strAttribute(name:str) -> strInput(string:str) -> SeleniumElementSubmit() -> SeleniumElementPressEnter() -> SeleniumElementScrollIntoElement() -> SeleniumElementTelegram(appid:str, apphash:str, sessionString:str=None)SessionString() -> strResolvePeerByUsername(username:str) -> TelegramPeer | NonePeerByIDAndHash(ID:int, Hash:int, Type:str="channel") -> TelegramPeer | NoneResolve() # 如果手动根据ID初始化一个TelegramPeer实例, 调用这个函数可以补全这个ID对应的Peer的信息SendMessage(message:str)Messages(limit:int=100, offset:int=0) -> list[TelegramMessage]Message(id:str) -> TelegramMessageRefresh() -> TelegramMessage # 有时候同一个id, 被编辑了, 刷新一下返回最新的消息ClickButton(buttonText:str) -> boolDelete()TelegramBotOfficial(token:str)GetMe() -> telebot.types.UserSetChatID(chatid:int) -> TelegramBotSetTags(*tags:str) -> TelegramBotSendFile(path:str)SendImage(path:str)SendVideo(path:str)SendAudio(path:str)SendLocation(latitude:float, longitude:float)SendMsg(msg:str, *tags:str)ProgressBar(iterable_obj, total=None, title=None, leave=False)Redis(host: str, port: int = 6379, database: int = 0, password: str = "")Set(key:str, value:str, ttl:int=None) -> (bool | None)Get(key:str) -> (str | None)Del(key:str) -> intLock(key:str) -> RedisLockAcquire()Release()Queue(key:str) -> RedisQueueSize() -> intPut(item:str)Get(block=True, timeout=None) -> strMySQL(host: str, port: int, user: str, password: str, database: str, prefix:str = "") # 跟5.7兼容. 因为orator跟5.7兼容, 跟8.0会有小问题, 作者很久不更新, 有空换掉这个orm.注意, Python的MySQL操作不支持多线程, 需要每个线程连接一次MySQL, 不过这个是自动的, 在Get, Update等操作的时候如果链接异常就重连SQLite(path: str, prefix:str = "")由于SQLite每次只能一个线程进行操作, 所以这里默认会有一个锁, 线程安全Queue(tbname:str, size:int=None) -> NamedQueueSize() -> intGet(wait=True) -> AnyPut(string:Any)QueueConfirm(tbname:str, size:int=None, timeout:int=900) -> NamedConfirmQueueSize() -> intSizeStarted() -> intSizeTotal() -> intGet(wait=True) -> typing.Tuple[int, typing.Any]Put(item:typing.Any)Done(id:int)Execute(sql: str) -> (bool | int | list)Tables() -> listTable(tbname: str) -> MySQLSQLiteTableAddColumn(colname: str, coltype: str, default=None, nullable:bool = True) -> MySQLSQLiteTableAddIndex(*cols: str) -> MySQLSQLiteTableFields(*cols: str) -> MySQLSQLiteTableWhere(key:str, opera:str, value:str) -> MySQLSQLiteTableWhereIn(key:str, value: list) -> MySQLSQLiteTableWhereNotIn(key:str, value: list) -> MySQLSQLiteTableWhereNull(key:str) -> MySQLSQLiteTableWhereNotNull(key:str) -> MySQLSQLiteTableWhereBetween(key:str, start:int|float|str, end:int|float|str) -> MySQLSQLiteTableWhereNotBetween(key:str, start:int|float|str, end:int|float|str) -> MySQLSQLiteTableOrWhere(key:str, opera:str, value:str) -> MySQLSQLiteTableOrWhereIn(key:str, value: list) -> MySQLSQLiteTableOrderBy(*key:str) -> MySQLSQLiteTableLimit(num:int) -> MySQLSQLiteTablePaginate(size:int, page:int) -> MySQLSQLiteTableData(value:map) -> MySQLSQLiteTableOffset(num:int) -> MySQLSQLiteTableInsert()Update()Delete()InsertGetID() -> intExists() -> boolCount() -> intFind(id:int) -> mapFirst() -> mapGet() -> listColumns() -> list[map]KeyValue(tbname:str)Get(key:str) -> AnySet(key:str, value:Any)Del(key:str)Keys() -> list[str]其它的Thread(func, *args:Any, daemon:bool=True) -> threading.Thread # 启动线程, daemon=TrueProcess(func, *args:Any, daemon:bool=True) -> multiprocessing.Process # 启动进程, daemon=True