-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_user_order.py
57 lines (41 loc) · 1.46 KB
/
extract_user_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
import tempfile
import pandas as pd
import requests
BEGIN_MARKER = 'Сумма(р)'
END_MARKER = 'Unnamed'
ITEMS_COLUMN = 'Наименование'
BUY_EVENT_MARKER = 1
def load_gsheet(sheet_url):
with tempfile.NamedTemporaryFile(suffix='.csv') as tfile:
with open(tfile.name, 'wb') as f:
f.write(requests.get(sheet_url).content)
return pd.read_csv(tfile.name, skiprows=1)
def extract_users(df):
begin_column = df.columns.tolist().index(BEGIN_MARKER)
end_column = min([
i
for i, name in enumerate(df.columns.tolist())
if name.startswith(END_MARKER)
])
return df[df.columns[begin_column + 1:end_column]]
def extract_items(df):
return df[ITEMS_COLUMN]
def get_order(sheet_url, user_name):
df = load_gsheet(sheet_url)
users = extract_users(df)
items = extract_items(df)
user_items = items[users[user_name] == BUY_EVENT_MARKER].tolist()
return user_items
def main(sheet_url):
df = load_gsheet(sheet_url)
users = extract_users(df)
items = extract_items(df)
for user in users.columns:
user_items = items[users[user] == BUY_EVENT_MARKER].tolist()
if len(user_items) > 0:
print(f'>> {user}')
print(user_items)
print('')
if __name__ == '__main__':
main(
sheet_url='https://docs.google.com/spreadsheets/d/1p-Xs3UB8HaVkauVS-K6Wko2T_0txNILp/export?format=csv&id=1p-Xs3UB8HaVkauVS-K6Wko2T_0txNILp&gid=1128347462')