mdbq 1.8.0__py3-none-any.whl → 1.8.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mdbq/other/sku_picture.py
CHANGED
@@ -5,6 +5,7 @@ import json
|
|
5
5
|
import os
|
6
6
|
import platform
|
7
7
|
import random
|
8
|
+
from dateutil.relativedelta import relativedelta
|
8
9
|
import re
|
9
10
|
import time
|
10
11
|
import warnings
|
@@ -18,6 +19,15 @@ from selenium.webdriver.chrome.service import Service
|
|
18
19
|
from mdbq.config import set_support
|
19
20
|
from mdbq.config import get_myconf
|
20
21
|
from mdbq.mysql import mysql
|
22
|
+
from mdbq.mysql import s_query
|
23
|
+
import ua_sj
|
24
|
+
import requests
|
25
|
+
from openpyxl import load_workbook
|
26
|
+
# from openpyxl.drawing.image import Image
|
27
|
+
import openpyxl
|
28
|
+
from PIL import Image as PILImage
|
29
|
+
from io import BytesIO
|
30
|
+
from openpyxl.utils import get_column_letter
|
21
31
|
|
22
32
|
warnings.filterwarnings('ignore')
|
23
33
|
|
@@ -444,16 +454,86 @@ class SkuPicture:
|
|
444
454
|
|
445
455
|
if results:
|
446
456
|
self.df = pd.concat(results)
|
457
|
+
self.df = self.df[['sku图片链接'] != '0']
|
447
458
|
|
448
459
|
def read_df(self):
|
449
460
|
path = os.path.join(self.path, self.filename)
|
450
461
|
df = pd.read_excel(path, header=0)
|
451
462
|
df = df[['商品id', '商家编码', '是否新增']]
|
463
|
+
df['是否新增'].fillna(0, inplace=True)
|
452
464
|
df = df.astype({'是否新增': int})
|
453
465
|
df = df[df['是否新增'] == 1]
|
454
466
|
self.urls = df.to_dict('records')
|
455
467
|
|
456
468
|
|
469
|
+
class DownloadPicture():
|
470
|
+
"""
|
471
|
+
从数据库中下载数据
|
472
|
+
"""
|
473
|
+
def __init__(self, service_name):
|
474
|
+
# target_service 从哪个服务器下载数据
|
475
|
+
self.months = 0 # 下载几个月数据, 0 表示当月, 1 是上月 1 号至今
|
476
|
+
# 实例化一个下载类
|
477
|
+
username, password, host, port = get_myconf.select_config_values(target_service=service_name,
|
478
|
+
database='mysql')
|
479
|
+
self.download = s_query.QueryDatas(username=username, password=password, host=host, port=port)
|
480
|
+
self.df = pd.DataFrame()
|
481
|
+
self.headers = {'User-Agent': ua_sj.get_ua()}
|
482
|
+
self.save_path = '/Users/xigua/Downloads/sku图片链接'
|
483
|
+
self.filename = ''
|
484
|
+
if not os.path.exists(self.save_path):
|
485
|
+
os.mkdir(self.save_path)
|
486
|
+
|
487
|
+
def get_df_from_service(self):
|
488
|
+
start_date, end_date = self.months_data(num=self.months)
|
489
|
+
projection = {
|
490
|
+
'商品id': 1,
|
491
|
+
'商家编码': 1,
|
492
|
+
'sku编码': 1,
|
493
|
+
'sku名称': 1,
|
494
|
+
'sku图片链接': 1
|
495
|
+
}
|
496
|
+
self.df = self.download.data_to_df(
|
497
|
+
db_name='属性设置2',
|
498
|
+
table_name='天猫商品sku信息',
|
499
|
+
start_date=start_date,
|
500
|
+
end_date=end_date,
|
501
|
+
projection=projection,
|
502
|
+
)
|
503
|
+
|
504
|
+
def download_data(self):
|
505
|
+
dict_data = self.df.to_dict('records')
|
506
|
+
num = len(dict_data)
|
507
|
+
i = 0
|
508
|
+
for data in dict_data:
|
509
|
+
url = data['sku图片链接']
|
510
|
+
sku_name = re.sub('/', '_', data['sku名称'])
|
511
|
+
self.filename = f'{data['商品id']}_{data['商家编码']}_{data['sku编码']}_{sku_name}.jpg'
|
512
|
+
if os.path.isfile(os.path.join(self.save_path, self.filename)):
|
513
|
+
i += 1
|
514
|
+
continue
|
515
|
+
if 'https' not in url:
|
516
|
+
i += 1
|
517
|
+
continue
|
518
|
+
|
519
|
+
print(f'正在下载: {i}/{num}, {data['sku编码']}')
|
520
|
+
self.headers.update({'User-Agent': ua_sj.get_ua()})
|
521
|
+
res = requests.get(url, headers=self.headers) # 下载图片到内存
|
522
|
+
# 保存图片到本地文件夹
|
523
|
+
with open(os.path.join(self.save_path, self.filename), 'wb') as f:
|
524
|
+
f.write(res.content)
|
525
|
+
i += 1
|
526
|
+
time.sleep(0.5)
|
527
|
+
|
528
|
+
@staticmethod
|
529
|
+
def months_data(num=0, end_date=None):
|
530
|
+
""" 读取近 num 个月的数据, 0 表示读取当月的数据 """
|
531
|
+
if not end_date:
|
532
|
+
end_date = datetime.datetime.now()
|
533
|
+
start_date = end_date - relativedelta(months=num) # n 月以前的今天
|
534
|
+
start_date = f'{start_date.year}-{start_date.month}-01' # 替换为 n 月以前的第一天
|
535
|
+
return pd.to_datetime(start_date), pd.to_datetime(end_date)
|
536
|
+
|
457
537
|
def main(service_name, database):
|
458
538
|
if not os.path.exists(Share_Path):
|
459
539
|
print(f'当前系统环境不支持')
|
@@ -483,5 +563,90 @@ def main(service_name, database):
|
|
483
563
|
) # 3. 回传数据库
|
484
564
|
|
485
565
|
|
566
|
+
def main2(service_name, database):
|
567
|
+
d = DownloadPicture(service_name=service_name)
|
568
|
+
d.get_df_from_service()
|
569
|
+
d.download_data()
|
570
|
+
|
571
|
+
|
572
|
+
class InsertPicture():
|
573
|
+
def __init__(self):
|
574
|
+
self.file = '/Users/xigua/Downloads/test.xlsx'
|
575
|
+
self.path = '/Users/xigua/Downloads/sku图片链接'
|
576
|
+
self.pic_datas = []
|
577
|
+
self.header = 0 # sku 的标题栏起始行数
|
578
|
+
|
579
|
+
def insert_data(self):
|
580
|
+
self.get_filename()
|
581
|
+
# sku_in_files = [item['sku'] for item in self.pic_datas]
|
582
|
+
# print(len(sku_in_files))
|
583
|
+
|
584
|
+
# df = pd.read_excel(self.file, header=self.header)
|
585
|
+
# cols = df.columns.tolist()
|
586
|
+
# print(cols)
|
587
|
+
|
588
|
+
workbook = load_workbook(self.file)
|
589
|
+
sheet = workbook.active
|
590
|
+
rows = sheet.max_row # 总行数
|
591
|
+
columns = sheet.max_column # 总列数
|
592
|
+
# print(columns)
|
593
|
+
# print(rows)
|
594
|
+
sheet.insert_cols(0, 1) # 在第0列开始插入1列空白列
|
595
|
+
sheet['A1'] = '商品图片'
|
596
|
+
|
597
|
+
for col in range(1, columns+1):
|
598
|
+
for row in range(1, rows+1):
|
599
|
+
# print(f'第{col}列, 第{row}行...')
|
600
|
+
value = sheet.cell(row=row, column=col).value
|
601
|
+
if value:
|
602
|
+
for data in self.pic_datas:
|
603
|
+
if str(value) == data['sku']:
|
604
|
+
print(value, data['文件名称'])
|
605
|
+
image_path = os.path.join(data['文件路径'], data['文件名称'])
|
606
|
+
img = PILImage.open(image_path)
|
607
|
+
output = BytesIO()
|
608
|
+
img.save(output, format='JPEG')
|
609
|
+
image_data = output.getvalue()
|
610
|
+
|
611
|
+
# 调整图片大小
|
612
|
+
img_resized = img.resize((128, 128)) # 调整为128x128大小
|
613
|
+
output_resized = BytesIO()
|
614
|
+
img_resized.save(output_resized, format='JPEG')
|
615
|
+
image_data_resized = output_resized.getvalue()
|
616
|
+
col_letter = 'A'
|
617
|
+
sheet.add_image(openpyxl.drawing.image.Image(sheet, image_data_resized), f'{col_letter}{row}')
|
618
|
+
break
|
619
|
+
|
620
|
+
# workbook.save(self.file)
|
621
|
+
# print(filenames)
|
622
|
+
# sheet.insert_cols(0, 1) # 在第0列开始插入1列空白列
|
623
|
+
# sheet['A1'] = '商品图片'
|
624
|
+
|
625
|
+
|
626
|
+
# col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
|
627
|
+
# sheet.column_dimensions[col_letter].width = 10
|
628
|
+
# sheet.row_dimensions[row].height = 80
|
629
|
+
# # sheet.cell(row=row, column=col).value = "" # 删除原内容
|
630
|
+
# now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S ")
|
631
|
+
# print(f'{now}正在转换: 第{col}列, 第{row}行..')
|
632
|
+
|
633
|
+
def get_filename(self):
|
634
|
+
for root, dirs, files in os.walk(self.path, topdown=False):
|
635
|
+
for name in files:
|
636
|
+
if name.endswith('.jpg'):
|
637
|
+
sku_id = re.findall(r'\d+_\d+_(\d+)_|\d+_\d+_(\d+-\d+)_|\d+_\d+_([A-Za-z]+\d+)_', name)
|
638
|
+
sku_id = [item for item in sku_id[0] if item != '']
|
639
|
+
self.pic_datas.append({'文件路径': root, '文件名称': name, 'sku': sku_id[0]})
|
640
|
+
|
641
|
+
|
642
|
+
def main3():
|
643
|
+
p = InsertPicture()
|
644
|
+
p.header = 1
|
645
|
+
p.insert_data()
|
646
|
+
|
647
|
+
|
648
|
+
|
486
649
|
if __name__ == '__main__':
|
487
|
-
main(service_name='company', database='mysql')
|
650
|
+
# main(service_name='company', database='mysql')
|
651
|
+
# main2(service_name='company', database='mysql')
|
652
|
+
main3()
|
@@ -30,13 +30,13 @@ mdbq/mysql/year_month_day.py,sha256=VgewoE2pJxK7ErjfviL_SMTN77ki8GVbTUcao3vFUCE,
|
|
30
30
|
mdbq/other/__init__.py,sha256=jso1oHcy6cJEfa7udS_9uO5X6kZLoPBF8l3wCYmr5dM,18
|
31
31
|
mdbq/other/porxy.py,sha256=UHfgEyXugogvXgsG68a7QouUCKaohTKKkI4RN-kYSdQ,4961
|
32
32
|
mdbq/other/pov_city.py,sha256=AEOmCOzOwyjHi9LLZWPKi6DUuSC-_M163664I52u9qw,21050
|
33
|
-
mdbq/other/sku_picture.py,sha256=
|
33
|
+
mdbq/other/sku_picture.py,sha256=sf-3i4sfk8kQUZgXMXlg6aiibw2BJF38tVr-ULUi8TI,28937
|
34
34
|
mdbq/other/ua_sj.py,sha256=JuVYzc_5QZ9s_oQSrTHVKkQv4S_7-CWx4oIKOARn_9U,22178
|
35
35
|
mdbq/pbix/__init__.py,sha256=Trtfaynu9RjoTyLLYBN2xdRxTvm_zhCniUkVTAYwcjo,24
|
36
36
|
mdbq/pbix/pbix_refresh.py,sha256=JUjKW3bNEyoMVfVfo77UhguvS5AWkixvVhDbw4_MHco,2396
|
37
37
|
mdbq/pbix/refresh_all.py,sha256=0uAnBKCd5cx5FLTkawN1GV9yi87rfyMgYal5LABtumQ,7186
|
38
38
|
mdbq/spider/__init__.py,sha256=RBMFXGy_jd1HXZhngB2T2XTvJqki8P_Fr-pBcwijnew,18
|
39
|
-
mdbq-1.8.
|
40
|
-
mdbq-1.8.
|
41
|
-
mdbq-1.8.
|
42
|
-
mdbq-1.8.
|
39
|
+
mdbq-1.8.1.dist-info/METADATA,sha256=4KLPOuQB3iLxcdwbEQ39prUCY6TNJJTnLZTwEbNNlYA,245
|
40
|
+
mdbq-1.8.1.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
|
41
|
+
mdbq-1.8.1.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
|
42
|
+
mdbq-1.8.1.dist-info/RECORD,,
|
File without changes
|
File without changes
|