mdbq 1.8.0__py3-none-any.whl → 1.8.2__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,14 @@ 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
+
26
+ import io
27
+ from openpyxl import load_workbook
28
+ from openpyxl.drawing.image import Image
29
+ from openpyxl.utils import get_column_letter
21
30
 
22
31
  warnings.filterwarnings('ignore')
23
32
 
@@ -444,16 +453,86 @@ class SkuPicture:
444
453
 
445
454
  if results:
446
455
  self.df = pd.concat(results)
456
+ self.df = self.df[['sku图片链接'] != '0']
447
457
 
448
458
  def read_df(self):
449
459
  path = os.path.join(self.path, self.filename)
450
460
  df = pd.read_excel(path, header=0)
451
461
  df = df[['商品id', '商家编码', '是否新增']]
462
+ df['是否新增'].fillna(0, inplace=True)
452
463
  df = df.astype({'是否新增': int})
453
464
  df = df[df['是否新增'] == 1]
454
465
  self.urls = df.to_dict('records')
455
466
 
456
467
 
468
+ class DownloadPicture():
469
+ """
470
+ 从数据库中下载数据
471
+ """
472
+ def __init__(self, service_name):
473
+ # target_service 从哪个服务器下载数据
474
+ self.months = 0 # 下载几个月数据, 0 表示当月, 1 是上月 1 号至今
475
+ # 实例化一个下载类
476
+ username, password, host, port = get_myconf.select_config_values(target_service=service_name,
477
+ database='mysql')
478
+ self.download = s_query.QueryDatas(username=username, password=password, host=host, port=port)
479
+ self.df = pd.DataFrame()
480
+ self.headers = {'User-Agent': ua_sj.get_ua()}
481
+ self.save_path = '/Users/xigua/Downloads/sku图片链接'
482
+ self.filename = ''
483
+ if not os.path.exists(self.save_path):
484
+ os.mkdir(self.save_path)
485
+
486
+ def get_df_from_service(self):
487
+ start_date, end_date = self.months_data(num=self.months)
488
+ projection = {
489
+ '商品id': 1,
490
+ '商家编码': 1,
491
+ 'sku编码': 1,
492
+ 'sku名称': 1,
493
+ 'sku图片链接': 1
494
+ }
495
+ self.df = self.download.data_to_df(
496
+ db_name='属性设置2',
497
+ table_name='天猫商品sku信息',
498
+ start_date=start_date,
499
+ end_date=end_date,
500
+ projection=projection,
501
+ )
502
+
503
+ def download_data(self):
504
+ dict_data = self.df.to_dict('records')
505
+ num = len(dict_data)
506
+ i = 0
507
+ for data in dict_data:
508
+ url = data['sku图片链接']
509
+ sku_name = re.sub('/', '_', data['sku名称'])
510
+ self.filename = f'{data['商品id']}_{data['商家编码']}_{data['sku编码']}_{sku_name}.jpg'
511
+ if os.path.isfile(os.path.join(self.save_path, self.filename)):
512
+ i += 1
513
+ continue
514
+ if 'https' not in url:
515
+ i += 1
516
+ continue
517
+
518
+ print(f'正在下载: {i}/{num}, {data['sku编码']}')
519
+ self.headers.update({'User-Agent': ua_sj.get_ua()})
520
+ res = requests.get(url, headers=self.headers) # 下载图片到内存
521
+ # 保存图片到本地文件夹
522
+ with open(os.path.join(self.save_path, self.filename), 'wb') as f:
523
+ f.write(res.content)
524
+ i += 1
525
+ time.sleep(0.5)
526
+
527
+ @staticmethod
528
+ def months_data(num=0, end_date=None):
529
+ """ 读取近 num 个月的数据, 0 表示读取当月的数据 """
530
+ if not end_date:
531
+ end_date = datetime.datetime.now()
532
+ start_date = end_date - relativedelta(months=num) # n 月以前的今天
533
+ start_date = f'{start_date.year}-{start_date.month}-01' # 替换为 n 月以前的第一天
534
+ return pd.to_datetime(start_date), pd.to_datetime(end_date)
535
+
457
536
  def main(service_name, database):
458
537
  if not os.path.exists(Share_Path):
459
538
  print(f'当前系统环境不支持')
@@ -483,5 +562,159 @@ def main(service_name, database):
483
562
  ) # 3. 回传数据库
484
563
 
485
564
 
565
+ def main2(service_name, database):
566
+ d = DownloadPicture(service_name=service_name)
567
+ d.get_df_from_service()
568
+ d.download_data()
569
+
570
+
571
+ class InsertPicture():
572
+ def __init__(self):
573
+ self.filename = 'test.xlsx'
574
+ self.path = '/Users/xigua/Downloads'
575
+ self.pic_datas = []
576
+ self.header = 0 # sku 的标题栏起始行数
577
+
578
+ def insert_data(self):
579
+ self.get_filename()
580
+
581
+ workbook = load_workbook(os.path.join(self.path, self.filename))
582
+ sheet = workbook.active
583
+ rows = sheet.max_row # 总行数
584
+ columns = sheet.max_column # 总列数
585
+ sheet.insert_cols(0, 1) # 在第0列开始插入1列空白列
586
+ # sheet['A1'] = '商品图片'
587
+
588
+ is_trange = False
589
+ for col in range(1, columns+1): # 遍历每一列
590
+ # if is_trange == True:
591
+ # break
592
+ for row in range(1, rows+1): # 遍历每一行
593
+ # print(f'第{col}列, 第{row}行...')
594
+ value = sheet.cell(row=row, column=col).value
595
+ if value:
596
+ for data in self.pic_datas:
597
+ if str(value) == data['sku'] or (len(str(value)) > 16 and str(value) in data['sku']):
598
+ # print(value, data['sku'])
599
+ print(f'转换: 第{col}列, 第{row}行, sku: {data['sku']} ...')
600
+ image_path = os.path.join(data['文件路径'], data['文件名称'])
601
+ with open(image_path, 'rb') as f:
602
+ img_data = f.read()
603
+ img = Image(io.BytesIO(img_data))
604
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
605
+ col_letter = 'A'
606
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
607
+ sheet.column_dimensions[col_letter].width = 13
608
+ sheet.row_dimensions[row].height = 80
609
+ img.width = width
610
+ img.height = height
611
+ sheet.add_image(img, f'{col_letter}{row}')
612
+ is_trange = True
613
+
614
+ if is_trange == False: # 如果 sku 没有匹配到任何值,则使用 商家编码
615
+ for col in range(1, columns + 1): # 遍历每一列
616
+ # if is_trange == True:
617
+ # break
618
+ for row in range(1, rows + 1): # 遍历每一行
619
+ # print(f'第{col}列, 第{row}行...')
620
+ value = sheet.cell(row=row, column=col).value
621
+ if value:
622
+ for data in self.pic_datas:
623
+ if str(value) == data['商家编码']:
624
+ # print(value, data['sku'])
625
+ print(f'转换: 第{col}列, 第{row}行, 商家编码: {data['商家编码']} ...')
626
+ image_path = os.path.join(data['文件路径'], data['文件名称'])
627
+ with open(image_path, 'rb') as f:
628
+ img_data = f.read()
629
+ img = Image(io.BytesIO(img_data))
630
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
631
+ col_letter = 'A'
632
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
633
+ sheet.column_dimensions[col_letter].width = 13
634
+ sheet.row_dimensions[row].height = 80
635
+ img.width = width
636
+ img.height = height
637
+ sheet.add_image(img, f'{col_letter}{row}')
638
+ is_trange = True
639
+ break # 商家编码只需要添加一次,所以必须 break,否则可能添加多个图片到某个单元格
640
+
641
+ if is_trange == False: # 如果 sku 和商家编码都没有匹配到任何值,则使用 商品id
642
+ for col in range(1, columns + 1): # 遍历每一列
643
+ # if is_trange == True:
644
+ # break
645
+ for row in range(1, rows + 1): # 遍历每一行
646
+ # print(f'第{col}列, 第{row}行...')
647
+ value = sheet.cell(row=row, column=col).value
648
+ if value:
649
+ for data in self.pic_datas:
650
+ if str(value) == data['商品id']:
651
+ # print(value, data['sku'])
652
+ print(f'转换: 第{col}列, 第{row}行, 商品id: {data['商品id']} ...')
653
+ image_path = os.path.join(data['文件路径'], data['文件名称'])
654
+ with open(image_path, 'rb') as f:
655
+ img_data = f.read()
656
+ img = Image(io.BytesIO(img_data))
657
+ width, height = self.img_resize(img.width, img.height) # 等比例缩放图片
658
+ col_letter = 'A'
659
+ # col_letter = get_column_letter(col) # 将数字索引转换为列标签 A、B、C、D...
660
+ sheet.column_dimensions[col_letter].width = 13
661
+ sheet.row_dimensions[row].height = 80
662
+ img.width = width
663
+ img.height = height
664
+ sheet.add_image(img, f'{col_letter}{row}')
665
+ is_trange = True
666
+ break # 商品id只需要添加一次,所以必须 break,否则可能添加多个图片到某个单元格
667
+
668
+ if is_trange == False:
669
+ print(f'{self.filename}:\n'
670
+ f'在该文件中没有找到匹配的 skuid/商品id/商家编码, 注意程序只会转换当前活动的 sheet, \n'
671
+ f'1. 如果您确定文件中确实存在 skuid/商品id/商家编码, 可能是因为 sheet 不是活动状态, 请切换后再重新运行本程序。\n'
672
+ f'2. 程序只能转换已经收录的商品图, 如果未被收录亦会转换失败, 请联系开发者添加。')
673
+
674
+ workbook.save(os.path.join(self.path, f'ok_{self.filename}'))
675
+
676
+ def img_resize(self, width, height, num=100):
677
+ """
678
+ 设置基础大小为 num, 并等比例缩放
679
+ """
680
+ if width > height:
681
+ height = height * num // width
682
+ width = num
683
+ else:
684
+ width = width * num // height
685
+ height = num
686
+ return width, height
687
+
688
+ def get_filename(self):
689
+ for root, dirs, files in os.walk(os.path.join(self.path, 'sku图片链接'), topdown=False):
690
+ for name in files:
691
+ if name.endswith('.jpg'):
692
+ sku_id = re.findall(r'\d+_\d+_(\d+)_|\d+_\d+_(\d+-\d+)_|\d+_\d+_([A-Za-z]+\d+)_', name)
693
+ sku_id = [item for item in sku_id[0] if item != '']
694
+ sp_id = re.findall(r'^(\d+)_', name)
695
+ spbm = re.findall(r'(\d{13})\d+', sku_id[0])
696
+ if not spbm:
697
+ spbm = ['0']
698
+ self.pic_datas.append(
699
+ {
700
+ '文件路径': root,
701
+ '文件名称': name,
702
+ 'sku': sku_id[0],
703
+ '商品id': sp_id[0],
704
+ '商家编码': spbm[0],
705
+ }
706
+ )
707
+
708
+
709
+ def main3():
710
+ p = InsertPicture()
711
+ p.filename = 'test222.xlsx'
712
+ # p.header = 1
713
+ p.insert_data()
714
+
715
+
716
+
486
717
  if __name__ == '__main__':
487
- main(service_name='company', database='mysql')
718
+ # main(service_name='company', database='mysql')
719
+ # main2(service_name='company', database='mysql')
720
+ main3()
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: mdbq
3
- Version: 1.8.0
3
+ Version: 1.8.2
4
4
  Home-page: https://pypi.org/project/mdbsql
5
5
  Author: xigua,
6
6
  Author-email: 2587125111@qq.com
@@ -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=U9NG-laKns3g0FJsK-JwqlW1EQZRTEXvIsbdFNBR0Ro,22250
33
+ mdbq/other/sku_picture.py,sha256=xNXOzyaO-qz1htYroPmYnsct_CXF4MLJASereVEGN6g,33429
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.0.dist-info/METADATA,sha256=hb11aMvPCn1u8Jtmn2BQaMfgkUloamu_-ri-FFpgpns,245
40
- mdbq-1.8.0.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
41
- mdbq-1.8.0.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
42
- mdbq-1.8.0.dist-info/RECORD,,
39
+ mdbq-1.8.2.dist-info/METADATA,sha256=PsbFqqZUVz03tpO86vKRA68rj6Oy3JtEQ2qOUqnauQI,245
40
+ mdbq-1.8.2.dist-info/WHEEL,sha256=cpQTJ5IWu9CdaPViMhC9YzF8gZuS5-vlfoFihTBC86A,91
41
+ mdbq-1.8.2.dist-info/top_level.txt,sha256=2FQ-uLnCSB-OwFiWntzmwosW3X2Xqsg0ewh1axsaylA,5
42
+ mdbq-1.8.2.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.44.0)
2
+ Generator: setuptools (70.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5