From ac8682faeec27cbafc38833f1dd53bd26d5cb44f Mon Sep 17 00:00:00 2001 From: spla Date: Thu, 13 Aug 2020 14:06:49 +0200 Subject: [PATCH 01/10] Updated README.md --- README.md | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f521531..5d5c3fd 100644 --- a/README.md +++ b/README.md @@ -7,21 +7,20 @@ Publish to Mastodon's server the rss feed of your choice. - **Python 3** - Postgresql server -- Everything else at the top of `mastofeeds.py`! +- Mastodon's bot account ### Usage: Within Python Virtual Environment: -1. Run 'python db-setup.py' to create needed database and table. It's needed to control what feeds are already published. db-setup.py +1. Run `pip install -r requirements.txt` to install all needed libraries. + +2. Run `python db-setup.py` to create needed database and table. It's needed to control what feeds are already published. db-setup.py will also ask you the feed's url. -2. Run 'python setup.py' to get your bot's access token of an existing user. It will be saved to 'secrets/secrets.txt' for further use. +3. Run `python setup.py` to get your bot's access token of an existing user. It will be saved to 'secrets/secrets.txt' for further use. -3. Run 'python mastofeeds.py' to start publishing feeds. - -4. Use your favourite scheduling method to set mastofeeds.py to run regularly. - -Note: install all needed packages with 'pip install package' or use 'pip install -r requirements.txt' to install them. +4. Run `python mastofeeds.py` to start publishing feeds. +5. Use your favourite scheduling method to set `python mastofeeds.py` to run regularly. From 70b8575fe579ebfe58135d6776eebd818cce9bcb Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 20 Oct 2020 12:41:24 +0200 Subject: [PATCH 02/10] Added id column to feeds database --- db-setup.py | 2 +- mastofeeds.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/db-setup.py b/db-setup.py index 765496f..d0e7cb1 100644 --- a/db-setup.py +++ b/db-setup.py @@ -139,7 +139,7 @@ print("Creating table...") db = feeds_db db_user = feeds_db_user table = "feeds" -sql = "create table "+table+" (link varchar(200) PRIMARY KEY)" +sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))" create_table(db, db_user, table, sql) ##################################### diff --git a/mastofeeds.py b/mastofeeds.py index 4b6e8c1..15fc0ee 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -75,7 +75,7 @@ for entry in newsfeeds.entries: cur = conn.cursor() - cur.execute('select link from feeds where link=(%s)', (link,)) + cur.execute('select id from feeds where id=(%s)', (id,)) row = cur.fetchone() if row == None: @@ -109,7 +109,7 @@ for entry in newsfeeds.entries: ######################################################### - insert_line = 'INSERT INTO feeds(link) VALUES (%s)' + insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' conn = None @@ -119,7 +119,7 @@ for entry in newsfeeds.entries: cur = conn.cursor() - cur.execute(insert_line, (link,)) + cur.execute(insert_line, (id, link,)) conn.commit() From c65a4c1fc8913ed7846ef4038a76ec202b6c88b3 Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 20 Oct 2020 12:54:34 +0200 Subject: [PATCH 03/10] Fixed indents --- db-setup.py | 109 ++++++++++++++++++++++----------------------- mastofeeds.py | 119 ++++++++++++++++++++++++++------------------------ 2 files changed, 116 insertions(+), 112 deletions(-) diff --git a/db-setup.py b/db-setup.py index d0e7cb1..cf0e542 100644 --- a/db-setup.py +++ b/db-setup.py @@ -66,84 +66,85 @@ def create_table(db, db_user, table, sql): conn.close() -############################################################################################# +############################################################################### +# main -# Load configuration from config file -config_filepath = "db_config.txt" -feeds_db = get_parameter("feeds_db", config_filepath) -feeds_db_user = get_parameter("feeds_db_user", config_filepath) -feeds_url = get_parameter("feeds_url", config_filepath) +if __name__ == '__main__': -############################################################ -# create database -############################################################ + # Load configuration from config file + config_filepath = "db_config.txt" + feeds_db = get_parameter("feeds_db", config_filepath) + feeds_db_user = get_parameter("feeds_db_user", config_filepath) + feeds_url = get_parameter("feeds_url", config_filepath) -conn = None + ############################################################ + # create database + ############################################################ -try: + conn = None - conn = psycopg2.connect(dbname='postgres', - user=feeds_db_user, host='', - password='') + try: - conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) + conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='') - cur = conn.cursor() + conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - print("Creating database " + feeds_db + ". Please wait...") + cur = conn.cursor() - cur.execute(sql.SQL("CREATE DATABASE {}").format( - sql.Identifier(feeds_db)) - ) - print("Database " + feeds_db + " created!") + print("Creating database " + feeds_db + ". Please wait...") -except (Exception, psycopg2.DatabaseError) as error: + cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db))) - print(error) + print("Database " + feeds_db + " created!") -finally: + except (Exception, psycopg2.DatabaseError) as error: - if conn is not None: + print(error) - conn.close() + finally: -############################################################################################# + if conn is not None: -try: + conn.close() - conn = None - conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") + ############################################################################################# -except (Exception, psycopg2.DatabaseError) as error: + try: - print(error) - # Load configuration from config file - os.remove("db_config.txt") - print("Exiting. Run setup again with right parameters") - sys.exit(0) + conn = None + conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") -if conn is not None: + except (Exception, psycopg2.DatabaseError) as error: - print("\n") - print("Host parameters saved to config.txt!") - print("\n") + print(error) -############################################################ -# Create needed tables -############################################################ + # Load configuration from config file + os.remove("db_config.txt") + print("Exiting. Run setup again with right parameters") + sys.exit(0) -print("Creating table...") + if conn is not None: -######################################## + print("\n") + print("Host parameters saved to config.txt!") + print("\n") -db = feeds_db -db_user = feeds_db_user -table = "feeds" -sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))" -create_table(db, db_user, table, sql) + ############################################################ + # Create needed tables + ############################################################ -##################################### + print("Creating table...") -print("Done!") -print("Now you can run setup.py!") -print("\n") + ######################################## + + db = feeds_db + db_user = feeds_db_user + table = "feeds" + sql = "create table "+table+" (id varchar(200) PRIMARY KEY, link varchar(250))" + create_table(db, db_user, table, sql) + + ##################################### + + print("Done!") + print("Now you can run setup.py!") + print("\n") diff --git a/mastofeeds.py b/mastofeeds.py index 15fc0ee..8ef907b 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -52,76 +52,39 @@ mastodon = Mastodon( # Initialise access headers headers={ 'Authorization': 'Bearer %s'%uc_access_token } -######################################################## +############################################################################### +# main -publish = 0 +if __name__ == '__main__': -newsfeeds = feedparser.parse(feeds_url) + publish = 0 -for entry in newsfeeds.entries: + newsfeeds = feedparser.parse(feeds_url) - title = entry['title'] - id = entry['id'] - link = entry['link'] + for entry in newsfeeds.entries: - ################################################################### - # check database if feed is already published - ################################################################### + title = entry['title'] + id = entry['id'] + link = entry['link'] - try: - - conn = None - conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") - - cur = conn.cursor() - - cur.execute('select id from feeds where id=(%s)', (id,)) - - row = cur.fetchone() - if row == None: - publish = 1 - else: - publish = 0 - - cur.close() - - except (Exception, psycopg2.DatabaseError) as error: - - print(error) - - finally: - - if conn is not None: - - conn.close() - - ########################################################### - - if publish == 1: - - toot_text = str(title)+'\n' - toot_text += str(link) - - print("Tooting...") - print(toot_text) - - mastodon.status_post(toot_text, in_reply_to_id=None,) - - ######################################################### - - insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' - - conn = None + ################################################################### + # check database if feed is already published + ################################################################### try: + conn = None conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") cur = conn.cursor() - cur.execute(insert_line, (id, link,)) + cur.execute('select id from feeds where id=(%s)', (id,)) - conn.commit() + row = cur.fetchone() + if row == None: + publish = 1 + else: + publish = 0 cur.close() @@ -134,6 +97,46 @@ for entry in newsfeeds.entries: if conn is not None: conn.close() - else: - print("Any new feeds") + ########################################################### + + if publish == 1: + + toot_text = str(title)+'\n' + toot_text += str(link) + + print("Tooting...") + print(toot_text) + + mastodon.status_post(toot_text, in_reply_to_id=None,) + + ######################################################### + + insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' + + conn = None + + try: + + conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432") + + cur = conn.cursor() + + cur.execute(insert_line, (id, link,)) + + conn.commit() + + cur.close() + + except (Exception, psycopg2.DatabaseError) as error: + + print(error) + + finally: + + if conn is not None: + + conn.close() + else: + + print("Any new feeds") From 3e89fb8a509c76cf2cb9e1d4b426b2d388d47fda Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 20 Oct 2020 12:56:21 +0200 Subject: [PATCH 04/10] Fixed indents --- setup.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/setup.py b/setup.py index f32d45b..62a9581 100644 --- a/setup.py +++ b/setup.py @@ -175,12 +175,17 @@ def get_hostname( parameter, config_filepath ): print("hostname setup done!") sys.exit(0) -# Load secrets from secrets file -secrets_filepath = "secrets/secrets.txt" -uc_client_id = get_parameter("uc_client_id", secrets_filepath) -uc_client_secret = get_parameter("uc_client_secret", secrets_filepath) -uc_access_token = get_parameter("uc_access_token", secrets_filepath) +############################################################################### +# main -# Load configuration from config file -config_filepath = "config.txt" -mastodon_hostname = get_hostname("mastodon_hostname", config_filepath) # E.g., mastodon.social +if __name__ == '__main__': + + # Load secrets from secrets file + secrets_filepath = "secrets/secrets.txt" + uc_client_id = get_parameter("uc_client_id", secrets_filepath) + uc_client_secret = get_parameter("uc_client_secret", secrets_filepath) + uc_access_token = get_parameter("uc_access_token", secrets_filepath) + + # Load configuration from config file + config_filepath = "config.txt" + mastodon_hostname = get_hostname("mastodon_hostname", config_filepath) From 9726ed0a644d8c0248f320f897a48cd42dddb27b Mon Sep 17 00:00:00 2001 From: spla Date: Tue, 20 Oct 2020 13:21:08 +0200 Subject: [PATCH 05/10] Now it exit when find first already published feed --- mastofeeds.py | 59 +++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/mastofeeds.py b/mastofeeds.py index 8ef907b..68f7885 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -1,3 +1,4 @@ +import pdb import os import feedparser from mastodon import Mastodon @@ -25,38 +26,40 @@ def get_parameter( parameter, file_path ): print(file_path + " Missing parameter %s "%parameter) sys.exit(0) -# Load secrets from secrets file -secrets_filepath = "secrets/secrets.txt" -uc_client_id = get_parameter("uc_client_id", secrets_filepath) -uc_client_secret = get_parameter("uc_client_secret", secrets_filepath) -uc_access_token = get_parameter("uc_access_token", secrets_filepath) - -# Load configuration from config file -db_config_filepath = "db_config.txt" -feeds_db = get_parameter("feeds_db", db_config_filepath) -feeds_db_user = get_parameter("feeds_db_user", db_config_filepath) -feeds_url = get_parameter("feeds_url", db_config_filepath) - -# Load configuration from config file -config_filepath = "config.txt" -mastodon_hostname = get_parameter("mastodon_hostname", config_filepath) # E.g., mastodon.social - -# Initialise Mastodon API -mastodon = Mastodon( - client_id = uc_client_id, - client_secret = uc_client_secret, - access_token = uc_access_token, - api_base_url = 'https://' + mastodon_hostname, -) - -# Initialise access headers -headers={ 'Authorization': 'Bearer %s'%uc_access_token } - ############################################################################### # main if __name__ == '__main__': + # Load secrets from secrets file + secrets_filepath = "secrets/secrets.txt" + uc_client_id = get_parameter("uc_client_id", secrets_filepath) + uc_client_secret = get_parameter("uc_client_secret", secrets_filepath) + uc_access_token = get_parameter("uc_access_token", secrets_filepath) + + # Load configuration from config file + db_config_filepath = "db_config.txt" + feeds_db = get_parameter("feeds_db", db_config_filepath) + feeds_db_user = get_parameter("feeds_db_user", db_config_filepath) + feeds_url = get_parameter("feeds_url", db_config_filepath) + + # Load configuration from config file + config_filepath = "config.txt" + mastodon_hostname = get_parameter("mastodon_hostname", config_filepath) # E.g., mastodon.social + + # Initialise Mastodon API + mastodon = Mastodon( + client_id = uc_client_id, + client_secret = uc_client_secret, + access_token = uc_access_token, + api_base_url = 'https://' + mastodon_hostname, + ) + + # Initialise access headers + headers={ 'Authorization': 'Bearer %s'%uc_access_token } + + ####################################################################### + publish = 0 newsfeeds = feedparser.parse(feeds_url) @@ -69,7 +72,6 @@ if __name__ == '__main__': ################################################################### # check database if feed is already published - ################################################################### try: @@ -140,3 +142,4 @@ if __name__ == '__main__': else: print("Any new feeds") + sys.exit(0) From 4f810126452846dc3df9201ed1b7b2f13bf51a7d Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 24 Oct 2020 10:48:23 +0200 Subject: [PATCH 06/10] Fixed indents --- db-setup.py | 48 ++++++++++++++++++++++++------------------------ 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/db-setup.py b/db-setup.py index cf0e542..ab5606f 100644 --- a/db-setup.py +++ b/db-setup.py @@ -29,42 +29,42 @@ def get_parameter( parameter, file_path ): sys.exit(0) def write_parameter( parameter, file_path ): - print("Setting up newsfeed parameters...") - print("\n") - feeds_db = input("feeds db name: ") - feeds_db_user = input("feeds db user: ") - feeds_url = input("enter feeds url: ") + print("Setting up newsfeed parameters...") + print("\n") + feeds_db = input("feeds db name: ") + feeds_db_user = input("feeds db user: ") + feeds_url = input("enter feeds url: ") - with open(file_path, "w") as text_file: - print("feeds_db: {}".format(feeds_db), file=text_file) - print("feeds_db_user: {}".format(feeds_db_user), file=text_file) - print("feeds_url: {}".format(feeds_url), file=text_file) + with open(file_path, "w") as text_file: + print("feeds_db: {}".format(feeds_db), file=text_file) + print("feeds_db_user: {}".format(feeds_db_user), file=text_file) + print("feeds_url: {}".format(feeds_url), file=text_file) def create_table(db, db_user, table, sql): - conn = None - try: + conn = None + try: - conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432") - cur = conn.cursor() + conn = psycopg2.connect(database = db, user = db_user, password = "", host = "/var/run/postgresql", port = "5432") + cur = conn.cursor() - print("Creating table.. "+table) - # Create the table in PostgreSQL database - cur.execute(sql) + print("Creating table.. "+table) + # Create the table in PostgreSQL database + cur.execute(sql) - conn.commit() - print("Table "+table+" created!") - print("\n") + conn.commit() + print("Table "+table+" created!") + print("\n") - except (Exception, psycopg2.DatabaseError) as error: + except (Exception, psycopg2.DatabaseError) as error: - print(error) + print(error) - finally: + finally: - if conn is not None: + if conn is not None: - conn.close() + conn.close() ############################################################################### # main From 8659e6c68bb91a9c181d5708cd7be9068be791d8 Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 24 Oct 2020 10:49:22 +0200 Subject: [PATCH 07/10] Removed unneeded library --- mastofeeds.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mastofeeds.py b/mastofeeds.py index 68f7885..758bbe4 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -1,4 +1,3 @@ -import pdb import os import feedparser from mastodon import Mastodon From 9ab7d02526eb15a6331df382b942842315908a66 Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 24 Oct 2020 11:03:00 +0200 Subject: [PATCH 08/10] Fixed indents --- setup.py | 207 +++++++++++++++++++++++++++---------------------------- 1 file changed, 103 insertions(+), 104 deletions(-) diff --git a/setup.py b/setup.py index 62a9581..6d33c3e 100644 --- a/setup.py +++ b/setup.py @@ -9,126 +9,125 @@ import os import sys def create_dir(): - if not os.path.exists('secrets'): - os.makedirs('secrets') + if not os.path.exists('secrets'): + os.makedirs('secrets') def create_file(): - if not os.path.exists('secrets/secrets.txt'): - with open('secrets/secrets.txt', 'w'): pass - print(secrets_filepath + " created!") + if not os.path.exists('secrets/secrets.txt'): + with open('secrets/secrets.txt', 'w'): pass + print(secrets_filepath + " created!") def create_config(): - if not os.path.exists(config_filepath): - print(config_filepath + " created!") - with open('config.txt', 'w'): pass + if not os.path.exists(config_filepath): + print(config_filepath + " created!") + with open('config.txt', 'w'): pass def write_params(): - with open(secrets_filepath, 'a') as the_file: - print("Writing secrets parameter names to " + secrets_filepath) - the_file.write('uc_client_id: \n'+'uc_client_secret: \n'+'uc_access_token: \n') + with open(secrets_filepath, 'a') as the_file: + print("Writing secrets parameter names to " + secrets_filepath) + the_file.write('uc_client_id: \n'+'uc_client_secret: \n'+'uc_access_token: \n') def write_config(): - with open(config_filepath, 'a') as the_file: - the_file.write('mastodon_hostname: \n') - print("adding parameter name 'mastodon_hostname' to "+ config_filepath) + with open(config_filepath, 'a') as the_file: + the_file.write('mastodon_hostname: \n') + print("adding parameter name 'mastodon_hostname' to "+ config_filepath) def read_client_lines(self): - client_path = 'app_clientcred.txt' - with open(client_path) as fp: - line = fp.readline() - cnt = 1 - while line: - if cnt == 1: - print("Writing client id to " + secrets_filepath) - modify_file(secrets_filepath, "uc_client_id: ", value=line.rstrip()) - elif cnt == 2: - print("Writing client secret to " + secrets_filepath) - modify_file(secrets_filepath, "uc_client_secret: ", value=line.rstrip()) - line = fp.readline() - cnt += 1 + client_path = 'app_clientcred.txt' + with open(client_path) as fp: + line = fp.readline() + cnt = 1 + while line: + if cnt == 1: + print("Writing client id to " + secrets_filepath) + modify_file(secrets_filepath, "uc_client_id: ", value=line.rstrip()) + elif cnt == 2: + print("Writing client secret to " + secrets_filepath) + modify_file(secrets_filepath, "uc_client_secret: ", value=line.rstrip()) + line = fp.readline() + cnt += 1 def read_token_line(self): - token_path = 'app_usercred.txt' - with open(token_path) as fp: - line = fp.readline() - print("Writing access token to " + secrets_filepath) - modify_file(secrets_filepath, "uc_access_token: ", value=line.rstrip()) + token_path = 'app_usercred.txt' + with open(token_path) as fp: + line = fp.readline() + print("Writing access token to " + secrets_filepath) + modify_file(secrets_filepath, "uc_access_token: ", value=line.rstrip()) def read_config_line(): - with open(config_filepath) as fp: - line = fp.readline() - modify_file(config_filepath, "mastodon_hostname: ", value=hostname) + with open(config_filepath) as fp: + line = fp.readline() + modify_file(config_filepath, "mastodon_hostname: ", value=hostname) def log_in(): - error = 0 - try: - global hostname - hostname = input("Enter Mastodon hostname: ") - user_name = input("User name, ex. user@" + hostname +"? ") - user_password = getpass.getpass("User password? ") - app_name = input("This app name? ") - Mastodon.create_app(app_name, scopes=["read","write"], - to_file="app_clientcred.txt", api_base_url=hostname) - mastodon = Mastodon(client_id = "app_clientcred.txt", api_base_url = hostname) - mastodon.log_in( - user_name, - user_password, - scopes = ["read", "write"], - to_file = "app_usercred.txt" - ) - except MastodonIllegalArgumentError as i_error: - error = 1 - if os.path.exists("secrets/secrets.txt"): - print("Removing secrets/secrets.txt file..") - os.remove("secrets/secrets.txt") - if os.path.exists("app_clientcred.txt"): - print("Removing app_clientcred.txt file..") - os.remove("app_clientcred.txt") - sys.exit(i_error) - except MastodonNetworkError as n_error: - error = 1 - if os.path.exists("secrets/secrets.txt"): - print("Removing secrets/secrets.txt file..") - os.remove("secrets/secrets.txt") - if os.path.exists("app_clientcred.txt"): - print("Removing app_clientcred.txt file..") - os.remove("app_clientcred.txt") - sys.exit(n_error) - except MastodonReadTimeout as r_error: - error = 1 - if os.path.exists("secrets/secrets.txt"): - print("Removing secrets/secrets.txt file..") - os.remove("secrets/secrets.txt") - if os.path.exists("app_clientcred.txt"): - print("Removing app_clientcred.txt file..") - os.remove("app_clientcred.txt") - sys.exit(r_error) - except MastodonAPIError as a_error: - error = 1 - if os.path.exists("secrets/secrets.txt"): - print("Removing secrets/secrets.txt file..") - os.remove("secrets/secrets.txt") - if os.path.exists("app_clientcred.txt"): - print("Removing app_clientcred.txt file..") - os.remove("app_clientcred.txt") - sys.exit(a_error) - finally: - if error == 0: - - create_dir() - create_file() - write_params() - client_path = 'app_clientcred.txt' - read_client_lines(client_path) - token_path = 'app_usercred.txt' - read_token_line(token_path) - if os.path.exists("app_clientcred.txt"): - print("Removing app_clientcred.txt temp file..") - os.remove("app_clientcred.txt") - if os.path.exists("app_usercred.txt"): - print("Removing app_usercred.txt temp file..") - os.remove("app_usercred.txt") - print("Secrets setup done!\n") + error = 0 + try: + global hostname + hostname = input("Enter Mastodon hostname: ") + user_name = input("User name, ex. user@" + hostname +"? ") + user_password = getpass.getpass("User password? ") + app_name = input("This app name? ") + Mastodon.create_app(app_name, scopes=["read","write"], + to_file="app_clientcred.txt", api_base_url=hostname) + mastodon = Mastodon(client_id = "app_clientcred.txt", api_base_url = hostname) + mastodon.log_in( + user_name, + user_password, + scopes = ["read", "write"], + to_file = "app_usercred.txt" + ) + except MastodonIllegalArgumentError as i_error: + error = 1 + if os.path.exists("secrets/secrets.txt"): + print("Removing secrets/secrets.txt file..") + os.remove("secrets/secrets.txt") + if os.path.exists("app_clientcred.txt"): + print("Removing app_clientcred.txt file..") + os.remove("app_clientcred.txt") + sys.exit(i_error) + except MastodonNetworkError as n_error: + error = 1 + if os.path.exists("secrets/secrets.txt"): + print("Removing secrets/secrets.txt file..") + os.remove("secrets/secrets.txt") + if os.path.exists("app_clientcred.txt"): + print("Removing app_clientcred.txt file..") + os.remove("app_clientcred.txt") + sys.exit(n_error) + except MastodonReadTimeout as r_error: + error = 1 + if os.path.exists("secrets/secrets.txt"): + print("Removing secrets/secrets.txt file..") + os.remove("secrets/secrets.txt") + if os.path.exists("app_clientcred.txt"): + print("Removing app_clientcred.txt file..") + os.remove("app_clientcred.txt") + sys.exit(r_error) + except MastodonAPIError as a_error: + error = 1 + if os.path.exists("secrets/secrets.txt"): + print("Removing secrets/secrets.txt file..") + os.remove("secrets/secrets.txt") + if os.path.exists("app_clientcred.txt"): + print("Removing app_clientcred.txt file..") + os.remove("app_clientcred.txt") + sys.exit(a_error) + finally: + if error == 0: + create_dir() + create_file() + write_params() + client_path = 'app_clientcred.txt' + read_client_lines(client_path) + token_path = 'app_usercred.txt' + read_token_line(token_path) + if os.path.exists("app_clientcred.txt"): + print("Removing app_clientcred.txt temp file..") + os.remove("app_clientcred.txt") + if os.path.exists("app_usercred.txt"): + print("Removing app_usercred.txt temp file..") + os.remove("app_usercred.txt") + print("Secrets setup done!\n") def modify_file(file_name,pattern,value=""): fh=fileinput.input(file_name,inplace=True) From e8a5ec6493721e7ac64f1bede94bbb2193b336b5 Mon Sep 17 00:00:00 2001 From: spla Date: Fri, 9 Apr 2021 11:35:47 +0200 Subject: [PATCH 09/10] Fix bad indenting and add two seconds sleep --- mastofeeds.py | 3 +++ setup.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/mastofeeds.py b/mastofeeds.py index 758bbe4..2458230 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -3,6 +3,7 @@ import feedparser from mastodon import Mastodon import psycopg2 import sys +import time ############################################################################### # INITIALISATION @@ -111,6 +112,8 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=None,) + time.sleep(2) + ######################################################### insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' diff --git a/setup.py b/setup.py index 6d33c3e..8e6303d 100644 --- a/setup.py +++ b/setup.py @@ -124,7 +124,7 @@ def log_in(): if os.path.exists("app_clientcred.txt"): print("Removing app_clientcred.txt temp file..") os.remove("app_clientcred.txt") - if os.path.exists("app_usercred.txt"): + if os.path.exists("app_usercred.txt"): print("Removing app_usercred.txt temp file..") os.remove("app_usercred.txt") print("Secrets setup done!\n") From 975a752a1085788965afa0755e4f8368e9e5d604 Mon Sep 17 00:00:00 2001 From: spla Date: Sat, 15 May 2021 11:19:08 +0200 Subject: [PATCH 10/10] Handle feedparser exception --- mastofeeds.py | 17 ++++++++++++----- setup.py | 2 +- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/mastofeeds.py b/mastofeeds.py index 758bbe4..d4d0d2c 100644 --- a/mastofeeds.py +++ b/mastofeeds.py @@ -3,10 +3,7 @@ import feedparser from mastodon import Mastodon import psycopg2 import sys - -############################################################################### -# INITIALISATION -############################################################################### +import time # Returns the parameter from the specified file def get_parameter( parameter, file_path ): @@ -61,7 +58,15 @@ if __name__ == '__main__': publish = 0 - newsfeeds = feedparser.parse(feeds_url) + try: + + newsfeeds = feedparser.parse(feeds_url) + print(newsfeeds.status) + + except: + + print(newsfeeds.status) + sys.exit(0) for entry in newsfeeds.entries: @@ -111,6 +116,8 @@ if __name__ == '__main__': mastodon.status_post(toot_text, in_reply_to_id=None,) + time.sleep(2) + ######################################################### insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)' diff --git a/setup.py b/setup.py index 6d33c3e..8e6303d 100644 --- a/setup.py +++ b/setup.py @@ -124,7 +124,7 @@ def log_in(): if os.path.exists("app_clientcred.txt"): print("Removing app_clientcred.txt temp file..") os.remove("app_clientcred.txt") - if os.path.exists("app_usercred.txt"): + if os.path.exists("app_usercred.txt"): print("Removing app_usercred.txt temp file..") os.remove("app_usercred.txt") print("Secrets setup done!\n")