def post_rand_song(bot_id):
    print("Finding spotify song")
    results = sp.user_playlist(username, playlist_id, 'tracks,next')
    tracks = results['tracks']
    all_tracks = tracks['items']
    while tracks['next']:
        tracks = sp.next(tracks)
        all_tracks += tracks['items']

    random_track = random.choice(all_tracks)
    post_text(u'\U0001F3B5: ' + random_track['track']['name'] + ' - ' + random_track['track']['artists'][0]['name'] + ' ' + random_track['track']['external_urls']['spotify'], bot_id)
import sys
import icalendar
import requests
import pytz
from datetime import datetime, timedelta
from libs import post_text
from icalendar import Calendar

r = requests.get(sys.argv[2])
icsData = r.text

cal = Calendar.from_ical(icsData)

for evt in cal.subcomponents:
    start = evt.decoded('DTSTART')
    now = datetime.now(tz=pytz.utc)
    time_left = start - now
    if timedelta(minutes=0) < time_left < timedelta(minutes=10):
        post_text(evt.decoded('SUMMARY'), sys.argv[1])
Exemple #3
0
from datetime import datetime
import pytz
from libs import post_text
import spotipy
import os
from spotipy.oauth2 import SpotifyClientCredentials
'''
sadness_texts = [line.strip() for line in open('list of saddness.txt')]
central = timezone('US/Central')
now = datetime.now(tz=pytz.utc)
'''

username = os.environ.get("USERNAME")
client_id = os.environ.get("SPOTIPY_CLIENT_ID")
client_secret = os.environ.get("SPOTIPY_CLIENT_SECRET")
client_credentials_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

results = sp.user_playlist(username, sys.argv[2], 'tracks,next')
tracks = results['tracks']
all_tracks = tracks['items']
while tracks['next']:
    tracks = sp.next(tracks)
    all_tracks += tracks['items']

random_track = random.choice(all_tracks)
post_text(
    u'\U0001F3B5\U0001F4C5: ' + random_track['track']['name'] + ' - ' +
    random_track['track']['artists'][0]['name'] + ' ' +
    random_track['track']['external_urls']['spotify'], sys.argv[1])
def parse_messages(bot_id):
    try:
        message = request.get_json()
        if message['sender_type'] != "user":
            return 'OK'
        """ Group Specific Actions"""

        # BUPD Things
        if request.args.get('bupd', '') != '':
            if re.search(r"\bbupd\b", message['text'].lower()):
                post_text(responses[request.args.get('bupd', '')], bot_id)

        # CONSEQUENCE
        if request.args.get('dorm', '') != '':
            if re.search(r"\bdorm[Ss]?\b", message['text'].lower()):
                post_text(responses[request.args.get('dorm', '')], bot_id)

        #Professionalism
        if request.args.get('punct', '') != '':
            if not (message['text'].strip().endswith((".", "?", "!"))):
                post_text(
                    "In the spirit of being professional, all messages must end with proper punctuation.",
                    bot_id)
        """ Actions for all groups"""

        # Say hello to anyone that says "Hi"
        if re.search(r"\bhi\b", message['text'].lower()):
            post_text("Hi " + message['name'].split(" ")[0] + "!", bot_id)

        # Post gif from Giphy
        if message['text'].startswith("/gif"):
            search = re.search(r"/gif (.*?)( \d+)?$", message['text'])
            (gif_search, num) = search.groups('1')
            if int(num) < 20:
                num = min(int(num), 5)
                for i in range(int(num)):
                    post_text(gif.translate(gif_search).media_url, bot_id)
            else:
                post_text("Can you not.", bot_id)

        # Clap a bunch
        if message['text'].startswith("/clap"):
            msg = message['text'][5:]
            msg = msg.upper()
            clap = '\U0001F44F'
            if message['sender_id'] == '19791433':
                clap += '\U0001F3FF'
            msg = clap.join(msg.split())
            post_text(msg, bot_id)

        # Jokes
        if message['text'].startswith("/joke"):
            headers = {'Accept': 'text/plain'}
            joke = requests.get("https://icanhazdadjoke.com", headers=headers)
            joke.raise_for_status()
            post_text(joke.content.decode("UTF-8"), bot_id)

        return 'OK'
    except:
        post_text(u'\U0001F916\u2620: ' + traceback.format_exc(), bot_id)
        return 'Not OK'
Exemple #5
0
from libs import post_text
from icalendar import Calendar
from database import find_bot_nname
import re

r = requests.get(sys.argv[2])
icsData = r.text

cal = Calendar.from_ical(icsData)

for evt in cal.walk('vevent'):
    start = evt.decoded('DTSTART')
    now = datetime.now(tz=pytz.utc)
    time_left = start - now
    if timedelta(minutes=0) < time_left < timedelta(minutes=10):
        raw_text = str(evt.get('SUMMARY'))
        search = re.search(r"([^ ]+)\s(.+)", raw_text)
        (nname, message) = search.groups('1')
        message = message[:-1]
        print(raw_text)
        print(nname)
        print(message)
        bot_id = find_bot_nname(nname)
        if not bot_id:
            bot_id = sys.argv[1]
            post_text("I was supposed to post '" + message + "' to " + nname,
                      bot_id)
        else:
            bot_id = bot_id[0][0]
            post_text(message, bot_id)