def youtube_playlist_videos(playlist_id): """ Finds the video info for a given playlist ID. The list returned will contain a maximum of 50 videos. :param playlist_id: str the playlist ID :return: list[ dict{'type=youTube', 'video_id', 'video_title', 'video_time'}] or None on failure. """ playlist_details_url = 'https://www.googleapis.com/youtube/v3/playlistItems?' \ 'key=AIzaSyCPQe4gGZuyVQ78zdqf9O5iEyfVLPaRwZg&playlistId=%s' \ '&maxResults=50&part=snippet,id' % playlist_id api_response = web_request.get_request(playlist_details_url, json=True) if api_response['content'] is not None: video_list = [] try: for item in api_response['content']['items']: video_id = item['snippet']['resourceId']['videoId'] video_title = item['snippet']['title'] video_time = youtube_time(video_id) if video_time is not None: info = { 'type': 'youTube', 'video_id': video_id, 'video_title': video_title, 'video_time': video_time['video_time'] } video_list.append(info) return video_list except KeyError: return None
def weather_search(city): """ Searches worldweatheronline's API for weather data for a given city. You must have a working API key to be able to use this function. :param city: The city str to search for. :return: weather data str or None on no match or error. """ if str(city).strip(): api_key = API_KEYS['weather'] # A valid API key. if not api_key: return False else: weather_api_url = 'http://api.worldweatheronline.com/free/v2/weather.ashx?' \ 'q=%s&format=json&key=%s' % (city, api_key) response = web_request.get_request(weather_api_url, json=True) if response['content'] is not None: try: pressure = response['content']['data'][ 'current_condition'][0]['pressure'] temp_c = response['content']['data']['current_condition'][ 0]['temp_C'] temp_f = response['content']['data']['current_condition'][ 0]['temp_F'] query = response['content']['data']['request'][0][ 'query'].encode('ascii', 'ignore') result = query + '. Temperature: ' + temp_c + 'C (' + temp_f + 'F) Pressure: ' + pressure + ' millibars' return result except (IndexError, KeyError): return None else: return None
def lastfm_listening_now(max_tunes, by_id=True): """ Gets a list of tunes other people using last.fm are listening to, and turns them in to a youtube list of tracks. :param max_tunes: int the amount of tracks we want. :param by_id: bool if set to True, only tunes that have a youtube id will be added(recommended) :return: list[ dict{'type=youtube', 'video_id', 'int(video_time)', 'video_title'} ] or None on error. """ url = 'http://lastfm-ajax-vip1.phx1.cbsig.net/kerve/listeningnow?limit=%s&format=json' % max_tunes lastfm = web_request.get_request(url, json=True) log.debug(lastfm) if lastfm is not None: if len(lastfm['content']['Users']) is not 0: yt_tracks = [] for user in lastfm['content']['Users']: if 'playlink' in user: if 'data-youtube-id' in user['playlink']: youtube_id = user['playlink']['data-youtube-id'] yt = youtube.youtube_time(youtube_id) log.debug(yt) if yt is not None: yt_tracks.append(yt) else: if 'Track' in user: search_str = '%s - %s' % (user['Track']['Artist'], user['Track']['Name']) if not by_id: yt = youtube.youtube_search(search_str) log.debug('search by search string: %s result: %s' % (search_str, yt)) if yt is not None: yt_tracks.append(yt) return yt_tracks return None
def get_captcha_key(roomname, uid, proxy=None): """ Find the captcha key needed before we can send messages in a room. :param roomname: str the room name. :param uid: str the ID we have in the room. :param proxy: str use a proxy for this request. :return: str the captcha key or None on captcha enabled room. """ url = 'http://tinychat.com/api/captcha/check.php?room=tinychat^%s&guest_id=%s' % ( roomname, uid) json_data = web_request.get_request(url, json=True, proxy=proxy) # print 'in captcha key' if json_data is not None: try: if 'key' in json_data['content']: return json_data['content']['key'] else: return None except Exception: # Handle abnormal response regarding the captcha key data to work by forcing a # text parse for the captcha key raw = json_data['content'] # print 'abnormal data received' # Credits to pinychat (NotNola) return raw.split('"key":"')[1].split('"')[0]
def get_cauth_cookie(roomname, proxy=None): """ Find the cauth 'cookie' needed to make a successful connection. This is not really a cookie, but named so after its name in the json response. :param roomname: str the room name. :param proxy: str use a proxy for this request. :return: str the 'cookie' """ ts = int(round(time.time() * 1000)) url = 'http://tinychat.com/cauth?room=%s&t=%s' % (roomname, str(ts)) json_data = web_request.get_request(url, json=True, proxy=proxy) # print 'in cauth' if json_data is not None: try: # Normal cauth cookie parsing if 'cookie' in json_data['content']: # print 'normal cookie data received' return json_data['content']['cookie'] else: return None except Exception: # Handle abnormal response regarding the cauth cookie data to work by forcing a # text parse for the cauth cookie raw = json_data['content'] # print 'abnormal cookie data received' # Credits to pinychat (NotNola) return raw.split('{"cookie":"')[1].split('"')[0]
def recaptcha(proxy=None): """ Check if we have to solve a captcha before we can connect. If yes, then it will open in the default browser. :param proxy: str use a proxy for this request. :return: dict{'cookies'} this is NOT used in the code , but are left here for debugging purposes. """ t = str(random.uniform(0.9, 0.10)) url = 'https://tinychat.com/cauth/captcha?%s' % t response = web_request.get_request(url, json=True, proxy=proxy) if response is not None: if 'need_to_solve_captcha' in response['content']: successful_recaptcha = False link = '' try: if response['content']['need_to_solve_captcha'] == 1: link = 'https://tinychat.com/cauth/recaptcha?token=%s' % response['content']['token'] successful_recaptcha = True except (Exception, KeyError): if '"need_to_solve_captcha":1' in response['content']: token = response['content'].split('"token":"')[1].split('"')[0] link = 'https://tinychat.com/cauth/recaptcha?token=%s' % token if successful_recaptcha: print(link) if os.name == 'nt': webbrowser.open(link, new=1, autoraise=True) raw_input('Solve the captcha and hit enter to continue (OR hit enter to join anonymously).') return response['cookies']
def get_bauth_token(roomname, nick, uid, greenroom, proxy=None): # A token IS present even if password is enabled, will it work? needs more testing.. """ Find the bauth token needed before we can start a broadcast. :param roomname: str the room name. :param nick: str the nick we use in the room. :param uid: str our ID in the room. :param greenroom: bool should be True if greenroom is enabled. :param proxy: str use a proxy for this request. :return: str token or PW if a password is needed to broadcast. """ if greenroom: xmlurl = 'http://tinychat.com/api/broadcast.pw?site=greenroom&name=%s&nick=%s&id=%s' % ( roomname, nick, uid) else: xmlurl = 'http://tinychat.com/api/broadcast.pw?site=tinychat&name=%s&nick=%s&id=%s' % ( roomname, nick, uid) web_content = web_request.get_request(xmlurl, proxy=proxy) if web_content is not None: xml = parseString(web_content['content']) root = xml.getElementsByTagName('response')[0] result = root.getAttribute('result') if result == 'PW': return result else: token = root.getAttribute('token') return token
def youtube_playlist_search(search, results=5): """ Searches youtube for a playlist matching the search term. :param search: str the search term to search to search for. :param results: int the number of playlist matches we want returned. :return: list[ dict{'playlist_title', 'playlist_id'}] or None on failure. """ if str(search).strip(): youtube_search_url = 'https://www.googleapis.com/youtube/v3/search?' \ 'type=playlist&key=AIzaSyCPQe4gGZuyVQ78zdqf9O5iEyfVLPaRwZg' \ '&maxResults=50&q=%s&part=snippet' % search api_response = web_request.get_request(youtube_search_url, json=True) if api_response['content'] is not None: play_lists = [] try: for item in api_response['content']['items']: playlist_id = item['id']['playlistId'] playlist_title = item['snippet']['title'].encode('utf-8', 'replace') play_list_info = {'playlist_title': playlist_title, 'playlist_id': playlist_id} play_lists.append(play_list_info) if len(play_lists) == results: return play_lists except KeyError: return None else: return None
def spy_info(room): """ Finds info for a given room name. The info is how many mods, broadcasters, users and a comma separated string(users) with all the user names. :param room: str the room name to get spy info for. :return: dict{'mod_count', 'broadcaster_count', 'total_count', 'users'} or None on password protected room/failure. """ xmlurl = 'http://api.tinychat.com/%s.xml' % room passcheck = get_roomconfig_xml(room) if passcheck == 'PW': return passcheck else: web_content = web_request.get_request(xmlurl) xml = parseString(web_content['content']) try: root = xml.getElementsByTagName('tinychat')[0] mod_count = root.getAttribute('mod_count') broadcaster_count = root.getAttribute('broadcaster_count') total_count = root.getAttribute('total_count') if total_count > 0: u = [] names = xml.getElementsByTagName('names') for name in names: u.append(name.firstChild.nodeValue) users = ', '.join(u) return {'mod_count': mod_count, 'broadcaster_count': broadcaster_count, 'total_count': total_count, 'users': users} except IndexError: return None
def time_is(location): """ Retrieves the time in a location by parsing the time element in the html from Time.is . :param location: str location of the place you want to find time (works for small towns as well). :return: time str or None on failure. """ if BeautifulSoup: header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en-GB,en;q=0.5', 'Accept-Encoding': 'gzip, deflate', 'Connection': 'keep-alive', 'Referrer': 'http://time.is/', } post_url = 'http://time.is/' + str(location) time_data = web_request.get_request(post_url, header=header) time_html = time_data['content'] soup = BeautifulSoup(time_html, "html.parser") time = '' try: for hit in soup.findAll(attrs={'id': 'twd'}): time = hit.contents[0].strip() except Exception: pass return time else: return None
def omdb_search(search): """ Query the OMDb API - https://omdbapi.com/ :param search: Search term :return: Title, rating, and short description """ if str(search).strip(): omdb_url = 'http://www.omdbapi.com/?t=%s&plot=short&r=json' % search response = web_request.get_request(omdb_url, json=True) if response: try: title = response['content']['Title'] plot = response['content']['Plot'] imdbid = response['content']['imdbID'] imdbrating = response['content']['imdbRating'] if len(plot) >= 160: plot_parts = plot.split('.') omdb_info = '*Title:* ' + title + '\nDetails: ' + plot_parts[0] + '\n*Rating: *' + imdbrating +\ '\n*More Info:* http://www.imdb.com/title/' + imdbid else: omdb_info = '*Title:* ' + title + '\n' + plot + '\n*Rating:*' + imdbrating +\ '\n*More Info:* http://www.imdb.com/title/' + imdbid return omdb_info except KeyError: return None except IndexError: return None else: return None
def etymonline(search): """ Searches the etymology of words/phrases using the Etymonline website. :param search: str the word/phrase you want to search for. :return: dict the results from the search. """ if BeautifulSoup is not None: url = 'http://etymonline.com/index.php?term=%s&allowed_in_frame=0' search_parts = search.split(' ') search_term = '+'.join(search_parts) post_url = url % search_term raw = web_request.get_request(url=post_url) if raw['status_code'] == 200: html = raw['content'] soup = BeautifulSoup(html, "html.parser") quotes = soup.findAll("dd", {"class": "highlight"}) # There are several quotes/term results returned, we only want # the first one, alternatively a loop can be setup. # Represent the tags as a string, since we do not have specific identification. # Unicode characters in this process will be represented as their respective values. quote = quotes[0].getText() quotes = quote.split('\r\n\r\n') # There are more than one set of quotes parsed, you may iterate over this too in order to return a # greater set of results. return u"" + quotes[0] # Result is returned in unicode. else: return None else: return None
def google_time(location): """ Retrieves the time in a location using Google. :param location: str location of the place you want to find time (Location must be a large town/city/country). :return: time str or None on failure. """ if BeautifulSoup is not None: to_send = location.replace(' ', '%20') url = 'https://www.google.co.uk/search?q=time%20in%20' + str(to_send) raw = web_request.get_request(url) if raw['status_code'] == 200: raw_content = raw['content'] soup = BeautifulSoup(raw_content, 'html.parser') raw_info = None try: for hit in soup.findAll( attrs={'class': 'vk_c vk_gy vk_sh card-section _MZc'}): raw_info = hit.contents except Exception: pass if raw_info is None: return None else: return [str(raw_info[1].getText()), str(raw_info[5].getText())] else: return None else: return None
def google_time(location): """ Retrieves the time in a location using Google. :param location: str location of the place you want to find time (Location must be a large town/city/country). :return: time str or None on failure. """ if BeautifulSoup is not None: to_send = location.replace(' ', '%20') url = 'https://www.google.co.uk/search?q=time%20in%20' + str(to_send) raw = web_request.get_request(url) if raw['status_code'] == 200: raw_content = raw['content'] soup = BeautifulSoup(raw_content, 'html.parser') raw_info = None try: for hit in soup.findAll(attrs={'class': 'vk_c vk_gy vk_sh card-section _MZc'}): raw_info = hit.contents except Exception: pass if raw_info is None: return None else: return [str(raw_info[1].getText()), str(raw_info[5].getText())] else: return None else: return None
def tinychat_user_info(tc_account): """ Finds info for a given tinychat account name. :param tc_account: str the account name. :return: dict {'username', 'tinychat_id', 'last_active', 'name', 'location'} """ url = 'http://tinychat.com/api/tcinfo?username=%s' % tc_account json_data = web_request.get_request(url=url, json=True) if json_data is not None: try: username = json_data['content']['username'] user_id = json_data['content']['id'] last_active = time.ctime(int(json_data['content']['last_active'])) name = json_data['content']['name'] location = json_data['content']['location'] return { 'username': username, 'tinychat_id': user_id, 'last_active': last_active, 'name': name, 'location': location } except KeyError: return None
def lastfm_listening_now(max_tunes, by_id=True): """ Gets a list of tunes other people using last.fm are listening to, and turns them in to a youtube list of tracks. :param max_tunes: int the amount of tracks we want. :param by_id: bool if set to True, only tunes that have a youtube id will be added(recommended) :return: list[ dict{'type=youtube', 'video_id', 'int(video_time)', 'video_title'} ] or None on error. """ url = 'http://lastfm-ajax-vip1.phx1.cbsig.net/kerve/listeningnow?limit=%s&format=json' % max_tunes lastfm = web_request.get_request(url, json=True) log.debug(lastfm) if lastfm is not None: if len(lastfm['content']['Users']) is not 0: yt_tracks = [] for user in lastfm['content']['Users']: if 'playlink' in user: if 'data-youtube-id' in user['playlink']: youtube_id = user['playlink']['data-youtube-id'] yt = youtube.youtube_time(youtube_id) log.debug(yt) if yt is not None: yt_tracks.append(yt) else: if 'Track' in user: search_str = '%s - %s' % (user['Track']['Artist'], user['Track']['Name']) if not by_id: yt = youtube.youtube_search(search_str) log.debug( 'search by search string: %s result: %s' % (search_str, yt)) if yt is not None: yt_tracks.append(yt) return yt_tracks return None
def youtube_playlist_videos(playlist_id): """ Finds the video info for a given playlist ID. The list returned will contain a maximum of 50 videos. :param playlist_id: str the playlist ID :return: list[ dict{'type=youTube', 'video_id', 'video_title', 'video_time'}] or None on failure. """ playlist_details_url = 'https://www.googleapis.com/youtube/v3/playlistItems?' \ 'key=AIzaSyCPQe4gGZuyVQ78zdqf9O5iEyfVLPaRwZg&playlistId=%s' \ '&maxResults=50&part=snippet,id' % playlist_id api_response = web_request.get_request(playlist_details_url, json=True) if api_response['content'] is not None: video_list = [] try: for item in api_response['content']['items']: video_id = item['snippet']['resourceId']['videoId'] video_title = item['snippet']['title'] video_time = youtube_time(video_id) if video_time is not None: info = {'type': 'youTube', 'video_id': video_id, 'video_title': video_title, 'video_time': video_time['video_time']} video_list.append(info) return video_list except KeyError: return None
def spy_info(room): """ Finds info for a given room name. The info shows many mods, broadcasters, total users and a users(list) with all the user names. :param room: str the room name to get spy info for. :return: dict{'mod_count', 'broadcaster_count', 'total_count', list('users')} or PW on password protected room., or None on failure or empty room. """ url = 'http://api.tinychat.com/%s.json' % room check = get_roomconfig_xml(room) if check == 'PW': return check else: try: json_data = web_request.get_request(url, json=True) mod_count = str(json_data['content']['mod_count']) broadcaster_count = str(json_data['content']['broadcaster_count']) total_count = json_data['content']['total_count'] status_code = json_data['status_code'] if total_count > 0: users = json_data['content']['names'] return { 'mod_count': mod_count, 'broadcaster_count': broadcaster_count, 'total_count': str(total_count), 'users': users, 'status_code': status_code } except (TypeError, IndexError, KeyError): return None
def spy_info(room): """ Finds info for a given room name. The info shows many mods, broadcasters, total users and a users(list) with all the nicknames. :param room: str the room name to get spy info for. :return: dict{'mod_count', 'broadcaster_count', 'total_count', list('users')} or PW on password protected room., or None on failure or empty room. """ url = 'http://api.tinychat.com/%s.json' % room check = get_roomconfig_xml(room) if check == 'PW': return check else: try: json_data = web_request.get_request(url, json=True) mod_count = str(json_data['content']['mod_count']) broadcaster_count = str(json_data['content']['broadcaster_count']) total_count = json_data['content']['total_count'] status_code = json_data['status_code'] if total_count > 0: users = json_data['content']['names'] return {'mod_count': mod_count, 'broadcaster_count': broadcaster_count, 'total_count': str(total_count), 'users': users, 'status_code': status_code} except (TypeError, IndexError, KeyError): return None
def soundcloud_search(search): """ Searches soundcloud's API for a given search term. :param search: The search term str to search for. :return: dict['type=soundcloud', 'video_id', 'video_time', 'video_title'] or None on no match or error. """ if str(search).strip(): search_url = 'http://api.soundcloud.com/tracks.json?' \ 'filter=streamable&order=hotness&q=%s&limit=25&client_id=4ce43a6430270a1eea977ff8357a25a3' % search response = web_request.get_request(search_url, json=True) if response['content'] is not None: try: track_id = response['content'][0]['id'] track_time = response['content'][0]['duration'] track_title = response['content'][0]['title'].encode('ascii', 'ignore') return {'type': 'soundCloud', 'video_id': track_id, 'video_time': track_time, 'video_title': track_title} except KeyError: return None except IndexError: return None else: return None
def soundcloud_search(search): """ Searches soundcloud's API for a given search term. :param search: The search term str to search for. :return: dict['type=soundcloud', 'video_id', 'video_time', 'video_title'] or None on no match or error. """ if str(search).strip(): search_url = 'http://api.soundcloud.com/tracks.json?' \ 'filter=streamable&order=hotness&q=%s&limit=25&client_id=4ce43a6430270a1eea977ff8357a25a3' % search response = web_request.get_request(search_url, json=True) if response['content'] is not None: try: track_id = response['content'][0]['id'] track_time = response['content'][0]['duration'] track_title = response['content'][0]['title'].encode( 'ascii', 'ignore') return { 'type': 'soundCloud', 'video_id': track_id, 'video_time': track_time, 'video_title': track_title } except KeyError: return None except IndexError: return None else: return None
def get_bauth_token(roomname, nick, uid, greenroom, proxy=None): # A token IS present even if password is enabled, will it work? needs more testing ... """ Find the bauth token needed before we can start a broadcast. :param roomname: str the room name. :param nick: str the nick we use in the room. :param uid: str our ID in the room. :param greenroom: bool should be True if greenroom is enabled. :param proxy: str use a proxy for this request. :return: str token or PW if a password is needed to broadcast. """ if greenroom: xmlurl = 'http://tinychat.com/api/broadcast.pw?site=greenroom&name=%s&nick=%s&id=%s' % (roomname, nick, uid) else: xmlurl = 'http://tinychat.com/api/broadcast.pw?site=tinychat&name=%s&nick=%s&id=%s' % (roomname, nick, uid) web_content = web_request.get_request(xmlurl, proxy=proxy) if web_content is not None: xml = parseString(web_content['content']) root = xml.getElementsByTagName('response')[0] result = root.getAttribute('result') if result == 'PW': return result else: token = root.getAttribute('token') return token
def recaptcha(proxy=None): """ Check if we have to solve a captcha before we can connect. If yes, then it will open in the default browser. :param proxy: str use a proxy for this request. :return: dict{'cookies'} this is NOT used in the code , but are left here for debugging purposes. """ t = str(random.uniform(0.9, 0.10)) url = 'http://tinychat.com/cauth/captcha?%s' % t response = web_request.get_request(url, json=True, proxy=proxy) if response is not None: if 'need_to_solve_captcha' in response['content']: successful_recaptcha = False link = '' try: if response['content']['need_to_solve_captcha'] == 1: link = 'http://tinychat.com/cauth/recaptcha?token=%s' % response['content']['token'] successful_recaptcha = True except (Exception, KeyError): if '"need_to_solve_captcha":1' in response['content']: token = response['content'].split('"token":"')[1].split('"')[0] link = 'http://tinychat.com/cauth/recaptcha?token=%s' % token if successful_recaptcha: print(link) if os.name == 'nt': webbrowser.open(link, new=1, autoraise=True) raw_input('Solve the captcha and hit enter to continue (OR hit enter to join anonymously).') return response['cookies']
def weather_search(city): """ Searches worldweatheronline's API for weather data for a given city. You must have a working API key to be able to use this function. :param city: The city str to search for. :return: weather data str or None on no match or error. """ if str(city).strip(): api_key = '' # A working API key. if not api_key: return 'Missing api key.' else: weather_api_url = 'http://api.worldweatheronline.com/free/v2/weather.ashx?' \ 'q=%s&format=json&key=%s' % (city, api_key) response = web_request.get_request(weather_api_url, json=True) if response['content'] is not None: try: pressure = response['content']['data']['current_condition'][0]['pressure'] temp_c = response['content']['data']['current_condition'][0]['temp_C'] temp_f = response['content']['data']['current_condition'][0]['temp_F'] query = response['content']['data']['request'][0]['query'] result = query + '. Tempature: ' + temp_c + 'C (' + temp_f + 'F) Pressure: ' + pressure + ' millibars' return result except KeyError: return None except IndexError: return None else: return None
def youtube_playlist_search(search, results=5): """ Searches youtube for a playlist matching the search term. :param search: str the search term to search to search for. :param results: int the number of playlist matches we want returned. :return: list[dict{'playlist_title', 'playlist_id'}] or None on failure. """ if search: youtube_search_url = 'https://www.googleapis.com/youtube/v3/search?' \ 'type=playlist&key=%s' \ '&maxResults=50&q=%s&part=snippet' % (YOUTUBE_API_KEY, search, ) api_response = web_request.get_request(youtube_search_url, json=True) if api_response is not None: play_lists = [] try: for item in api_response['content']['items']: playlist_id = item['id']['playlistId'] playlist_title = item['snippet']['title'].encode('ascii', 'ignore') play_list_info = {'playlist_title': playlist_title, 'playlist_id': playlist_id} play_lists.append(play_list_info) if len(play_lists) == results: return play_lists except KeyError as ke: log.error(ke, exc_info=True) return None else: return None
def youtube_playlist_search(search, results=5): """ Searches youtube for a playlist matching the search term. :param search: str the search term to search to search for. :param results: int the number of playlist matches we want returned. :return: list[ dict{'playlist_title', 'playlist_id'}] or None on failure. """ if str(search).strip(): youtube_search_url = 'https://www.googleapis.com/youtube/v3/search?' \ 'type=playlist&key=AIzaSyCPQe4gGZuyVQ78zdqf9O5iEyfVLPaRwZg' \ '&maxResults=50&q=%s&part=snippet' % search api_response = web_request.get_request(youtube_search_url, json=True) if api_response['content'] is not None: play_lists = [] try: for item in api_response['content']['items']: playlist_id = item['id']['playlistId'] playlist_title = item['snippet']['title'].encode( 'utf-8', 'replace') play_list_info = { 'playlist_title': playlist_title, 'playlist_id': playlist_id } play_lists.append(play_list_info) if len(play_lists) == results: return play_lists except KeyError: return None else: return None
def parse_privacy_settings(self, response=None): """ Parse privacy settings. """ header = {"Referrer": self._privacy_url} if response is None: response = web_request.get_request(self._privacy_url, header=header, proxy=self._proxy) if response is not None and response["content"] is not None: soup = BeautifulSoup(response["content"], "html.parser") # validation hash key validate = soup.find("input", {"type": "hidden", "name": "validate"}) self._validation_key = validate.get("value") # guest mode guest_settings = soup.find("select", {"name": "allowGuests"}) allow_guests_type = guest_settings.find("option", {"selected": True}) self._allow_guests = str(allow_guests_type.get("value")) # public directory listing directory_settings = soup.find("select", {"name": "public_directory"}) directory_value = directory_settings.find("option", {"selected": True}) self._public_directory = str(directory_value.get("value")) # push2talk push2talk_setting = soup.find("select", {"name": "push2talk"}) push2talk_value = push2talk_setting.find("option", {"selected": True}) self._push2talk = str(push2talk_value.get("value")) # green room greenroom_setting = soup.find("select", {"name": "greenroom"}) greenroom_value = greenroom_setting.find("option", {"selected": True}) self._greenroom = str(greenroom_value.get("value")) # moderators moderators = soup.findAll("input", {"type": "hidden", "name": "mods[]"}) if moderators: for mod in moderators: mod_account = str(mod.get("value")) if mod_account not in self.room_moderators: self.room_moderators.append(mod_account)
def youtube_search(search): """ Searches the youtube API for a youtube video matching the search term. A json response of ~50 possible items matching the search term will be presented. Each video_id will then be checked by youtube_time() until a candidate has been found and the resulting dict can be returned. :param search: The search term str to search for. :return: dict['type=youtube', 'video_id', 'int(video_time)', 'video_title'] or None on error. """ if str(search).strip(): youtube_search_url = 'https://www.googleapis.com/youtube/v3/search?' \ 'type=video&key=AIzaSyCPQe4gGZuyVQ78zdqf9O5iEyfVLPaRwZg' \ '&maxResults=50&q=%s&part=snippet' % search response = web_request.get_request(youtube_search_url, json=True) if response['content'] is not None: try: for item in response['content']['items']: video_id = item['id']['videoId'] video_title = item['snippet']['title'].encode('ascii', 'ignore') video_time = youtube_time(video_id) if video_time is not None: return {'type': 'youTube', 'video_id': video_id, 'video_time': video_time['video_time'], 'video_title': video_title} except KeyError: return None else: return None
def _is_tc_account(account_name): """ Helper method to check if a user account is a valid account name. :param account_name: str the account name to check. :return: bool True if it is a valid account, False if invalid account """ url = "http://tinychat.com/api/tcinfo?username=%s" % account_name json_data = web_request.get_request(url=url, json=True) if json_data is not None and json_data["content"] is not None: if "error" not in json_data["content"]: return True return False
def _is_tc_account(account_name): """ Helper method to check if a user account is a valid account name. :param account_name: str the account name to check. :return: bool True if it is a valid account, False if invalid account """ url = 'http://tinychat.com/api/tcinfo?username=%s' % account_name json_data = web_request.get_request(url=url, json=True) if json_data is not None and json_data['content'] is not None: if 'error' not in json_data['content']: return True return False
def chuck_norris(): """ Finds a random Chuck Norris joke/quote. :return: joke str or None on failure. """ url = 'http://api.icndb.com/jokes/random/?escape=javascript' json_data = web_request.get_request(url, json=True) if json_data['content']['type'] == 'success': joke = json_data['content']['value']['joke'].decode('string_escape') return joke else: return None
def yo_mama_joke(): """ Retrieves a random 'Yo Mama' joke from an API. :return: joke str or None on failure. """ url = 'http://api.yomomma.info/' json_data = web_request.get_request(url, json=True) if json_data['content']: joke = json_data['content']['joke'].decode('string_escape') return joke else: return None
def online_advice(): """ Retrieves a random string of advice from an API. :return: advice str or None on failure. """ url = 'http://api.adviceslip.com/advice' json_data = web_request.get_request(url, json=True) if json_data['content']: advice = json_data['content']['slip']['advice'].decode('string_escape') return str(advice) else: return None
def one_liners(tag=None): """ Retrieves a one-liner from http://onelinefun.com/ (by choosing a random category). :param tag: str a specific tag name from which you want to choose a joke from. :return: joke: str a one line joke/statement (depending on category). """ if BeautifulSoup is not None: url = "http://onelinefun.com/" if tag: joke_url = url + str(tag) + "/" else: global tags # Select a random tag from the list if one has not been provided joke_tag = random.randint(0, len(tags) - 1) joke_url = url + tags[joke_tag] + "/" raw = web_request.get_request(url=joke_url) if raw['status_code'] == 200: html = raw['content'] soup = BeautifulSoup(html, "html.parser") jokes = soup.findAll("p") if jokes: all_jokes = [] for x in xrange(len(jokes)): individual_joke = unicodedata.normalize( 'NFKD', jokes[x].getText()).encode('ascii', 'ignore') all_jokes.append(individual_joke) if len(all_jokes) is not 0: del all_jokes[0] for x in range(6): del all_jokes[len(all_jokes) - 1] joke = str(all_jokes[random.randint(0, len(all_jokes) - 1)]) return joke else: return None else: return None else: return None else: return None
def chuck_norris(): """ Finds a random Chuck Norris joke/quote from http://www.icndb.com/api/ . The API also has category specifications, i.e. categories are either "nerdy"/"explicit" set via webform "?limtTo". The character names can also be altered via passing the webform "?firstName=[name]" or "?lastName=[name]". :return: joke str or None on failure. """ url = 'http://api.icndb.com/jokes/random/?escape=javascript' json_data = web_request.get_request(url, json=True) if json_data['content']['type'] == 'success': joke = json_data['content']['value']['joke'].decode('string_escape') return joke else: return None
def get_captcha_key(roomname, uid, proxy=None): """ Find the captcha key needed before we can send messages in a room. :param roomname: str the room name. :param uid: str the ID we have in the room. :param proxy: str use a proxy for this request. :return: str the captcha key or None on captcha enabled room. """ url = 'http://tinychat.com/api/captcha/check.php?room=tinychat^%s&guest_id=%s' % (roomname, uid) json_data = web_request.get_request(url, json=True, proxy=proxy) if 'key' in json_data['content']: return json_data['content']['key'] else: return None
def recaptcha(proxy=None): """ Check if we have to solve a captcha before we can connect. If yes, then it will open in the default browser. :param proxy: str use a proxy for this request. :return: dict{'cookies'} this NOT used in the code , but are left here for debugging purpose. """ t = str(random.uniform(0.9, 0.10)) url = 'http://tinychat.com/cauth/captcha?%s' % t response = web_request.get_request(url, json=True, proxy=proxy) if response['content']['need_to_solve_captcha'] == 1: link = 'http://tinychat.com/cauth/recaptcha?token=%s' % response['content']['token'] webbrowser.open(link, new=1) raw_input('Click enter to continue.') return response['cookies']
def youtube_playlist_videos(playlist_id): """ Finds the video info for a given playlist ID. The list returned will contain all the videos in the playlist; retrieves all video information using pageToken. :param playlist_id: str the playlist ID :return: list[dict{'type=youTube', 'video_id', 'video_title', 'video_time'}] or None on failure. """ playlist_details_url = 'https://www.googleapis.com/youtube/v3/playlistItems?' \ 'key=%s&playlistId=%s' \ '&maxResults=50&part=contentDetails' % (YOUTUBE_API_KEY, playlist_id, ) video_list = [] start_token = True non_public = 0 while start_token: api_response = web_request.get_request(playlist_details_url, json=True) if api_response is not None: try: pageToken = str(api_response['content']['nextPageToken']) playlist_details_url = 'https://www.googleapis.com/youtube/v3/playlistItems?' \ 'key=%s&playlistId=%s' \ '&maxResults=50&part=status,contentDetails&pageToken=%s' % (YOUTUBE_API_KEY, playlist_id, pageToken, ) except KeyError as ke: log.error(ke, exc_info=True) start_token = False try: for item in api_response['content']['items']: if item['status']['privacyStatus'] != "public": non_public += 1 else: video_id = item['contentDetails']['videoId'] # video_title = item['snippet']['title'].encode('ascii', 'ignore') video_time = youtube_time(video_id) if video_time is not None: info = {'type': 'youTube', 'video_id': video_id, 'video_title': video_time['video_title'], 'video_time': video_time['video_time']} video_list.append(info) # print info['video_title'] # debug only except Exception as e: log.error(e) pass return video_list, non_public
def one_liners(tag=None): """ Retrieves a one-liner from http://onelinefun.com/ (by choosing a random category). :param tag: str a specific tag name from which you want to choose a joke from. :return: joke: str a one line joke/statement (depending on category). """ if BeautifulSoup is not None: url = "http://onelinefun.com/" if tag: joke_url = url + str(tag) + "/" else: global tags # Select a random tag from the list if one has not been provided joke_tag = random.randint(0, len(tags) - 1) joke_url = url + tags[joke_tag] + "/" raw = web_request.get_request(url=joke_url) if raw['status_code'] == 200: html = raw['content'] soup = BeautifulSoup(html, "html.parser") jokes = soup.findAll("p") if jokes: all_jokes = [] for x in xrange(len(jokes)): individual_joke = unicodedata.normalize('NFKD', jokes[x].getText()).encode('ascii', 'ignore') all_jokes.append(individual_joke) if len(all_jokes) is not 0: del all_jokes[0] for x in range(6): del all_jokes[len(all_jokes) - 1] joke = str(all_jokes[random.randint(0, len(all_jokes) - 1)]) return joke else: return None else: return None else: return None else: return None
def get_cauth_cookie(roomname, proxy=None): """ Find the cauth 'cookie' needed to make a successful connection. This is not really a cookie, but named so after its name in the json response. :param roomname: str the room name. :param proxy: str use a proxy for this request. :return: str the 'cookie' """ ts = int(round(time.time() * 1000)) url = 'http://tinychat.com/cauth?room=%s&t=%s' % (roomname, str(ts)) json_data = web_request.get_request(url, json=True, proxy=proxy) if 'cookie' in json_data['content']: return json_data['content']['cookie'] else: return None
def recaptcha(proxy=None): """ Check if we have to solve a captcha before we can connect. If yes, then it will open in the default browser. :param proxy: str use a proxy for this request. :return: dict{'cookies'} this is NOT used in the code , but are left here for debugging purpose. """ t = str(random.uniform(0.9, 0.10)) url = 'http://tinychat.com/cauth/captcha?%s' % t response = web_request.get_request(url, json=True, proxy=proxy) # print 'in recaptcha' if response is not None: # print response['content'] try: # print 'normal recaptcha token retrieval' # Handle abnormal response regarding the recaptcha data to work by forcing a # text parse for the recaptcha information if response['content']['need_to_solve_captcha'] == 1: # print 'normal recaptcha token used' link = 'http://tinychat.com/cauth/recaptcha?token=%s' % response[ 'content']['token'] print(link) if os.name == 'nt': webbrowser.open(link, new=1) raw_input( 'Solve the captcha and hit enter to continue (OR hit enter to join anonymously).' ) else: # print 'normal recaptcha retrieval not used' pass except Exception: # print 'text recaptcha token retrieval' if '"need_to_solve_captcha":1' in response['content']: # print 'text recaptcha token used' # credits to pinychat (NotNola) token = response['content'].split('"token":"')[1].split('"')[0] link = 'http://tinychat.com/cauth/recaptcha?token=%s' % token print(link) if os.name == 'nt': webbrowser.open(link, new=1) raw_input( 'Solve the captcha and hit enter to continue (OR hit enter to join anonymously).' ) else: # print 'text recaptcha retrieval not used' pass
def get_roomconfig_xml(room, roompass=None, proxy=None): """ Finds room configuration for a given room name. :param room: str the room name to find info for. :param roompass: str the password to the room. Defaults to None. :param proxy: str use a proxy for this request. :return: dict {'tcurl', 'ip', 'port', 'app', 'roomtype', 'greenroom=bool', 'roombroadcastpass'} """ if roompass: # TODO: api ---> apl xmlurl = 'http://api.tinychat.com/api/find.room/%s?site=tinychat&password=%s&url=tinychat.com' % \ (room, roompass) else: # TODO: api ---> apl xmlurl = 'http://api.tinychat.com/api/find.room/%s?site=tinychat&url=tinychat.com' % room web_content = web_request.get_request(xmlurl, proxy=proxy) if web_content is not None: xml = parseString(web_content['content']) root = xml.getElementsByTagName('response')[0] result = root.getAttribute('result') if result == 'PW': return result else: roomtype = root.getAttribute('roomtype') tc_url = root.getAttribute('rtmp') rtmp_parts = tc_url.split('/') app = rtmp_parts[3] ip_port_parts = rtmp_parts[2].split(':') ip = ip_port_parts[0] port = int(ip_port_parts[1]) room_broadcast_pass = None if root.getAttribute('greenroom'): greenroom = True try: room_broadcast_pass = root.getAttribute('bpassword') except Exception: pass else: greenroom = False return {'tcurl': tc_url, 'ip': ip, 'port': port, 'app': app, 'roomtype': roomtype, \ 'greenroom': greenroom, 'roombroadcastpass': room_broadcast_pass}
def tinychat_user_info(tc_account): """ Finds info for a given tinychat account name. :param tc_account: str the account name. :return: dict {'username', 'tinychat_id', 'last_active'} """ xmlurl = 'http://tinychat.com/api/tcinfo?format=xml&username=%s' % tc_account web_content = web_request.get_request(xmlurl) try: xml = parseString(web_content['content']) root = xml.getElementsByTagName('result')[0] username = root.getAttribute('username') user_id = root.getAttribute('id') last_active = time.ctime(int(root.getAttribute('last_active'))) return {'username': username, 'tinychat_id': user_id, 'last_active': last_active} except: return None
def tinychat_user_info(tc_account): """ Finds info for a given tinychat account name. :param tc_account: str the account name. :return: dict {'username', 'tinychat_id', 'last_active', 'name', 'location'}. """ url = 'http://tinychat.com/api/tcinfo?username=%s' % tc_account json_data = web_request.get_request(url=url, json=True) if json_data is not None: try: user_id = json_data['content']['id'] username = json_data['content']['username'] name = json_data['content']['name'] location = json_data['content']['location'] last_active = time.ctime(int(json_data['content']['last_active'])) return {'username': username, 'tinychat_id': user_id, 'last_active': last_active, 'name': name, 'location': location} except KeyError: return None