-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_order.py
150 lines (147 loc) · 5.85 KB
/
upload_order.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# -*- coding: utf-8 -*-
import warnings
warnings.filterwarnings("ignore")
import pandas as pd
import glob,os
from shutil import copyfile
import math
import numpy as np
masterdata = pd.read_excel('metadata.xlsx')
masterdata.head()
def insert_upload_order(file_name):
try:
masterdata = pd.read_excel('metadata.xlsx')
try:
data = pd.read_excel(file_name)
except:
q = "Cannot read ",file_name.split("\\")[-1]
return q
data = data.drop(data.index[[0,1,2,3]])
data = data.rename(columns=data.iloc[0])
data = data[1:]
if u'PHASE' not in data.columns:
os.remove(file_name)
q = (file_name.split("\\")[-1]," has a problem with structure, no PHASE")
return q
if u'ALBUM' in data.columns:
ALBUM = data[data[u'ALBUM'].notnull() == True][u'ALBUM']
HD = data[data[u'HỢP ĐỒNG'].notnull() == True][u'HỢP ĐỒNG']
if u'ID VIDEO' in data.columns:
try:
try:
data = data.drop(u'VIDEO ID', 1)
except:
data = data.drop(u'Video ID', 1)
try:
data.rename(columns={u'ID VIDEO':u'VIDEO ID'},inplace=True)
except:
data.rename(columns={u'ID Video':u'VIDEO ID'},inplace=True)
except KeyError:
pass
if u'ID video' in data.columns:
try:
try:
data = data.drop(u'VIDEO ID', 1)
except:
data = data.drop(u'Video ID', 1)
try:
data.rename(columns={u'ID video':u'VIDEO ID'},inplace=True)
except:
data.rename(columns={u'ID VIDEO':u'VIDEO ID'},inplace=True)
except KeyError:
pass
if u'id video' in data.columns:
try:
try:
data = data.drop(u'VIDEO ID', 1)
except:
data = data.drop(u'Video ID', 1)
try:
data.rename(columns={u'id video':u'VIDEO ID'},inplace=True)
except:
data.rename(columns={u'ID VIDEO':u'VIDEO ID'},inplace=True)
except KeyError:
pass
try:
data[u'ID video Yt']
data = data.drop(u'Video ID', 1)
data.rename(columns={u'ID video Yt':u'VIDEO ID'},inplace=True)
except KeyError:
pass
try:
data[u'Video ID']
data.rename(columns={u'Video ID':u'VIDEO ID'},inplace=True)
except KeyError:
pass
try:
if (str(data.columns[1])=='nan' and str(data.columns[2])=='nan'):
try:
data = data.drop('VIDEO ID',1)
except KeyError:
data = data.drop('Video ID',1)
data = data[data.iloc[:, 1].notnull()==True]
column_names = data.columns.values
column_names[1] = u'VIDEO ID'
data.columns = column_names
except:
pass
data = data[data['VIDEO ID'].notnull()]
try:
data['TÊN NỘI DUNG']
except KeyError:
try:
data[u'NỘI DUNG']
data = data.drop(u'TÊN NỘI DUNG', 1)
data.rename(columns={u'NỘI DUNG':u'TÊN NỘI DUNG'},inplace=True)
except KeyError:
try:
data[u'TÊN BÀI HÁT']
data.rename(columns={u'TÊN BÀI HÁT':u'TÊN NỘI DUNG'},inplace=True)
except KeyError:
pass
try:
data[u'Tên bài hát | Tên ca sĩ /Lyric Video']
data.rename(columns={'Tên bài hát | Tên ca sĩ /Lyric Video':u'TÊN NỘI DUNG'},inplace=True)
except KeyError:
pass
try:
data[u'TÊN PHIM']
data.rename(columns={u'TÊN PHIM':u'TÊN NỘI DUNG'},inplace=True)
except KeyError:
pass
try:
data[u'Series Name']
data.rename(columns={u'Series Name':u'SERIES NAME'},inplace=True)
except KeyError:
pass
if u'ALBUM' in data.columns:
if (len(data) != len(ALBUM) or len(data) != len(HD)):
q = ("The length is not equal in",file_name.split("\\")[-1])
copyfile(file_name,"undone\\"+file_name.split("\\")[-1])
os.remove(file_name)
return q
else:
if ALBUM.str.contains("VN").any() == True:
pass
else:
data[u'TÊN NỘI DUNG'] = ALBUM.values
data[u'HỢP ĐỒNG'] = HD.values
try:
merge_data = data[[u'VIDEO ID',u'TÊN NỘI DUNG',u'HỢP ĐỒNG',u'CATEGORY',u'SUB CATEGORY',u'SUB CATEGORY 2',u'SEASON',u'PHASE',u'SERIES NAME']]
frames = [masterdata,merge_data]
result = pd.concat(frames)
result.to_excel('metadata.xlsx')
q = (file_name.split("\\")[-1] , "is succeed")
except:
copyfile(file_name,"undone\\"+file_name.split("\\")[-1])
os.remove(file_name)
q = (file_name.split("\\")[-1]," has a problem with merging data")
pass
except (IndexError) as e:
copyfile(file_name,"undone\\"+file_name.split("\\")[-1])
os.remove(file_name)
q = (file_name.split("\\")[-1]," has a problem with structure")
pass
return q
for file in glob.glob("Upload orders\\*"):
print insert_upload_order(file)