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**
|
||||
- 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.
|
||||
|
||||
|
|
75
db-setup.py
75
db-setup.py
|
@ -66,25 +66,26 @@ 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 = psycopg2.connect(dbname='postgres', user=feeds_db_user, host='', password='')
|
||||
|
||||
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
|
||||
|
||||
|
@ -92,58 +93,58 @@ try:
|
|||
|
||||
print("Creating database " + feeds_db + ". Please wait...")
|
||||
|
||||
cur.execute(sql.SQL("CREATE DATABASE {}").format(
|
||||
sql.Identifier(feeds_db))
|
||||
)
|
||||
cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(feeds_db)))
|
||||
|
||||
print("Database " + feeds_db + " created!")
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
finally:
|
||||
|
||||
if conn is not None:
|
||||
|
||||
conn.close()
|
||||
|
||||
#############################################################################################
|
||||
#############################################################################################
|
||||
|
||||
try:
|
||||
try:
|
||||
|
||||
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:
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
|
||||
print(error)
|
||||
|
||||
# Load configuration from config file
|
||||
os.remove("db_config.txt")
|
||||
print("Exiting. Run setup again with right parameters")
|
||||
sys.exit(0)
|
||||
|
||||
if conn is not None:
|
||||
if conn is not None:
|
||||
|
||||
print("\n")
|
||||
print("Host parameters saved to config.txt!")
|
||||
print("\n")
|
||||
|
||||
############################################################
|
||||
# Create needed tables
|
||||
############################################################
|
||||
############################################################
|
||||
# Create needed tables
|
||||
############################################################
|
||||
|
||||
print("Creating table...")
|
||||
print("Creating table...")
|
||||
|
||||
########################################
|
||||
#####################################
|
||||
|
||||
db = feeds_db
|
||||
db_user = feeds_db_user
|
||||
table = "feeds"
|
||||
sql = "create table "+table+" (link varchar(300) PRIMARY KEY)"
|
||||
create_table(db, db_user, table, sql)
|
||||
db = feeds_db
|
||||
db_user = feeds_db_user
|
||||
table = "feeds"
|
||||
sql = "create table "+table+" (link varchar(300) PRIMARY KEY)"
|
||||
create_table(db, db_user, table, sql)
|
||||
|
||||
#####################################
|
||||
#####################################
|
||||
|
||||
print("Done!")
|
||||
print("Now you can run setup.py!")
|
||||
print("\n")
|
||||
print("Done!")
|
||||
print("Now you can run setup.py!")
|
||||
print("\n")
|
||||
|
|
|
@ -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 ):
|
||||
|
@ -25,40 +22,53 @@ 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)
|
||||
###############################################################################
|
||||
# main
|
||||
|
||||
# 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)
|
||||
if __name__ == '__main__':
|
||||
|
||||
# Load configuration from config file
|
||||
config_filepath = "config.txt"
|
||||
mastodon_hostname = get_parameter("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)
|
||||
|
||||
# Initialise Mastodon API
|
||||
mastodon = Mastodon(
|
||||
# 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 }
|
||||
# Initialise access headers
|
||||
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']
|
||||
id = entry['id']
|
||||
|
@ -66,7 +76,6 @@ for entry in newsfeeds.entries:
|
|||
|
||||
###################################################################
|
||||
# check database if feed is already published
|
||||
###################################################################
|
||||
|
||||
try:
|
||||
|
||||
|
@ -75,7 +84,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:
|
||||
|
@ -107,9 +116,11 @@ for entry in newsfeeds.entries:
|
|||
|
||||
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
|
||||
|
||||
|
@ -119,7 +130,7 @@ for entry in newsfeeds.entries:
|
|||
|
||||
cur = conn.cursor()
|
||||
|
||||
cur.execute(insert_line, (link,))
|
||||
cur.execute(insert_line, (id, link,))
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
@ -137,3 +148,4 @@ for entry in newsfeeds.entries:
|
|||
else:
|
||||
|
||||
print("Any new feeds")
|
||||
sys.exit(0)
|
||||
|
|
22
setup.py
22
setup.py
|
@ -114,7 +114,6 @@ def log_in():
|
|||
sys.exit(a_error)
|
||||
finally:
|
||||
if error == 0:
|
||||
|
||||
create_dir()
|
||||
create_file()
|
||||
write_params()
|
||||
|
@ -175,12 +174,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)
|
||||
|
|
Loading…
Referencia en una nova incidència