You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

37 lines
931 B

  1. import mastodon
  2. import random
  3. import requests
  4. import re
  5. def randline(fname):
  6. with open(fname) as afile:
  7. line = next(afile)
  8. for num, aline in enumerate(afile, 2):
  9. if random.randrange(num) == 0:
  10. line = aline
  11. return line.strip()
  12. def bot():
  13. genre = randline('genres.txt')
  14. if genre:
  15. genre += ' '
  16. beats = "lo-fi {}beats to {} and {} to".format(
  17. genre,
  18. randline('1syllableverbs.txt'), randline('2syllableverbs.txt'))
  19. print(beats)
  20. mdon = mastodon.Mastodon(
  21. access_token='token.secret',
  22. api_base_url='https://botsin.space')
  23. query = requests.get(f'https://www.youtube.com/results?search_query={beats}')
  24. match = re.search(r'\?v=([^"]+)"', query.text)
  25. if match:
  26. beats += f'\n\nhttps://youtube.com/watch?v={match.group(1)}'
  27. print(beats)
  28. mdon.status_post(beats)
  29. if __name__ == '__main__':
  30. bot()