def ten(channel, ranking): channel = channel.lower() if channel not in self.scores_dict: return self.STRINGS["nochan"].format(channel) q = 0 top_scores = list() if ranking == 'b': tob = 'Bottom' elif ranking == 't': tob= 'Top' str_say = "\x02%s 10 (for %s):\x02" % (tob, channel) sort = True if ranking == 'b': sort = False scores = sorted(iter(self.scores_dict[channel].items()), key=lambda k_v: (k_v[1][0] - k_v[1][1]), reverse=sort) for key, value in scores: top_scores.append(self.str_score(key, channel)) if len(scores) == q + 1: str_say += ' %s' % (uc.decode(top_scores[q])) else: str_say += ' %s |' % (uc.decode(top_scores[q])) q += 1 if q > 9: break return str_say
def short(text): """ This function creates a bitly url for each url in the provided string. The return type is a list. """ if not bitly_loaded: return list() if not text: return list() bitlys = list() try: a = re.findall(url_finder, text) k = len(a) i = 0 while i < k: b = uc.decode(a[i][0]) ## make sure that it is not already a bitly shortened link if not is_bitly(b): longer = urllib.parse.quote(b) url = 'https://api-ssl.bitly.com/v3/shorten?login=%s' % (bitly_user) url += '&apiKey=%s&longUrl=%s&format=txt' % (bitly_api_key, longer) #shorter = proxy.get(url) shorter = web.get(url) shorter.strip() bitlys.append([b, shorter]) else: bitlys.append([b, str()]) i += 1 return bitlys except: return return bitlys
def short(text): """ This function creates a bitly url for each url in the provided string. The return type is a list. """ if not bitly_loaded: return list() if not text: return list() bitlys = list() try: a = re.findall(url_finder, text) k = len(a) i = 0 while i < k: b = uc.decode(a[i][0]) ## make sure that it is not already a bitly shortened link if not is_bitly(b): longer = urllib.parse.quote(b) url = 'https://api-ssl.bitly.com/v3/shorten?login=%s' % ( bitly_user) url += '&apiKey=%s&longUrl=%s&format=txt' % (bitly_api_key, longer) #shorter = proxy.get(url) shorter = web.get(url) shorter.strip() bitlys.append([b, shorter]) else: bitlys.append([b, str()]) i += 1 return bitlys except: return return bitlys
def save(self): """ Save to file in comma seperated values """ os.rename(self.scores_filename, '%s-%s' % (self.scores_filename, str(time.time()))) scores_file = codecs.open(self.scores_filename, 'w', encoding='utf-8') for each_chan in self.scores_dict: for each_nick in self.scores_dict[each_chan]: line = '{0},{1},{2},{3}\n'.format(each_chan, each_nick, self.scores_dict[each_chan][each_nick][0], self.scores_dict[each_chan][each_nick][1]) scores_file.write(uc.decode(line)) scores_file.close()
def ep(m): entity = m.group() if entity.startswith('&#x'): cp = int(entity[3:-1], 16) meep = chr(cp) elif entity.startswith('&#'): cp = int(entity[2:-1]) meep = chr(cp) else: entity_stripped = entity[1:-1] try: char = name2codepoint[entity_stripped] meep = chr(char) except: if entity_stripped in HTML_ENTITIES: meep = HTML_ENTITIES[entity_stripped] else: meep = str() try: return uc.decode(meep) except: return uc.decode(uc.encode(meep))
def get_results(text, manual=False): if not text: return False, list() a = re.findall(url_finder, text) k = len(a) i = 0 display = list() passs = False channel = str() if hasattr(text, 'sender'): channel = text.sender while i < k: url = uc.encode(a[i][0]) url = uc.decode(url) url = uc.iriToUri(url) url = remove_nonprint(url) domain = getTLD(url) if '//' in domain: domain = domain.split('//')[1] if 'i.imgur.com' in url and url.startswith('http://'): url = url.replace('http:', 'https:') bitly = url if not url.startswith(EXCLUSION_CHAR): passs, page_title = find_title(url) if not manual: if bitly_loaded: if channel and channel not in simple_channels: bitly = short(url) if bitly: bitly = bitly[0][1] display.append([page_title, url, bitly, passs]) else: ## has exclusion character if manual: ## only process excluded URLs if .title is used url = url[1:] passs, page_title = find_title(url) display.append([page_title, url, bitly, passs]) i += 1 ## check to make sure at least 1 URL worked correctly overall_pass = False for x in display: if x[-1] == True: overall_pass = True return overall_pass, display
def show_title_auto(jenni, input): '''No command - Automatically displays titles for URLs''' for each in BLOCKED_MODULES: if input.startswith('.%s ' % (each)): ## Don't want it to show duplicate titles return if hasattr(jenni.config, 'auto_title_disable_chans'): disabled_channels = jenni.config.auto_title_disable_chans if input.sender in disabled_channels or (input.sender).lower() in disabled_channels: return if len(re.findall('\([\d]+\sfiles\sin\s[\d]+\sdirs\)', input)) == 1: ## Directory Listing of files return try: status, results = get_results(input) except Exception as e: print('[%s]' % e, input) return k = 1 output_shorts = str() results_len = len(results) for r in results: ## loop through link, shorten pairs, and titles returned_title = r[0] orig = r[1] bitly_link = r[2] link_pass = r[3] if orig and bitly_link and bitly_link != orig and ('bit.ly' in bitly_link or 'j.mp' in bitly_link): ## if we get back useful data ## and we have a bitly link (bitly worked!) ## and the shortened link is 'valid' ## let's make it 'https' instead of 'http' bitly_link = bitly_link.replace('http:', 'https:') if 'imgur: the most awesome images on the internet' in (returned_title).lower(): ## because of the i.imgur hack above this is done ## to prevent from showing useless titles on image ## files return if k > 3: ## more than 3 titles to show from one line of text? ## let's just show only the first 3. break k += 1 ## deteremine if we should display the bitly link useBitLy = doUseBitLy(returned_title, orig) reg_format = '[ %s ] - %s' special_format = '[ %s ]' response = str() if status and link_pass: if useBitLy and input.sender not in simple_channels and bitly_link: response = reg_format % (uc.decode(returned_title), bitly_link) else: if input.sender in simple_channels: response = special_format % (returned_title) else: response = reg_format % (returned_title, getTLD(orig)) elif len(orig) > BITLY_TRIGGER_LEN_NOTITLE: if useBitLy and bitly_link != orig: #response = '%s' % (bitly_link) output_shorts += bitly_link + ' ' else: ## Fail silently, link can't be bitly'ed and no title was found pass if response: jenni.say(response) if output_shorts: jenni.say((output_shorts).strip())
def find_title(url): """ This finds the title when provided with a string of a URL. """ for item in IGNORE: if item in url: return False, 'ignored' if not re.search('^((https?)|(ftp))://', url): url = 'http://' + url if '/#!' in url: url = url.replace('/#!', '/?_escaped_fragment_=') if 'i.imgur' in url: a = url.split('.') url = a[0][:-1] + '.'.join(a[1:-1]) if 'zerobin.net' in url: return True, 'ZeroBin' url = uc.decode(url) msg = str() k = 0 status = False while not status: k += 1 if k > 3: break msg = dict() try: status, msg = proxy.get_more(url) except: try: status, msg = get_page_backup(url) except: continue if type(msg) == type(dict()) and 'code' in msg: status = msg['code'] else: continue time.sleep(0.5) if not status: return False, msg useful = msg info = useful['headers'] page = useful['read'] try: mtype = info['content-type'] except: print('failed mtype:', str(info)) return False, 'mtype failed' if not (('/html' in mtype) or ('/xhtml' in mtype)): return False, str(mtype) content = page regex = re.compile('<(/?)title( [^>]+)?>', re.IGNORECASE) content = regex.sub(r'<\1title>', content) regex = re.compile('[\'"]<title>[\'"]', re.IGNORECASE) content = regex.sub('', content) start = content.find('<title>') if start == -1: return False, 'NO <title> found' end = content.find('</title>', start) if end == -1: return False, 'NO </title> found' content = content[start + 7:end] content = content.strip('\n').rstrip().lstrip() title = content if len(title) > 200: title = title[:200] + '[...]' def e(m): entity = m.group() if entity.startswith('&#x'): cp = int(entity[3:-1], 16) meep = chr(cp) elif entity.startswith('&#'): cp = int(entity[2:-1]) meep = chr(cp) else: entity_stripped = entity[1:-1] try: char = name2codepoint[entity_stripped] meep = chr(char) except: if entity_stripped in HTML_ENTITIES: meep = HTML_ENTITIES[entity_stripped] else: meep = str() try: return uc.decode(meep) except: return uc.decode(uc.encode(meep)) title = r_entity.sub(e, title) title = title.replace('\n', '') title = title.replace('\r', '') def remove_spaces(x): if ' ' in x: x = x.replace(' ', ' ') return remove_spaces(x) else: return x title = remove_spaces(title) new_title = str() for char in title: unichar = uc.encode(char) if len(list(uc.encode(char))) <= 3: new_title += uc.encode(char) title = new_title title = re.sub(r'(?i)dcc\ssend', '', title) title += '\x0F' if title: return True, title else: return False, 'No Title'
def show_title_auto(jenni, input): '''No command - Automatically displays titles for URLs''' for each in BLOCKED_MODULES: if input.startswith('.%s ' % (each)): ## Don't want it to show duplicate titles return if hasattr(jenni.config, 'auto_title_disable_chans'): disabled_channels = jenni.config.auto_title_disable_chans if input.sender in disabled_channels or ( input.sender).lower() in disabled_channels: return if len(re.findall('\([\d]+\sfiles\sin\s[\d]+\sdirs\)', input)) == 1: ## Directory Listing of files return try: status, results = get_results(input) except Exception as e: print('[%s]' % e, input) return k = 1 output_shorts = str() results_len = len(results) for r in results: ## loop through link, shorten pairs, and titles returned_title = r[0] orig = r[1] bitly_link = r[2] link_pass = r[3] if orig and bitly_link and bitly_link != orig and ( 'bit.ly' in bitly_link or 'j.mp' in bitly_link): ## if we get back useful data ## and we have a bitly link (bitly worked!) ## and the shortened link is 'valid' ## let's make it 'https' instead of 'http' bitly_link = bitly_link.replace('http:', 'https:') if 'imgur: the most awesome images on the internet' in ( returned_title).lower(): ## because of the i.imgur hack above this is done ## to prevent from showing useless titles on image ## files return if k > 3: ## more than 3 titles to show from one line of text? ## let's just show only the first 3. break k += 1 ## deteremine if we should display the bitly link useBitLy = doUseBitLy(returned_title, orig) reg_format = '[ %s ] - %s' special_format = '[ %s ]' response = str() if status and link_pass: if useBitLy and input.sender not in simple_channels and bitly_link: response = reg_format % (uc.decode(returned_title), bitly_link) else: if input.sender in simple_channels: response = special_format % (returned_title) else: response = reg_format % (returned_title, getTLD(orig)) elif len(orig) > BITLY_TRIGGER_LEN_NOTITLE: if useBitLy and bitly_link != orig: #response = '%s' % (bitly_link) output_shorts += bitly_link + ' ' else: ## Fail silently, link can't be bitly'ed and no title was found pass if response: jenni.say(response) if output_shorts: jenni.say((output_shorts).strip())