12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import requests, json, praw
- from api_authentication import reddit_obj, get_aws_secret
- cred = get_aws_secret("hpg-keys","us-west-2")["reddit"]
- REDDIT = reddit_obj(
- client_id=cred['clientid'],
- client_secret=cred['clientsecret'],
- user=cred['user'])
- def get_posts(subreddit_name, post_count):
-
- titles = [r.title for r in REDDIT.subreddit(subreddit_name).hot(limit=post_count)]
- return titles
- def parse_title(title):
- """Try to parse a title for a song name and artist
- This is based off common naming convention used in subreddit... no ML yet :)
- Rules:::
- - any post starting with [FRESH] will follow with <ARTIST> - <SONG> ...
- - posts with <NAME> - <WORDS> is almost always a song
- - any titles with `ft.` will have a feature, meaning a song"""
-
- dash_loc = title.find('-')
- if dash_loc == -1 or title[:12].lower() == '[discussion]':
- return None
- else:
- pass
-
- if title[0] == "[":
- artist = title[(title.find(']')+2) : (dash_loc)]
- song = title[(dash_loc+1) :]
-
- else:
- artist = title[: dash_loc]
- song = title[dash_loc+1 :]
-
- info_loc = title.rfind('(')
-
- song = song if info_loc < dash_loc else title[(dash_loc+1) : (info_loc)]
-
- song = song if song.find('[') == -1 else song[: song.find('[')-1]
-
-
- artist = artist if "ft." not in artist else artist[: artist.find('ft.')]
-
-
- song_post = {"artist":artist, "track": song}
- print(f"\nSong post we found so far:\n\t{song_post}")
- return song_post
|