-
Notifications
You must be signed in to change notification settings - Fork 0
/
twitch_dump.py
67 lines (54 loc) · 1.84 KB
/
twitch_dump.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
from website_extract import read_state
import os
import time
import shutil
import re
import json
import pickle
from ocr_clean import *
from analyze_images import *
from salty_elo import predict
state = read_state()
def wait_for_bets():
global state
while state['status'] != 'open':
state = read_state()
if state['status'] == 'open':
time.sleep(8)
break
time.sleep(15)
def video_to_png(video_path):
"""converts a .flv video to a .png"""
img_path = re.sub(r"flv", r"png", video_path)
os.system("ffmpeg -i " + video_path + " -r 1 -t 1 " + img_path)
return img_path
if __name__ == "__main__":
db = pickle.load(open('db.pickle'))
matches = json.load(open('matches.json', 'r'))
wait_for_bets()
for i in range(1000):
wait_for_bets()
vid_path = os.path.join('vid_dir', str(i).zfill(3) + ".flv")
os.system("timeout 2.0 livestreamer www.twitch.tv/saltybet Source -o " + vid_path)
if not os.path.exists(vid_path) or os.path.getsize(vid_path) < 1:
print "missed a betting round. skipping"
time.sleep(30)
continue
img_path = video_to_png(vid_path)
match = ocr_match_from_image(img_path, cleanup=True)
if len(match) > 1:
predict(match[0], match[1], db, matches)
time.sleep(25)
while state['status'] not in ['1', '2']: # wait for result
state = read_state()
if state['status'] in ['1', '2']:
break
time.sleep(15)
winner = state['status']
# rename video and images with winner
vid_name = os.path.join('vid_dir', str(i).zfill(3) + 'w' + winner + '.flv')
img_name = os.path.join('vid_dir', str(i).zfill(3) + 'w' + winner + '.png')
shutil.move(vid_path, vid_name)
shutil.move(img_path, img_name)
match = ocr_match_from_image(img_name)
print match