-
Notifications
You must be signed in to change notification settings - Fork 8
/
socialReport.py
70 lines (57 loc) · 1.87 KB
/
socialReport.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
#!/usr/bin/env python3
import sys
from brightcove.Social import Social
from brightcove.OAuth import OAuth
from brightcove.utils import load_account_info
from brightcove.utils import list_to_csv, eprint
videos_processed = 0
hits_to_process = 0
def show_progress(progress: int, total: int):
"""
Simple progress counter.
"""
sys.stderr.write(f'\r{progress}/{total} processed...\r')
sys.stderr.flush()
cms = None
# account/API credentials (can be None to use user defaults)
account_id = ''
client_id = ''
client_secret = ''
report_name = 'social_report.csv'
# get account info from config file if not hardcoded
if '' in [account_id, client_id, client_secret]:
try:
account_id, client_id, client_secret, _ = load_account_info()
except Exception as e:
print(e)
sys.exit(2)
# create a Social API instance
social = Social( oauth=OAuth(account_id=account_id,client_id=client_id, client_secret=client_secret) )
page_key = None
keep_running = True
row_list = [ ['id','account_id','destination_id','remote_id','remote_url','status','timestamp','published_at','distribution_method','autosync_id','error','warning','action','result'] ]
while keep_running:
search_query = '' if not page_key else f'page_key={page_key}'
response = social.ListStatusForVideos(search_query=search_query)
if response.status_code == 200:
body = response.json()
hits_to_process = body.get('total_hits')
page_key = body.get('page_key')
if not page_key:
keep_running = False
videos = body.get('videos')
if videos:
for video in videos:
row = [ video.get(field) for field in row_list[0] ]
row_list.append(row)
videos_processed += 1
if videos_processed%100==0:
show_progress(videos_processed,hits_to_process)
else:
keep_running = False
show_progress(videos_processed,hits_to_process)
#write list to file
try:
list_to_csv(row_list, report_name)
except Exception as e:
eprint(f'\n{e}')