from update_state import r, get_submission_urls, get_stream_from_url, get_reddit_username, get_or_create
from app import Streamer, Submission, db


def add_streamers():
    submissions = r.get_subreddit('watchpeoplecode').get_new(limit=None)
    for s in submissions:
        for url in get_submission_urls(s):
            stream = get_stream_from_url(url, s.id)
            if stream and stream.streamer is None:
                reddit_username = get_reddit_username(s, url)
                streamer = Streamer.query.filter_by(reddit_username=reddit_username).first()
                if streamer is None:
                    streamer = Streamer(reddit_username)

                stream.streamer = streamer
                stream._update_status()

                db.session.add_all([stream, streamer])
                db.session.commit()


def add_submissions():
    submissions = r.get_subreddit('watchpeoplecode').get_new(limit=None)
    for s in submissions:
        for url in get_submission_urls(s):
            stream = get_stream_from_url(url, s.id)
            if stream:
                stream.submission = get_or_create(Submission, submission_id=s.id)

@@ -489,60 +489,11 @@
     
-streamer = Streamer.query.filter_by(reddit_username=
+if 
 redd
@@ -507,17 +507,9 @@
 name
-).first()
+:
 %0A   
@@ -525,68 +525,52 @@
     
-if
+   
  stream
-er is None:%0A                    streamer = 
+.streamer = get_or_create(
 Streamer
 (red
@@ -565,17 +565,18 @@
 Streamer
-(
+, 
 reddit_u
@@ -586,54 +586,30 @@
 name
-)%0A%0A                stream.streamer = streamer%0A
+=reddit_username)%0A    
     
@@ -679,31 +679,15 @@
 .add
-_all(%5Bstream, streamer%5D
+(stream
 )%0A  

refactor add_streamers code