def get_country_name_and_flag(phone): try: country = phone_country(phone) return flag.flag(country) + country except: None return flag.flagize(":MX: MX")
def socialmedia(bot, update): bot.send_message( chat_id=update.message.chat_id, text=flag.flagize( "[Twitter](https://twitter.com/BitMartExchange)\n[Facebook](https://www.facebook.com/bitmartexchange)\n[Reddit](https://www.reddit.com/r/bitmartexchange)\n[Medium](https://medium.com/@bitmart.exchange)\n[Steemit](https://steemit.com/@bitmart)\n[Bitcointalk](https://bitcointalk.org/index.php?action=profile;u=1376628;sa=showPosts)\n[Instagram](https://www.instagram.com/bitmart_exchange)\n[LinkedIn](https://www.linkedin.com/company/bitmart)\n[Weibo](https://weibo.com/u/6424218806)\nMulti-lanuguage telegram group:\n[Telegram-Channel](t.me/BitMartExchange_Channel)\n:US:-[English-Telegram](t.me/BitMartExchange)\n:CN:-[Chinese-Telegram](t.me/BitMart_China)\n:VN:-[Vietnam-Telegram](t.me/BitMartExchange_Vietnam)\n:JP:-[Japanese-Telegram](t.me/BitMartExchange_Japanese)\n:KR:-[SouthKorea-Telegram](t.me/BitMartExchange_SouthKorea)\n:IN:-[India-Telegram](t.me/BitMartExchange_India)\n:RU:-[Russia-Telegram](t.me/BitMartExchange_Russia)\n:ES:-[Spanish-Telegram](t.me/BitMartExchange_Spanish)\n:ID:-[Indonesia-Telegram](t.me/BitMartExchange_Indonesia)\n:PH:-[Philippines-Telegram](t.me/BitMartExchange_Philippines)\n:NG:-[Nigeria-Telegram](https://t.me/joinchat/GI-JPlCCfDLTDHb_YBKc6Q)\n:TR:-[Turkish-Telegram](https://t.me/Bitmart_Turkish)\n:IT:-[Italy-Telegram](https://t.me/bitmart_italy)\n:FR:-[France-Telegram](https://t.me/bitmart_france)\n:DE:-[German-Telegram](https://t.me/BitMartExchange_Germany)"), parse_mode=telegram.ParseMode.MARKDOWN, disable_web_page_preview=True)
def send_documents(bot, update): # a function that sends documents to the user bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) inline_keyboard = [ [InlineKeyboardButton('Бланк запроса на Терапевтическое Использование', url='https://drive.google.com/open?id=1zma8UD8YRvMHEMdXlNNK2_Rs4A8Cm4mK')], [InlineKeyboardButton('Запрещенный список 2019', url='https://drive.google.com/open?id=1WtA4fXrLcozGTTvaCW9R6jGtCEkuJBIq')], [InlineKeyboardButton('Заявление о завершении карьеры', url='https://drive.google.com/open?id=14qKfdlWNfla-LKPsyRQ8PnIgkwU-Or7S')], [InlineKeyboardButton('Инструкция по заполнению системы ADAMS', url='https://drive.google.com/open?id=1gt0VL2_HjJmEjwN-aY6JLeEJaCsnkpOU')], [InlineKeyboardButton('Международный стандарт по терапевтическому использованию', url='https://drive.google.com/open?id=1R4SGuAaSIpntYEQTzpy6SMpGekBGsK97')], [InlineKeyboardButton('Обзор основных изменений в Запрещенном списке 2019', url='https://drive.google.com/open?id=1I3a6QTyVUm6tifrF7XjIBQ6u88-Io9Q0')], [InlineKeyboardButton('Положение по терапевтическому использованию',url='https://drive.google.com/open?id=1u_lGQdDbx_sziXIuTouBs0RWoQ0cTm1u')], [InlineKeyboardButton('Программа мониторинга 2019', url='https://drive.google.com/open?id=1CrGj-eXl5EqmPxBXyki5m9_PgI9vb_pQ')], [InlineKeyboardButton('2020-Prohibited-List-RUS', url='https://drive.google.com/open?id=1sgEq5mdVykQNPF2M0IAih0PlpJjxYmVE')], [InlineKeyboardButton('Программа мониторинга 2020', url='https://drive.google.com/open?id=1MjQWARhEnsoPSTZpHjPZmB_sEzFJw2nQ')] ] # an array of inline keyboard elements (documents) inline_markup = telegram.InlineKeyboardMarkup(inline_keyboard) # inline keyboard markup for downloadable documents bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Өте жақсы! Қажетті құжатты таңдаңыз'+ '\n\n' + flag.flagize(':RU:') + ' Отлично! Выберите нужный вам документ', reply_markup=inline_markup, parse_mode=telegram.ParseMode.MARKDOWN) # the message which will be sent with the keyboard (it uses flags to show the language). Markdown mode in ON if(type_of_user_int == 0): # checks if user is a guest guest_login(bot, update) # runs the login function for guest elif(type_of_user_int == 1): # checks if user is an athlete athlete_login(bot, update)
def parse_vaccination_data(self, city): data_list = self.vaccination_data_list country_row = [] for row in data_list[:-1]: country_name = row[2] country_code = row[0] if (country_name.lower() == city.lower() or country_code.lower() == city.lower()): country_row = row if country_row: country_iso = pycountry.countries.search_fuzzy( country_row[2])[0].alpha_2 vaccinated_int = int(float( country_row[35])) if country_row[35] else 0 population = "{:.2f}".format((float(country_row[46]) / 1000000)) people_vaccinated = format((vaccinated_int), ",").replace(",", " ") people_vaccinated_per_100 = country_row[41] people_fully_vaccinated_per_100 = country_row[42] result = flag.flagize(( f"<b>{city}</b> :{country_iso}: სულ მოსახლეობა: 🧍 <b>{population} მილიონი</b>\n" f"აცრილი მოსახლეობა: 💉 <b>{people_vaccinated if people_vaccinated else 0}</b>\n" f"აცრილი: 📈 <b>{people_vaccinated_per_100 if people_vaccinated_per_100 else 0} %</b>\n" f"ორივე აცრა: 📊 <b>{people_fully_vaccinated_per_100 if people_fully_vaccinated_per_100 else 0} %</b>\n" )) return result else: return data_not_found()
def get_race_name(rid): race = races.loc[rid] circuit = circuits.loc[race["circuitId"]] nat = circuit["country"].lower() name = circuit["country"] flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) return name
def get_driver_name(did): driver = drivers.loc[did] name = driver["forename"] + " " + driver["surname"] nat = driver["nationality"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) return name
def send_phone_number(bot, update): # a function that allows the user to send phone number to authorize contact_keyboard = telegram.KeyboardButton(text="Телефон нөмірін жіберу/Отправить номер телефона", request_contact=True) # a customization of the keyboard that will send the phone number of the user custom_keyboard = [[contact_keyboard ]] # custom keyboard, which contains send the telephone number button reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the previous message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Бізге өз деректеріңізді жіберіңіз'+ '\n\n' + flag.flagize(':RU:') + " Отправьте нам свои данные", reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove()
def get_constructor_name(cid): try: constructor = constructors.loc[cid] name = constructor["name"] nat = constructor["nationality"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) return name except KeyError: return "UNKNOWN"
def guest_login(bot, update): # a function that provides options for the guest bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) custom_keyboard = [['Құжаттар тізімі/Список документов', 'FAQ'], ['Сұрақ қою/Задать вопрос'], ['Артқа/Назад']] # custom keyboard, which contains documents, FAQ, ask a question and go back buttons reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the previous message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Сіз не істегіңіз келеді?'+ '\n\n' + flag.flagize(':RU:') + ' Что вы хотите сделать?', reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove()
def send_data(bot, update): # a function that sends athletes' choice of data to send bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) custom_keyboard = [['Тамақтану/Питание'], ['Орналасуы/Местоположение'], ['Артқа/Назад']] # custom keyboard, which contains food data, location and go back buttons reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the previous message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':RU:') + ' Какие данные вы хотите отправить?' + '\n\n' + flag.flagize(':KZ:') + ' Қандай мәліметтерді жібергіңіз келеді?', reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove()
def get_race_name(rid, include_flag=True, include_country=True, line_br=None, use_shortened=False, include_year=False): """ Gets the stylized version of the given race's name :param rid: Race ID :param include_flag: Whether to include the nationality flag in the constructor's name :param include_country: Whether to use the full race name or just the country name :param line_br: Character to use for line break, or None if not desired :param use_shortened: Use shortened version of GP name :param include_year: Whether to include the year in the name, only works if `use_shortened` and `line_br` are both False :return: String """ global circuits, races if circuits is None: circuits = load_circuits() if races is None: races = load_races() race = races.loc[rid] circuit = circuits.loc[race["circuitId"]] name = circuit["country"] if include_country else race["name"] if use_shortened: name = name[:3].upper() if include_flag: nat = circuit["country"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] if line_br: name = flag.flagize(f"{name} {line_br} :{flag_t}:") else: name = flag.flagize(f":{flag_t}: " + name) else: logging.warning(f"Unknown nationality {nat}, race ID: {rid}") if include_year: name = str(race["year"]) + " " + name return name
def emojify(country): if country == 'England': return 'ЪЈ┤заЂДзаЂбзаЂЦзаЂ«заЂДзаЂ┐заЂДзаЂбзаЂ│заЂБзаЂ┤заЂ┐' if country == 'Scotland': return 'ЪЈ┤заЂДзаЂбзаЂ│заЂБзаЂ┤заЂ┐' if country == 'Russia': country = 'Russian Federation' try: alpha_2 = pycountry.countries.get(name=country).alpha_2 emoji = flag.flagize(f':{alpha_2}:') except Exception: return 'ЪЈ┤РђЇРўа№ИЈ' return emoji
def start(bot, update): # a function, which will start to work after clicking /start in the chat bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) custom_keyboard = [['Спортшы/Спортсмен'], ['Қонақ/Гость']] # custom keyboard, which contains athlete and guest buttons reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the /start message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Сіз кімсіз?' + '\n\n' + flag.flagize(':RU:') +' Кто вы?', reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove() # removes keyboard after user's response return TYPE
def athlete_auth(bot, update): # a function that will run to allow athlete to authorize bot.send_chat_action(chat_id=update.message.chat_id, action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) custom_keyboard = [['Иә/Да', 'Жоқ/Нет']] # custom keyboard, which contains yes and no buttons reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the previous message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Авторланғыңыз келе ме?'+ '\n\n' + flag.flagize(':RU:') + ' Хотите авторизоваться?', reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove() # removes keyboard after user's response return FIRST
def data_choice(bot, update): # a function that operates depending on what choice the user made if(update.message.text == 'Орналасуы/Местоположение'): # checks if the user chose to send location location_keyboard = telegram.KeyboardButton(text="Орналасуын жіберу/Отправить местоположение " + emoji.emojize(':round_pushpin:'), request_location=True) # customization of the keyboard to send the location of user custom_keyboard = [[location_keyboard ]] # places the location keyboard on the main custom keyboard reply_markup = telegram.ReplyKeyboardMarkup(custom_keyboard) # reply markup of the keyboard (will be sended in reply to the previous message) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Өз орналасуын бізге жіберіңіз'+ '\n\n' + flag.flagize(':RU:') + " Отправьте нам своё местоположение", reply_markup=reply_markup) # the message which will be sent with the keyboard (it uses flags to show the language) reply_markup = telegram.ReplyKeyboardRemove() # removes keyboard after user's response return LOCATION # returns LOCATION state of the conversation elif(update.message.text == 'Тамақтану/Питание'): # checks if the user chose to send food data bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Сіз немен тамақтандыңыз немесе қолдандыңыз? (өнімдерді үтір арқылы жазыңыз)'+ '\n\n' + flag.flagize(':RU:') + ' Что вы ели или употребляли? (напишите через запятую продукты)', reply_markup = ForceReply(force_reply=True)) # the message which will be sent with the keyboard (it uses flags to show the language). Force Reply will bind the user to type the reply to the message return FOOD # returns FOOD state of the conversation # elif(update.message.text == 'Экскреция/Экскреция'): # bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # time.sleep(1) # bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Сіздің экскреция процесін сипаттаңыз'+ '\n\n' + flag.flagize(':RU:') + ' Опишите ваш процесс экскреции', reply_markup = ForceReply(force_reply=True)) # return EXCRETE elif(update.message.text == 'Артқа/Назад'): # checks if the user chose to go back in the conversation athlete_login(bot, update) # runs previous function to continue conversation return ATHLETE
def get_circuit_name(cid, include_flag=True): """ Gets the stylized version of the given circuit's name :param cid: Circuit ID :param include_flag: Whether to include the nationality flag in the constructor's name :return: String """ global circuits if circuits is None: circuits = load_circuits() circuit = circuits.loc[cid] name = circuit["name"] if include_flag: nat = circuit["country"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) else: logging.warning(f"Unknown nationality {nat}, circuit ID: {cid}") return name
def getEmoji(aCountry): """ Get country emoji """ bEmoji = "" try: if aCountry in Config.dCountryCodes: bEmoji = flag.flagize(":%s:" % (Config.dCountryCodes[aCountry])) except Exception: myLogger.exception( "Exception in [file: %s | module: %s | line: %s]: %s", os.path.basename(traceback.extract_stack()[-1][0]), traceback.extract_stack()[-1][2], traceback.extract_stack()[-1][1], sys.exc_info()[1], ) return None return bEmoji
def get_driver_name(did, include_flag=True, just_last=False): """ Gets the stylized version of the given driver's name :param did: Driver ID :param include_flag: Whether to include the nationality flag in the driver's name :param just_last: Whether to include the first name :return: String """ global drivers if drivers is None: drivers = load_drivers() driver = drivers.loc[did] if just_last: name = driver["surname"] else: name = driver["forename"] + " " + driver["surname"] if include_flag: nat = driver["nationality"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) else: logging.warning(f"Unknown nationality {nat}, driver ID: {did}") return name
def get_constructor_name(cid, include_flag=True): """ Gets the stylized version of the given constructor's name :param cid: Constructor ID :param include_flag: Whether to include the nationality flag in the constructor's name :return: String """ global constructors if constructors is None: constructors = load_constructors() try: constructor = constructors.loc[cid] name = constructor["name"] if include_flag: nat = constructor["nationality"].lower() if nat in NATIONALITY_TO_FLAG.index: flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] name = flag.flagize(f":{flag_t}: " + name) else: logging.warning( f"Unknown nationality {nat}, constructor ID: {cid}") return name except KeyError: return "UNKNOWN"
def cbstats(update, context): chat_id = update.message.chat_id message = "" try: if len(context.args) == 0: stats, updated = data.get_global_cases() message = "*Global stats* " else: country = " ".join(context.args) stats, country_info, updated = data.get_country_cases(country) message = flagize(f":{country_info['country_code']}: " f"*{country_info['country_name']}* ") last_updated = datetime.fromtimestamp(int(updated) / 1000).strftime("%Y-%m-%d %H:%M") message += f"({last_updated})\n" message += formatting.format_stats(stats) except JSONDecodeError: if country is not None: message = f"{country} doesn't exist lmao" else: message = "Error: Could not look up stats" context.bot.send_message(chat_id=chat_id, text=message, parse_mode=ParseMode.MARKDOWN)
def send_question(bot, update): # a function that sends the link for asking the question from admins bot.send_chat_action(chat_id=update.message.chat_id , action = telegram.ChatAction.TYPING) # bot will show its typing process to the user time.sleep(1) # the period of time of the typing (1 second) bot.send_message(chat_id=update.message.chat_id, text=flag.flagize(':KZ:') + ' Осы ботты пайдаланып, сұрақ жазыңыз'+ '\n\n' + flag.flagize(':RU:') + ' Задайте свой вопрос этому боту' + '\n\n' + 'https://t.me/kaznadc_support_bot')
def nationality_to_flag(nat): flag_t = NATIONALITY_TO_FLAG.loc[nat, "flag"] return flag.flagize(f":{flag_t}:")
def make_graph(clients, clients_connections, countries, graph_file): g = Digraph('G', filename=graph_file) # NOTE: the subgraph name needs to begin with 'cluster' (all lowercase) # so that Graphviz recognizes it as a special cluster subgraph memo_con = sqlite3.connect('memories.db') cursorObj2 = memo_con.cursor() count = 0 known_queries = {} for country, val in countries.items(): name = 'cluster_' + str(count) with g.subgraph(name=str(name)) as c: c.attr(color='blue') for item in val: entities = [item[0]] cursorObj2.execute( '''SELECT COUNT(dns.client_id) FROM dns INNER JOIN ip_address AS ip ON dns.ip_id = ip.id WHERE ip.ip = ?;''', entities) times_resolved = cursorObj2.fetchall()[0][0] cursorObj2.execute( '''SELECT web.web_address FROM dns INNER JOIN ip_address AS ip ON dns.ip_id = ip.id INNER JOIN web_address AS web ON dns.web_id = web.id WHERE ip.ip = ?;''', entities) queries = cursorObj2.fetchall() if queries: known_queries[item[0]] = queries if times_resolved == 1: # for gramatical purposes node_info = "PTR: " + str(item[1]) + "\n" + "IP: " + str( item[0]) + "\n" + "Timezone: " + str( item[2]) + "\n" + "This IP was resolved " + str( times_resolved) + " time in other captures." else: node_info = "PTR: " + str(item[1]) + "\n" + "IP: " + str( item[0]) + "\n" + "Timezone: " + str( item[2]) + "\n" + "This IP was resolved " + str( times_resolved) + " times in other captures." if times_resolved > 0: c.attr( 'node', color='yellow' ) # if the IP was resolved by other device the not edges will be coloured green else: c.attr('node', color='red') #otherwise red c.node(str(item[0]), node_info) try: c.attr(label=flag.flagize(country + " :" + str(country) + ":")) except: c.attr(label=str(country)) count = count + 1 for ip, queries in known_queries.items(): list_of_queries = "" check_queries = [] for item in queries: if item[0] not in check_queries: list_of_queries = list_of_queries + item[0] + "\n" check_queries.append(item[0]) c.attr('node', rank="sink") g.node( str(ip) + "_list", "This IP is resolved in: " + "\n" + list_of_queries) g.edge(str(ip), str(ip) + "_list") #client_dict = {} for client in clients: #client_dict[client[0]]=client[1] g.node(str(client[0]), str(client[2] + "\n" + client[1])) #print client_dict for client, connections in clients_connections.items(): for connection in connections: g.edge(str(client), str(connection[0]), label="Out: " + str(connection[1]) + "\n" + "In: " + str(connection[2])) g.view()
def save_post(self, post, thread, first_post): """ Add post to database :param dict post: Post data to add :param dict thread: Data for thread the post belongs to :param dict first_post: First post in thread :return bool: Whether the post was inserted """ # check for data integrity missing = set(self.required_fields) - set(post.keys()) if missing != set(): self.log.warning("Missing fields %s in scraped post, ignoring" % repr(missing)) return False # save dimensions as a dumpable dict - no need to make it indexable if len({"w", "h", "tn_h", "tn_w"} - set(post.keys())) == 0: dimensions = { "w": post["w"], "h": post["h"], "tw": post["tn_w"], "th": post["tn_h"] } else: dimensions = {} # aggregate post data post_data = { "id": post["no"], "thread_id": thread["id"], "timestamp": post["time"], "subject": post.get("sub", ""), "body": post.get("com", ""), "author": post.get("name", ""), "author_trip": post.get("trip", ""), "author_type": post["id"] if "id" in post and self.type == "4chan-thread" else "", "author_type_id": post.get("capcode", ""), "country_code": post.get("country", ""), "country_name": post.get("country_name", ""), "image_file": post["filename"] + post["ext"] if "filename" in post else "", "image_4chan": str(post["tim"]) + post["ext"] if "filename" in post else "", "image_md5": post.get("md5", ""), "image_filesize": post.get("fsize", 0), "image_dimensions": json.dumps(dimensions), "semantic_url": post.get("semantic_url", ""), "unsorted_data": json.dumps({ field: post[field] for field in post.keys() if field not in self.known_fields }) } # this is mostly unsupported, feel free to ignore if hasattr(config, "HIGHLIGHT_SLACKHOOK") and hasattr( config, "HIGHLIGHT_MATCH") and self.type == "4chan-thread": for highlight in config.HIGHLIGHT_MATCH: attachments = [] if highlight.lower() in post_data["body"].lower(): if not post_data["country_code"]: country_flag = " (%s)" % post_data[ "country_name"] if post_data["country_name"] else "" else: pattern = " :%s:" % post_data["country_code"] country_flag = flag.flagize(pattern) if country_flag == pattern: country_flag = " (%s)" % post_data["country_code"] else: print(repr(country_flag)) subject = first_post.get("sub", first_post["no"]) attachments.append({ "title": "%s%s in '%s''" % (post_data["author"], country_flag, subject), "title_link": "https://boards.4chan.org/%s/thread/%s#pc%s" % (thread["board"], thread["id"], post_data["id"]), "text": strip_tags(post_data["body"], convert_newlines=True).replace( highlight, "*%s*" % highlight), "mrkdwn_in": ["text", "pretext"], "color": "#73ad34" }) if not attachments: continue try: requests.post( config.HIGHLIGHT_SLACKHOOK, json.dumps({ "text": "A post mentioning '%s' was just scraped from 4chan /%s/:" % (highlight, thread["board"]), "attachments": attachments })) except requests.RequestException as e: self.log.warning( "Could not send highlight alerts to Slack webhook (%s)" % e) # now insert the post into the database try: for field in post_data: if not isinstance(post_data[field], six.string_types): continue # apparently, sometimes \0 appears in posts or something; psycopg2 can't cope with this post_data[field] = post_data[field].replace("\0", "") self.db.insert("posts_" + self.prefix, post_data) except psycopg2.IntegrityError: self.db.rollback() dupe = self.db.fetchone("SELECT * from posts_" + self.prefix + " WHERE id = %s" % (str(post["no"]), ), commit=False) if dupe: pass #self.log.error("Post %s in thread %s/%s/%s (time: %s) scraped twice: first seen as %s in thread %s at %s" % ( # post["no"], self.datasource, thread["board"], thread["id"], post["time"], dupe["id"], dupe["thread_id"], dupe["timestamp"])) else: self.log.error( "Post %s in thread %s/%s/%s hit database constraint but no dupe was found?" % (post["no"], self.datasource, thread["board"], thread["id"])) return False except ValueError as e: self.db.rollback() self.log.error("ValueError (%s) during scrape of thread %s" % (e, post["no"])) # Download images (exclude .webm files) if "filename" in post and post["ext"] != ".webm": self.queue_image(post, thread) return True
def get_teams_squad(user_id): user = users_controller.get_user(user_id) url = get_team_foot_url(user) countries_dict = bot_methods.get_countries_dict() lang = user.language message_text = '' squad_positions = LANG_DICT[lang]['positions'] page = requests.get(url) html = page.text soup = BeautifulSoup(html, 'lxml') squad_block = soup.find('article', class_='team-consist') if not squad_block: return LANG_DICT[lang]['no_squad_msg'] for i, position_table in enumerate( squad_block.find_all('table', class_='consist-table')): message_text += squad_positions[i] footballers = position_table.find_all('tr') country_emoji = '' footballer_names_str = '\n'.join([ footballer.find('td', class_='num').get_text() + '. ' + footballer.find('a').get_text() for footballer in footballers ]) if lang == 'ua': translated_names = translator.translate(footballer_names_str, src='ru', dest='uk').text.split('\n') else: translated_names = footballer_names_str.split('\n') for trans_footballer in translated_names: num = trans_footballer.split('.')[0] name = trans_footballer.split('.')[1].strip() footballer = [ footballer for footballer in footballers if footballer.find('td', class_='num').get_text() == num ][0] try: birth_date = footballer.find( 'td', class_='birth').find('p').get_text() except: birth_date = LANG_DICT[lang]['not_mentioned_str'] country_name = footballer.find('img')['alt'] for key in countries_dict: if countries_dict[key] == country_name: country_code = key country_emoji = flag.flagize(':{}:'.format(country_code)) message_text += '{}. {} {} (_{}_)\n'.format( num, country_emoji, name, birth_date) message_text += '\n' return message_text
def home(): city_form = CityForm(request.form) if request.method == "POST": corrected_user_query_location = city_form.location.data.replace( " ", "%20") nominatim_json_reponse = get( f"https://nominatim.openstreetmap.org/search/" f"{corrected_user_query_location}" f"?format=json").json() location_lat = nominatim_json_reponse[0]["lat"] location_lon = nominatim_json_reponse[0]["lon"] opwm_json_response = get(f"https://api.openweathermap.org/data/2.5/weather" f"?lat={location_lat}" f"&lon={location_lon}" f"&appid={OPWM_API_KEY}" f"&units=metric")\ .json() opwm_uv_index_json_response = get(f"https://api.openweathermap.org/data/2.5/uvi" f"?&appid={OPWM_API_KEY}" f"&lat={location_lat}" f"&lon={location_lon}")\ .json() opwm_forecast_json_response = get(f"https://api.openweathermap.org/data/2.5/forecast" f"?lat={location_lat}" f"&lon={location_lon}" f"&appid={OPWM_API_KEY}" f"&units=metric")\ .json() timezonedb_json_response = get(f"http://api.timezonedb.com/v2.1/get-time-zone" f"?format=json" f"&by=position" f"&lat={location_lat}" f"&lng={location_lon}" f"&key={TIMEZONEDB_API_KEY}")\ .json() aq_json_response = get(f"http://api.airvisual.com/v2/nearest_city" f"?lat={location_lat}" f"&lon={location_lon}" f"&key={AIRQUALITY_API_KEY}")\ .json() try: cache.set("weather_wind_direction_deg", round(opwm_json_response["wind"]["deg"])) cache.set("weather_wind_direction_abbr", portolan.point(degree=cache.get("weather_wind_direction_deg"))\ .capitalize()) except KeyError: cache.set("weather_wind_direction_deg", None) cache.set("weather_wind_direction_abbr", "No data") cache.set("user_query_location", city_form.location.data.split(",")[0]\ .title()) cache.set("country_code", opwm_json_response["sys"]["country"]) cache.set("country_full_name", countries.get(opwm_json_response["sys"]["country"]).name) cache.set("country_emoji_flag", flag.flagize(f":{opwm_json_response['sys']['country']}:")) cache.set("location_station_name", opwm_json_response["name"]) cache.set( "location_more_link", f"https://www.google.com/search" f"?q={cache.get('user_query_location')}") cache.set( "location_local_time", datetime.strptime(timezonedb_json_response["formatted"], "%Y-%m-%d %H:%M:%S")) cache.set("weather_time_calc_utc", datetime.fromtimestamp(opwm_json_response["dt"])\ .strftime("%d/%m/%Y, at %H:%M")) cache.set("weather_temp_current", round(opwm_json_response["main"]["temp"], 1)) cache.set("weather_temp_min", round(opwm_json_response["main"]["temp_min"], 1)) cache.set("weather_temp_max", round(opwm_json_response["main"]["temp_max"], 1)) cache.set("weather_description", opwm_json_response["weather"][0]["description"]\ .capitalize()) cache.set("weather_pressure", opwm_json_response["main"]["pressure"]) cache.set("weather_humidity", opwm_json_response["main"]["humidity"]) cache.set("weather_wind_speed", opwm_json_response["wind"]["speed"]) cache.set("weather_uv_index", round(opwm_uv_index_json_response["value"])) cache.set("weather_air_quality_index", aq_json_response["data"]["current"]["pollution"]["aqius"]) cache.set("weather_temp_forecast", [ temp["main"]["temp"] for temp in opwm_forecast_json_response["list"][::5] ]) cache.set("weather_forecast_time_calc_utc", [ datetime.strptime(time_calc["dt_txt"], "%Y-%m-%d %H:%M:%S") for time_calc in opwm_forecast_json_response["list"][::5] ]) city_form.location.data = "" return render_template("weather_report.html", cache=cache, city_form=city_form) return render_template("home.html", cache=cache, city_form=city_form)
import flag flag.flagize("Flag of Israel :IL:")