Increased link column to varchar(300)

This commit is contained in:
spla 2021-05-15 11:30:59 +02:00
commit c9219f1a85
S'han modificat 4 arxius amb 314 adicions i 298 eliminacions

Veure arxiu

@ -7,21 +7,20 @@ Publish to Mastodon's server the rss feed of your choice.
- **Python 3** - **Python 3**
- Postgresql server - Postgresql server
- Everything else at the top of `mastofeeds.py`! - Mastodon's bot account
### Usage: ### Usage:
Within Python Virtual Environment: 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. 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. 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.
5. Use your favourite scheduling method to set `python mastofeeds.py` to run regularly.

Veure arxiu

@ -66,7 +66,10 @@ def create_table(db, db_user, table, sql):
conn.close() conn.close()
############################################################################################# ###############################################################################
# main
if __name__ == '__main__':
# Load configuration from config file # Load configuration from config file
config_filepath = "db_config.txt" config_filepath = "db_config.txt"
@ -82,9 +85,7 @@ conn = None
try: try:
conn = psycopg2.connect(dbname='postgres', conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='')
user=feeds_db_user, host='',
password='')
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
@ -92,9 +93,8 @@ try:
print("Creating database " + feeds_db + ". Please wait...") print("Creating database " + feeds_db + ". Please wait...")
cur.execute(sql.SQL("CREATE DATABASE {}").format( cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db)))
sql.Identifier(feeds_db))
)
print("Database " + feeds_db + " created!") print("Database " + feeds_db + " created!")
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
@ -117,6 +117,7 @@ try:
except (Exception, psycopg2.DatabaseError) as error: except (Exception, psycopg2.DatabaseError) as error:
print(error) print(error)
# Load configuration from config file # Load configuration from config file
os.remove("db_config.txt") os.remove("db_config.txt")
print("Exiting. Run setup again with right parameters") print("Exiting. Run setup again with right parameters")
@ -134,7 +135,7 @@ if conn is not None:
print("Creating table...") print("Creating table...")
######################################## #####################################
db = feeds_db db = feeds_db
db_user = feeds_db_user db_user = feeds_db_user

Veure arxiu

@ -3,10 +3,7 @@ import feedparser
from mastodon import Mastodon from mastodon import Mastodon
import psycopg2 import psycopg2
import sys import sys
import time
###############################################################################
# INITIALISATION
###############################################################################
# Returns the parameter from the specified file # Returns the parameter from the specified file
def get_parameter( parameter, file_path ): def get_parameter( parameter, file_path ):
@ -25,6 +22,11 @@ def get_parameter( parameter, file_path ):
print(file_path + " Missing parameter %s "%parameter) print(file_path + " Missing parameter %s "%parameter)
sys.exit(0) sys.exit(0)
###############################################################################
# main
if __name__ == '__main__':
# Load secrets from secrets file # Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt" secrets_filepath = "secrets/secrets.txt"
uc_client_id = get_parameter("uc_client_id", secrets_filepath) uc_client_id = get_parameter("uc_client_id", secrets_filepath)
@ -52,11 +54,19 @@ mastodon = Mastodon(
# Initialise access headers # Initialise access headers
headers={ 'Authorization': 'Bearer %s'%uc_access_token } headers={ 'Authorization': 'Bearer %s'%uc_access_token }
######################################################## #######################################################################
publish = 0 publish = 0
try:
newsfeeds = feedparser.parse(feeds_url) newsfeeds = feedparser.parse(feeds_url)
print(newsfeeds.status)
except:
print(newsfeeds.status)
sys.exit(0)
for entry in newsfeeds.entries: for entry in newsfeeds.entries:
@ -66,7 +76,6 @@ for entry in newsfeeds.entries:
################################################################### ###################################################################
# check database if feed is already published # check database if feed is already published
###################################################################
try: try:
@ -75,7 +84,7 @@ for entry in newsfeeds.entries:
cur = conn.cursor() 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() row = cur.fetchone()
if row == None: if row == None:
@ -107,9 +116,11 @@ for entry in newsfeeds.entries:
mastodon.status_post(toot_text, in_reply_to_id=None,) mastodon.status_post(toot_text, in_reply_to_id=None,)
time.sleep(2)
######################################################### #########################################################
insert_line = 'INSERT INTO feeds(link) VALUES (%s)' insert_line = 'INSERT INTO feeds(id, link) VALUES (%s, %s)'
conn = None conn = None
@ -119,7 +130,7 @@ for entry in newsfeeds.entries:
cur = conn.cursor() cur = conn.cursor()
cur.execute(insert_line, (link,)) cur.execute(insert_line, (id, link,))
conn.commit() conn.commit()
@ -137,3 +148,4 @@ for entry in newsfeeds.entries:
else: else:
print("Any new feeds") print("Any new feeds")
sys.exit(0)

Veure arxiu

@ -114,7 +114,6 @@ def log_in():
sys.exit(a_error) sys.exit(a_error)
finally: finally:
if error == 0: if error == 0:
create_dir() create_dir()
create_file() create_file()
write_params() write_params()
@ -175,6 +174,11 @@ def get_hostname( parameter, config_filepath ):
print("hostname setup done!") print("hostname setup done!")
sys.exit(0) sys.exit(0)
###############################################################################
# main
if __name__ == '__main__':
# Load secrets from secrets file # Load secrets from secrets file
secrets_filepath = "secrets/secrets.txt" secrets_filepath = "secrets/secrets.txt"
uc_client_id = get_parameter("uc_client_id", secrets_filepath) uc_client_id = get_parameter("uc_client_id", secrets_filepath)
@ -183,4 +187,4 @@ uc_access_token = get_parameter("uc_access_token", secrets_filepath)
# Load configuration from config file # Load configuration from config file
config_filepath = "config.txt" config_filepath = "config.txt"
mastodon_hostname = get_hostname("mastodon_hostname", config_filepath) # E.g., mastodon.social mastodon_hostname = get_hostname("mastodon_hostname", config_filepath)