Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions examples/python/create_table_from_mysql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from seatable_api.constants import ColumnTypes
from seatable_api import Base, context
from sqlalchemy import create_engine

TABLE_NAME = 'Table_1'
SERVER_URL = context.server_url or 'https://cloud.seatable.cn/'
API_TOKEN = context.api_token or 'cacc42497886e4d0aa8ac0531bdcccb1c93bd0f5'


class DBConnection():
def __init__(self, db_instance):
self.db_engine = create_engine("mysql+mysqlconnector://username:password@localhost:8002/{}?charset=utf8mb4".format(db_instance))
self.db_engine.connect()

def read(self, sql): #返回[{}]
"""Executes a read query and returns a list of dicts, whose keys are column names."""
ret = self.db_engine.execute(sql).fetchall()
# return list of dict instead of tuple
if ret is not None:
return [{key: value for key, value in row.items()} for row in ret if row is not None]
else:
return [{}]

def write(self,sql):
return


class Seatable:

def __init__(self):
"""初始化登录接口
"""
self.base = Base(API_TOKEN, SERVER_URL)
self.base.auth()


def create_cols(self,dbname,sql):
"""创建表头(非必需)
"""
conn = DBConnection(dbname)
# describe 表属性来建seatable表头
mysql_data = conn.read(sql)
try:
for data in mysql_data:
# 默认都插入text类型,有需要可以自己根据field字段配置
self.base.insert_column(TABLE_NAME, data['Field'], ColumnTypes.TEXT )
return 'success'
except:
return 'fail'

if __name__ == "__main__":
s = Seatable()
DB = 'DB'
sql = 'describe tables;'
s.create_cols(DB,sql)
64 changes: 32 additions & 32 deletions examples/python/image_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@
SERVER_URL = context.server_url or 'https://cloud.seatable.cn/'
API_TOKEN = context.api_token or 'cacc42497886e4d0aa8ac0531bdcccb1c93bd0f5'
TABLE_NAME = 'Table1'
IMAGE_FILE_TYPE = ['jpg', 'png', 'jpeg', 'bmp', 'gif'] # 图片的格式

IMG_URL_COL = '图片链接' # 包含图片链接的列名,需要是 URL 或者文本类型
IMG_COL = 'img' # 用于存储图片的列名,需要是图片类型
IMAGE_FILE_TYPE = ['jpg', 'png', 'jpeg', 'bmp', 'gif','webp'] # 图片的格式

IMG_URL_DICT = {'图片链接':'图片','图标链接':'图标'} # 包含图片链接的列名,需要是 URL 或者文本类型 ,多列情况下{链接:存储图片的列名}
IMG_NAME_PRE = 'image' # 图片上传后使用的文件名称前缀
###################---基本信息配置---###################

Expand Down Expand Up @@ -57,35 +55,37 @@ def img_transfer():
count = 0
#3. 遍历每一行,获取‘图片链接‘列的信息
for row in rows:
time_stamp = get_time_stamp()
img_url = row.get(IMG_URL_COL, None)
img = row.get(IMG_COL, None)
try:
#若无图片链接或者img列有数据的话跳过,防止重复添加
if (not img_url) or img:
continue
#通过url链接获取文件扩展名
img_name_extend = img_url.strip().split('.')[-1]
img_name_extend = img_name_extend in IMAGE_FILE_TYPE and img_name_extend or 'jpg'
#通过uuid对下载的文件进行重命名 IMG_NAME_PRE + 时间戳 + 扩展名
img_name = "/tmp/image-%s.%s"%(time_stamp, img_name_extend)
#下载文件
response = requests.get(img_url)
if response.status_code != 200:
raise Exception('download file error')
with open(img_name, 'wb') as f:
f.write(response.content)
#文件上传
info_dict = base.upload_local_file(img_name, name=None, relative_path=None, file_type='image', replace=True)
row[IMG_COL] = [info_dict.get('url')]
base.update_row('Table1', row['_id'], row)
#上传完成之后删除
os.remove(img_name)
for IMG_URL_COL in IMG_URL_DICT.keys():
IMG_COL = IMG_URL_DICT[IMG_URL_COL]
time_stamp = get_time_stamp()
img_url = row.get(IMG_URL_COL, None)
img = row.get(IMG_COL, None)
try:
#若无图片链接或者img列有数据的话跳过,防止重复添加
if (not img_url) or img:
continue
#通过url链接获取文件扩展名
img_name_extend = img_url.strip().split('.')[-1]
img_name_extend = img_name_extend in IMAGE_FILE_TYPE and img_name_extend or 'jpg'
#通过uuid对下载的文件进行重命名 IMG_NAME_PRE + 时间戳 + 扩展名
img_name = "/tmp/image-%s.%s"%(time_stamp, img_name_extend)
#下载文件
response = requests.get(img_url)
if response.status_code != 200:
raise Exception('download file error')
with open(img_name, 'wb') as f:
f.write(response.content)
#文件上传
info_dict = base.upload_local_file(img_name, name=None, relative_path=None, file_type='image', replace=True)
row[IMG_COL] = [info_dict.get('url')]
base.update_row('Table1', row['_id'], row)
#上传完成之后删除
os.remove(img_name)

except Exception as err_msg:
print('count%s-%s-%s-message: %s' % (count, row['_id'], img_url, err_msg)) #发现异常打印行数等信息方便回查
continue
count += 1
except Exception as err_msg:
print('count%s-%s-%s-message: %s' % (count, row['_id'], img_url, err_msg)) #发现异常打印行数等信息方便回查
continue
count += 1

if __name__ == "__main__":
img_transfer()
Expand Down