# 0314 資料庫-2 共筆
## Firebase
### 初始化
1. 建立專案
2. 在 Console 選擇 Cloud Firestore
3. 建立資料庫
4. 以**測試**模式啟動
### 取得 Token
1. 進入以下網址:
https://console.firebase.google.com/u/0/project
2. 選擇左上角**專案總覽**按鈕右方的齒輪
3. 選取專案設定
4. 服務帳戶 -> Python -> 產生新的私密金鑰
5. 將下載下來的檔案重新命名為 **serviceAccount.json**
6. 將 serviceAccount.json 與 Python 檔案置於**同個資料夾**內
### 手動新增資料
1. 新建集合,名稱部分輸入 users
2. 如下圖所示新增欄位

## Python
### 安裝套件
```batch
pip install firebase_admin
```
### 引入標頭檔
```python
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
import time, os, json
```
### 身分驗證
```python=
os.chdir(os.path.dirname(os.path.realpath(__file__)))
cred = credentials.Certificate('serviceAccount.json')
app = firebase_admin.initialize_app(cred)
db = firestore.client()
```
### 讀取資料
```python=
collection_ref = db.collection("users")
docs = collection_ref.stream()
for doc in docs:
print(f"{doc.id} -> {doc.to_dict()}")
```
### 新增資料
```python=
document_ref = db.collection("users").document("user")
document_ref.set({
'id': '0',
'name': 'user',
})
```
### 修改資料
```python=
document_ref = db.collection("users").document("admin")
document_ref.set({
'id': '1',
'name': 'admin',
})
```
```python=
document_ref = db.collection("users").document("admin")
document_ref.update({
'name': 'myName',
})
```
### 刪除資料
```python=
document_ref = db.collection("users").document("admin")
document_ref.delete()
```
## 來點複雜的
:::spoiler
```json
{
"success": "true",
"result": {
"resource_id": "F-D0047-091",
"fields": [
{
"id": "contentDescription",
"type": "String"
},
{
"id": "datasetDescription",
"type": "String"
},
{
"id": "locationsName",
"type": "String"
},
{
"id": "dataid",
"type": "String"
},
{
"id": "locationName",
"type": "String"
},
{
"id": "geocode",
"type": "Double"
},
{
"id": "lat",
"type": "Double"
},
{
"id": "lon",
"type": "Double"
},
{
"id": "elementName",
"type": "String"
},
{
"id": "description",
"type": "String"
},
{
"id": "startTime",
"type": "Timestamp"
},
{
"id": "endTime",
"type": "Timestamp"
},
{
"id": "dataTime",
"type": "Timestamp"
},
{
"id": "value",
"type": "String"
},
{
"id": "measures",
"type": "String"
}
]
},
"records": {
"locations": [
{
"datasetDescription": "臺灣各縣市鄉鎮未來1週逐12小時天氣預報",
"locationsName": "台灣",
"dataid": "D0047-091",
"location": [
{
"locationName": "臺北市",
"geocode": "63000000",
"lat": "25.035095",
"lon": "121.558742",
"weatherElement": [
{
"elementName": "MinT",
"description": "最低溫度",
"time": [
{
"startTime": "2023-03-05 12:00:00",
"endTime": "2023-03-05 18:00:00",
"elementValue": [
{
"value": "18",
"measures": "攝氏度"
}
]
},
{
"startTime": "2023-03-05 18:00:00",
"endTime": "2023-03-06 06:00:00",
"elementValue": [
{
"value": "13",
"measures": "攝氏度"
}
]
}
]
},
{
"elementName": "UVI",
"description": "紫外線指數",
"time": [
{
"startTime": "2023-03-05 12:00:00",
"endTime": "2023-03-05 18:00:00",
"elementValue": [
{
"value": "6",
"measures": "紫外線指數"
},
{
"value": "高量級",
"measures": "曝曬級數"
}
]
}
]
},
{
"elementName": "MaxT",
"description": "最高溫度",
"time": [
{
"startTime": "2023-03-05 12:00:00",
"endTime": "2023-03-05 18:00:00",
"elementValue": [
{
"value": "22",
"measures": "攝氏度"
}
]
},
{
"startTime": "2023-03-05 18:00:00",
"endTime": "2023-03-06 06:00:00",
"elementValue": [
{
"value": "18",
"measures": "攝氏度"
}
]
}
]
}
]
}
]
}
]
}
}
```
:::
```python=
document_ref = db.collection("weathers").document("taipei")
weatherData = json.loads('''上面那一串''')
document_ref.set(weatherData)
```
## API Request
氣象資料開放平台: https://opendata.cwb.gov.tw/index
Swagger: https://opendata.cwb.gov.tw/dist/opendata-swagger.html
Json Viewer: https://jsoneditoronline.org/