Skip to content

Commit

Permalink
Update: wuhan2020_api upgrade latest
Browse files Browse the repository at this point in the history
  • Loading branch information
arrebole committed Feb 23, 2020
1 parent d0a09a4 commit 866dfc2
Show file tree
Hide file tree
Showing 22 changed files with 485 additions and 153 deletions.
5 changes: 5 additions & 0 deletions Data/wuhan2020_api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
go.sum
main.out
*.exe
*.log
*.pyc
167 changes: 151 additions & 16 deletions Data/wuhan2020_api/README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,156 @@
# wuhan2020_data_science_api
wuhan2020 data science 数据库 API接口
comment : to avoid potential confliction with the parent project
# wuhan2020_api
病毒爬虫提交平台


基于Flask
提交json格式:

## Table of contents

+ 提交数据
+ [提交结构](#提交结构)
+ [通过python3提交](#通过python3提交)

+ 获取数据
+ [1、通过城市获取](#1、通过城市获取)
+ [2、通过省级获取](#2、通过省级获取)
+ [3、获取提交日志](#3、获取提交日志)



### 提交结构

> 接口POST http://wuhan2020.muxxs.com/api/add
文章

| 字段 | 说明 |
| ------------- | -------- |
| announce_type | 默认 0 |
| city | 市级名 |
| content | 主要内容 |
| link | 原文链接 |
| links_to_pic | 图片链接 |
| province | 省名 |
| publish_date | 发布日期 |
| publish_time | 发布时间 |
| title | 标题 |
| uploader | 提交者 |



### 通过python3提交

> title重复会被判断为已存在
```python
import json
from urllib import request

headers = {'Content-Type': 'application/json'}

postdata = {
'uploader': '风行', # 提交用户名, 用于记录
'province': "test",
'city': 'test',
'publish_time': '00:00:00',
'publish_date': '0',
'title': 'teeeeeeeeeee',
'content': "测试测试测试",
'link': "http://wsjkw.hebei.gov.cn/content/content_45/395747.whtml",
'links_to_pic': '0',
'announce_type': 0
}

req = request.Request(
url="http://wuhan2020.muxxs.com/api/add",
headers=headers,
data=json.dumps(data, ensure_ascii=False).encode("UTF-8")
)

res = request.urlopen(req)
print(res.read().decode("utf-8"))
```





### 获取数据

#### 1、通过城市获取

>GET http://wuhan2020.muxxs.com/api/read?city=<城市名>
例如

GET http://wuhan2020.muxxs.com/api/read?city=test

```json
{
"code": 0,
"message": "success",
"data": [
{
"announce_type": "0",
"city": "test",
"content": "ttttttttttttttttttttt",
"link": "http://test.test",
"links_to_pic": "teeeeeeessst",
"province": "test",
"publish_date": "0000-00-00",
"publish_time": "00:00:00",
"title": "test"
}
]
}
```
data = {
'province': "",
'city': '0',
'publish_time': '00:00:00',
'publish_date': 0,
'title': 0,
'content': 0,
'link': 0,
'links_to_pic': 0,
'announce_type': 0
}

#### 2、通过省级获取

> GET http://wuhan2020.muxxs.com/api/read?province=<省级名>
>
> 例如
>
> GET http://wuhan2020.muxxs.com/api/read?province=test
```json
{
"code": 0,
"message": "success",
"data": [
{
"announce_type": "0",
"city": "test",
"content": "ttttttttttttttttttttt",
"link": "http://test.test",
"links_to_pic": "teeeeeeessst",
"province": "test",
"publish_date": "0000-00-00",
"publish_time": "00:00:00",
"title": "test"
}
]
}
```



#### 3、获取提交日志

> GET http://wuhan2020.muxxs.com/api/logs?limit=1
```json
{
"code": 0,
"message": "成功",
"data": [
{
"city": "上海",
"ip": "120.227.30.249",
"province": "上海",
"time": "2020.02.10-19:10:44",
"uploader": "BeiTown"
}
]
}
```

11 changes: 0 additions & 11 deletions Data/wuhan2020_api/app.py

This file was deleted.

4 changes: 0 additions & 4 deletions Data/wuhan2020_api/control/__init__.py

This file was deleted.

10 changes: 0 additions & 10 deletions Data/wuhan2020_api/control/read.py

This file was deleted.

12 changes: 0 additions & 12 deletions Data/wuhan2020_api/control/write.py

This file was deleted.

37 changes: 37 additions & 0 deletions Data/wuhan2020_api/controllers/addctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package controllers

import (
"encoding/json"
"net/http"

"github.com/arrebole/wuhan2020_api/model"
"github.com/arrebole/wuhan2020_api/service"
)

// AddCtl ...
func AddCtl(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

if r.Method != "POST" {
w.Write(model.FailResp("Only post method allowed"))
return
}

var postdata model.PostData
err := json.NewDecoder(r.Body).Decode(&postdata)
if err != nil {
w.Write(model.FailResp("Type error"))
return
}

if !postdata.IsLegal() {
w.Write(model.FailResp("Missing data field"))
return
}
if service.SaveArchive(postdata.GetArchive()) == 1 {
w.Write(model.SuccessResp("update success"))
return
}
service.SaveLog(postdata.GetLog(r.RemoteAddr))
w.Write(model.SuccessResp("add success"))
}
24 changes: 24 additions & 0 deletions Data/wuhan2020_api/controllers/logsctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import (
"net/http"
"strconv"

"github.com/arrebole/wuhan2020_api/model"
"github.com/arrebole/wuhan2020_api/service"
)

// LogsCtl ...
func LogsCtl(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

limit := r.FormValue("limit")
if limit == "" {
limit = "10"
}
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 10
}
w.Write(model.LogResp(service.GetLogs(limitInt)))
}
24 changes: 24 additions & 0 deletions Data/wuhan2020_api/controllers/readctl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package controllers

import (
"net/http"

"github.com/arrebole/wuhan2020_api/model"
"github.com/arrebole/wuhan2020_api/service"
)

// ReadCtl ...
func ReadCtl(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

city, province := r.FormValue("city"), r.FormValue("province")
if city == "" && province == "" {
w.Write(model.FailResp("miss query city or province"))
return
}
if province != "" {
w.Write(model.ArchivesResp(service.GetArchivesByProvince(province)))
return
}
w.Write(model.ArchivesResp(service.GetArchivesByCity(city)))
}
5 changes: 5 additions & 0 deletions Data/wuhan2020_api/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/arrebole/wuhan2020_api

go 1.13

require github.com/jinzhu/gorm v1.9.12
14 changes: 14 additions & 0 deletions Data/wuhan2020_api/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"net/http"

"github.com/arrebole/wuhan2020_api/controllers"
)

func main() {
http.HandleFunc("/api/add", controllers.AddCtl)
http.HandleFunc("/api/read", controllers.ReadCtl)
http.HandleFunc("/api/logs", controllers.LogsCtl)
http.ListenAndServe("0.0.0.0:80", nil)
}
47 changes: 47 additions & 0 deletions Data/wuhan2020_api/model/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package model

import "time"

// PostData ...
type PostData struct {
Uploader string `json:"uploader"`
Archive
}

// Archive ...
type Archive struct {
ID uint `json:"id" gorm:"primary_key" `
AnnounceType int `json:"announce_type"`
City string `json:"city"`
Content string `json:"content"`
Link string `json:"link"`
LinksToPic string `json:"links_to_pic"`
Province string `json:"province"`
PublishDate string `json:"publish_date"`
PublishTime string `json:"publish_time"`
Title string `json:"title"`
}

// GetLog ...
func (p PostData) GetLog(ip string) *Log {
return &Log{
IP: ip,
Time: time.Now().Format("2006-01-02 15:04:05"),
City: p.City,
Province: p.Province,
Uploader: p.Uploader,
}
}

// GetArchive ...
func (p PostData) GetArchive() *Archive {
return &p.Archive
}

// IsLegal ...
func (p PostData) IsLegal() bool {
if p.Title == "" || p.City == "" || p.Province == "" || p.Content == "" || p.Uploader == "" {
return false
}
return true
}
11 changes: 11 additions & 0 deletions Data/wuhan2020_api/model/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

// Log ...
type Log struct {
ID uint `json:"id" gorm:"primary_key" `
City string `json:"city"`
IP string `json:"ip"`
Province string `json:"province"`
Time string `json:"time"`
Uploader string `json:"uploader"`
}
Loading

0 comments on commit 866dfc2

Please sign in to comment.