Increased link column to varchar(300)
This commit is contained in:
commit
c9219f1a85
S'han modificat 4 arxius amb 314 adicions i 298 eliminacions
15
README.md
15
README.md
|
@ -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.
|
||||||
|
|
||||||
|
|
75
db-setup.py
75
db-setup.py
|
@ -66,25 +66,26 @@ def create_table(db, db_user, table, sql):
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
#############################################################################################
|
###############################################################################
|
||||||
|
# main
|
||||||
|
|
||||||
# Load configuration from config file
|
if __name__ == '__main__':
|
||||||
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)
|
|
||||||
|
|
||||||
############################################################
|
# Load configuration from config file
|
||||||
# create database
|
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',
|
try:
|
||||||
user=feeds_db_user, host='',
|
|
||||||
password='')
|
conn = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='')
|
||||||
|
|
||||||
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
||||||
|
|
||||||
|
@ -92,58 +93,58 @@ 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:
|
||||||
|
|
||||||
print(error)
|
print(error)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
|
|
||||||
if conn is not None:
|
if conn is not None:
|
||||||
|
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
#############################################################################################
|
#############################################################################################
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
||||||
conn = None
|
conn = None
|
||||||
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
conn = psycopg2.connect(database = feeds_db, user = feeds_db_user, password = "", host = "/var/run/postgresql", port = "5432")
|
||||||
|
|
||||||
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")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if conn is not None:
|
if conn is not None:
|
||||||
|
|
||||||
print("\n")
|
print("\n")
|
||||||
print("Host parameters saved to config.txt!")
|
print("Host parameters saved to config.txt!")
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
||||||
############################################################
|
############################################################
|
||||||
# Create needed tables
|
# Create needed tables
|
||||||
############################################################
|
############################################################
|
||||||
|
|
||||||
print("Creating table...")
|
print("Creating table...")
|
||||||
|
|
||||||
########################################
|
#####################################
|
||||||
|
|
||||||
db = feeds_db
|
db = feeds_db
|
||||||
db_user = feeds_db_user
|
db_user = feeds_db_user
|
||||||
table = "feeds"
|
table = "feeds"
|
||||||
sql = "create table "+table+" (link varchar(300) PRIMARY KEY)"
|
sql = "create table "+table+" (link varchar(300) PRIMARY KEY)"
|
||||||
create_table(db, db_user, table, sql)
|
create_table(db, db_user, table, sql)
|
||||||
|
|
||||||
#####################################
|
#####################################
|
||||||
|
|
||||||
print("Done!")
|
print("Done!")
|
||||||
print("Now you can run setup.py!")
|
print("Now you can run setup.py!")
|
||||||
print("\n")
|
print("\n")
|
||||||
|
|
|
@ -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,40 +22,53 @@ 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)
|
||||||
|
|
||||||
# Load secrets from secrets file
|
###############################################################################
|
||||||
secrets_filepath = "secrets/secrets.txt"
|
# main
|
||||||
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
|
if __name__ == '__main__':
|
||||||
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
|
# Load secrets from secrets file
|
||||||
config_filepath = "config.txt"
|
secrets_filepath = "secrets/secrets.txt"
|
||||||
mastodon_hostname = get_parameter("mastodon_hostname", config_filepath) # E.g., mastodon.social
|
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)
|
||||||
|
|
||||||
# Initialise Mastodon API
|
# Load configuration from config file
|
||||||
mastodon = Mastodon(
|
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_id = uc_client_id,
|
||||||
client_secret = uc_client_secret,
|
client_secret = uc_client_secret,
|
||||||
access_token = uc_access_token,
|
access_token = uc_access_token,
|
||||||
api_base_url = 'https://' + mastodon_hostname,
|
api_base_url = 'https://' + mastodon_hostname,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Initialise access headers
|
# Initialise access headers
|
||||||
headers={ 'Authorization': 'Bearer %s'%uc_access_token }
|
headers={ 'Authorization': 'Bearer %s'%uc_access_token }
|
||||||
|
|
||||||
########################################################
|
#######################################################################
|
||||||
|
|
||||||
publish = 0
|
publish = 0
|
||||||
|
|
||||||
newsfeeds = feedparser.parse(feeds_url)
|
try:
|
||||||
|
|
||||||
for entry in newsfeeds.entries:
|
newsfeeds = feedparser.parse(feeds_url)
|
||||||
|
print(newsfeeds.status)
|
||||||
|
|
||||||
|
except:
|
||||||
|
|
||||||
|
print(newsfeeds.status)
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
for entry in newsfeeds.entries:
|
||||||
|
|
||||||
title = entry['title']
|
title = entry['title']
|
||||||
id = entry['id']
|
id = entry['id']
|
||||||
|
@ -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)
|
||||||
|
|
22
setup.py
22
setup.py
|
@ -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,12 +174,17 @@ def get_hostname( parameter, config_filepath ):
|
||||||
print("hostname setup done!")
|
print("hostname setup done!")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
# Load secrets from secrets file
|
###############################################################################
|
||||||
secrets_filepath = "secrets/secrets.txt"
|
# main
|
||||||
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
|
if __name__ == '__main__':
|
||||||
config_filepath = "config.txt"
|
|
||||||
mastodon_hostname = get_hostname("mastodon_hostname", config_filepath) # E.g., mastodon.social
|
# 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)
|
||||||
|
|
Loading…
Referencia en una nova incidència