Ejemplo n.º 1
0
def main():
    try:
        pleroma = login()
    except MastodonError:
        logger.error("login failed", exc_info=True)
        return
    except KeyError:
        logger.error("login failed", exc_info=True)
        return

    logger.debug("Logged in")
    try:
        response = get(JSON_URL)
        json = response.json()
    except Exception as e:
        logger.error("Failed to connect to gambe.ro. Skipping.")
        return

    logger.debug("Downloaded stories")
    storage = PleromaStorage()
    latest = storage.load()
    if latest:
        new_stories = get_new_stories(latest, json)
    else:
        new_stories = [Story.from_json_dict(json[0])]

    if len(new_stories) == 0:
        logger.info("No new stories found since last check")
    else:
        for story in new_stories:
            text = PleromaStoryFormatter().format_string(story)
            pleroma.status_post(status=text, content_type="text/html")

        storage.save(new_stories[-1])
Ejemplo n.º 2
0
def main():
    # Creates bot
    bot = API(auth)
    logger.info("Checking for new posts...")
    # Fetches website to get new stories in JSON
    try:
        response = get(JSON_URL)
        json = response.json()
    except Exception as e:
        logger.error("Failed to connect to gambe.ro. Skipping.")
        return
    # Fetches Twitter for the last published stories
    last_posted_tweet = None
    new_stories = []
    try:
        last_posted_tweet = get_last_posted_tweet(bot)
        new_stories = get_new_stories(last_posted_tweet, json)
    # If is not possible to retrieve last tweet gets only the latest story on the website
    except ValueError:
        new_stories = [Story.from_json_dict(json[0])]
    # Tweets all the new stories
    if (len(new_stories) == 0):
        logger.info("Nothing new here, the bot is back to sleep.")
    else:
        for story in new_stories:
            tweet: str = None
            try:
                tweet = TwitterStoryFormatter().format_string(story)
                bot.update_status(tweet)
                logger.info(f"Tweeted: {tweet}")
            except ValueError:
                logger.critical("Unable to post tweet")
            except TweepError:
                print_exc()
Ejemplo n.º 3
0
def publish_news(context: CallbackContext):
    try:
        response = get(JSON_URL)
        json = response.json()
    except Exception as e:
        logger.error("Failed to connect to gambe.ro. Skipping.")
        return

    storage = TelegramStorage()
    latest = storage.load()
    if latest:
        new_stories = get_new_stories(latest, json)
    else:
        new_stories = [Story.from_json_dict(json[0])]

    if len(new_stories) == 0:
        logger.info("No new stories found since last check")
    else:
        for story in new_stories:
            text = TelegramStoryFormatter().format_string(story)
            try:
                context.bot.send_message(chat_id=CHAT_ID,
                                         text=text,
                                         parse_mode="html")
            except Exception:
                logging.getLogger(__name__).error(
                    f"Failed connection for story: {story}\n"
                    f"Caused by:\n{traceback.format_exc()}")
        storage.save(new_stories[-1])