main.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from reddit_ingest import get_posts, parse_title
  2. from spotify_apis import get_track_uri, replace_tracks
  3. # Main file for executing flow.
  4. def handler(event, context):
  5. # Get incoming payload to run job
  6. payload = event['body']
  7. post_count = payload['post_count']
  8. # ingest titles from reddit, one sub at a time. Can parallelize later.
  9. # TODO: look into kicking off separate threads across functions to update metrics DB
  10. for i in payload['playlists']:
  11. # parse out dictionary
  12. sub, playlist_uri = i['sub'], i['uri']
  13. print(f"Ingesting {sub}")
  14. posts = get_posts(subreddit_name=sub, post_count=post_count)
  15. # parse titles and filter out nulls that did not qualify as tracks
  16. titles = list(filter(None, map(parse_title, posts)))
  17. print(f"Received {len(titles)} tracks for {sub}")
  18. # TODO: write out titles to int. storage
  19. # TODO: kick off spotify apis in separate lambda later on
  20. # search URIs of tracks
  21. uris = [get_track_uri(artist=t['artist'], track=t['track']) for t in titles]
  22. uris = list(filter(None, uris))
  23. # Replace tracks in the playlist with the new list of track URIs
  24. replace_tracks(playlist=playlist_uri, tracks=uris)
  25. # local running
  26. if __name__=='__main__':
  27. payload = {
  28. "post_count": 50,
  29. "playlists": [
  30. {"sub":"HipHopHeads", "playlist": "hotHipHop", "uri":"5vrlPDIae4QQhdGHw7KFJc"},
  31. {"sub":"ListenToThis", "playlist": "hotCheckItOut", "uri":"2E6NKwGiLOkSaIocSff39Y"},
  32. {"sub":"Music", "playlist": "hotRandom", "uri":"7lRweQeV04LmHJggtLZr3n"},
  33. #{"sub":"Trap", "playlist": "", "uri":""},
  34. ]
  35. }
  36. event, context = {"body": payload}, None
  37. handler(event, context)