Python YES!

This commit is contained in:
qcminecraft 2021-02-03 08:06:44 +08:00
parent d6cdb00c1b
commit e6251155da
13 changed files with 113 additions and 66 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
config.json
.idea/

10
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,10 @@
# 默认忽略的文件
/shelf/
/workspace.xml
# 数据源本地存储已忽略文件
/../../../../:\Workspace\x61bot\.idea/dataSources/
/dataSources.local.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
*

17
.idea/dataSources.xml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="DB" uuid="ac3a5d78-6091-49a0-aea9-ea5fe008b39a">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:C:\Workspace\x61bot\DB.sqlite3</jdbc-url>
<working-dir>$ProjectFileDir$</working-dir>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.34.0/sqlite-jdbc-3.34.0.jar</url>
</library>
</libraries>
</data-source>
</component>
</project>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/x61bot.iml" filepath="$PROJECT_DIR$/.idea/x61bot.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

10
.idea/x61bot.iml Normal file
View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/.idea/dataSources" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

BIN
DB.sqlite3 Normal file

Binary file not shown.

14
config.example.json Normal file
View File

@ -0,0 +1,14 @@
{
"misskey.io": {
"name": "misskey.io",
"url" : "https://misskey.io",
"token": "9v1t2poJeEh5RI74VqBCzJLHk9N6Weds",
"channel": "8hrcz5rdvp"
},
"misskey.dev": {
"name": "misskey.dev",
"url" : "https://misskey.dev",
"token": "qnZl1OmgnAlTRbRO",
"channel": false
}
}

View File

@ -1,5 +0,0 @@
<?php
require ("lib.php");
echo xmlToJSON(sendReq("https://rsshub.app/dwnews/yaowen/global"));

61
lib.php
View File

@ -1,61 +0,0 @@
<?php
/**
* CURL实现GET或POST请求
* @param $url 请求地址
* @param $mode 请求方式GET/POST
* @param $data 请求参数执行POST请求时需要
* @return object 返回对象数据包
*/
function sendReq($url, $data = array(), $mode = 'GET'){
// 初始化
$curl = curl_init();
// 访问的URL
curl_setopt($curl, CURLOPT_URL, $url);
// 只获取页面内容,但不输出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
// 验证是否是https请求
if(substr($url, 0, 5) == 'https'){
// https请求不验证证书
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
// https请求不验证HOST
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
if($mode == 'POST'){
// 设置请求方式为POST请求
curl_setopt($curl, CURLOPT_POST, true);
// POST请求数据
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
// 执行CURL请求
$result = curl_exec($curl);
// 关闭curl释放资源
curl_close($curl);
return $result;
}
/**
* 获取当前时间戳,精确到毫秒
* @return float
*/
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
function xmlToJSON($content){
$dir_name = "cache/".date("Y-m-d");
$cache_name = $dir_name."/".microtime_float();
if(!is_dir($dir_name)){
mkdir($dir_name);
}
$xml = $content;
file_put_contents("$cache_name".".xml", $xml);
$json = json_encode(simplexml_load_file($cache_name.".xml"));
file_put_contents("$cache_name".".json", $json);
return $json;
}

36
x61bot.py Normal file
View File

@ -0,0 +1,36 @@
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Misskey API Docs: https://misskey.io/api-doc
import json
import sqlite3
import requests
import xmltodict
import time
config_file = open('config.json', 'r')
config = json.loads(config_file.read())
config_file.close()
conn = sqlite3.connect('DB.sqlite3')
print("Opened database successfully")
def xml_to_json(xml):
pars = xmltodict.parse(xml)
return json.dumps(pars)
def spider(rule_name, rss_url):
print("Fetch: ", rss_url)
c = conn.cursor()
result = xmltodict.parse(requests.get(rss_url).content)
c.execute('INSERT INTO "main"."spider_log" ("rule_name", "rss_url", "result_json", "timestamp") '
'VALUES (?, ?, ?, ?)', (rule_name, rss_url, json.dumps(result), time.time()))
c.close()
return result
if __name__ == '__main__':
print("Misskey X61 RSS Bot")
spider("duowei", "https://rsshub.app/dwnews/yaowen/global")
conn.commit()
conn.close()