diff --git a/config-example.json b/config-example.json index e2e3c42..670cd9f 100644 --- a/config-example.json +++ b/config-example.json @@ -5,8 +5,14 @@ "password": "correcthorsebatterystaple" }, "sources": { - "twitter": "metalabVie", - "soup": "metalab.soup.io" + "twitter": { + "user": "metalabVie", + "visibility": "unlisted" + }, + "soup": { + "user": "metalab.soup.io", + "visibility": "public" + } }, "settings": { "days": 1, diff --git a/tootbot.py b/tootbot.py index 8d7557b..2ed1b6e 100755 --- a/tootbot.py +++ b/tootbot.py @@ -29,8 +29,8 @@ mastinstance = config['mastodon']['instance'] mastuser = config['mastodon']['user'] mastpasswd = config['mastodon']['password'] -twitter = config['sources']['twitter'] -soup = config['sources']['soup'] +twitteruser = config['sources']['twitter']['user'] +soupuser = config['sources']['soup']['user'] dryrun = config['settings']['dryrun'] days = config['settings']['days'] @@ -80,13 +80,13 @@ def register_app(mastuser,mastpasswd,mastinstance,mastodon_api): print('====== TWITTER ======') -t = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+twitter) +t = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+twitteruser) for p in reversed(t.entries): # check if this tweet has been processed db.execute( 'SELECT * FROM posts WHERE srcpost = ? AND srcuser = ? AND mastuser = ? AND mastinstance = ?', - (p.id, twitter, mastuser, mastinstance) + (p.id, twitteruser, mastuser, mastinstance) ) last = db.fetchone() @@ -113,7 +113,7 @@ for p in reversed(t.entries): c = p.title - if p.author != '(%s)' % twitter: + if p.author != '(%s)' % twitteruser: c = ("RT %s\n" % p.author[1:-1]) + c toot_media = [] # get the pictures... @@ -145,10 +145,10 @@ for p in reversed(t.entries): print(c) if (not dryrun): - toot = mastodon_api.status_post(c, in_reply_to_id=None, media_ids=toot_media, sensitive=False, visibility='unlisted', spoiler_text=None) + toot = mastodon_api.status_post(c, in_reply_to_id=None, media_ids=toot_media, sensitive=False, visibility=config['sources']['twitter']['visibility'], spoiler_text=None) print( '--> toot posted!') try: - db.execute("INSERT INTO posts VALUES ( ? , ? , ? , ? , ? )", (p.id, twitter, toot.id, mastuser, mastinstance)) + db.execute("INSERT INTO posts VALUES ( ? , ? , ? , ? , ? )", (p.id, twitteruser, toot.id, mastuser, mastinstance)) sql.commit() except: print('database execution failed.') @@ -170,13 +170,13 @@ h.ignore_links = True h.ignore_images = True h.body_width = 0 -s = feedparser.parse('http://'+soup+'/rss') +s = feedparser.parse('http://'+soupuser+'/rss') for p in reversed(s.entries): # check if this tweet has been processed db.execute( 'SELECT * FROM posts WHERE srcpost = ? AND srcuser = ? AND mastuser = ? AND mastinstance = ?', - (p.id, soup, mastuser, mastinstance) + (p.id, soupuser, mastuser, mastinstance) ) last = db.fetchone() @@ -190,23 +190,23 @@ for p in reversed(s.entries): j = json.loads(p.soup_attributes) # get status id and user if twitter is source - twitterstatus = None - twitteruser = None + tweet_id = None + tweet_author = None if (isinstance(j['source'], str)): if ( j['source'].startswith('https://twitter.com/') or j['source'].startswith('https://mobile.twitter.com/')): twitterurl = j['source'].split('/') - twitteruser = twitterurl[3] + tweet_author = twitterurl[3] if ( twitterurl[4] == 'status'): - twitterstatus = twitterurl[5] + tweet_id = twitterurl[5] # get all tweeted statuses - db.execute('SELECT srcpost FROM posts where srcuser = ?', (twitter,)) + print(twitteruser) + db.execute('SELECT srcpost FROM posts where srcuser = ?', (twitteruser,)) postedtweets = [] for postedtweet in db.fetchall(): postedtweets.append(postedtweet[0].split('/')[-1]) - # check if already tweeted - if twitterstatus in postedtweets: + if tweet_id in postedtweets: print('Already tweeted: ', j['source']) else: @@ -258,11 +258,11 @@ for p in reversed(s.entries): if (not dryrun): # post toot - toot = mastodon_api.status_post(text, in_reply_to_id=None, media_ids=toot_media, sensitive=False, visibility='public', spoiler_text=None) + toot = mastodon_api.status_post(text, in_reply_to_id=None, media_ids=toot_media, sensitive=False, visibility=config['sources']['soup']['visibility'], spoiler_text=None) # add entry to database if "id" in toot: - db.execute("INSERT INTO posts VALUES ( ? , ? , ? , ? , ? )", (p.id, soup, toot.id, mastuser, mastinstance)) + db.execute("INSERT INTO posts VALUES ( ? , ? , ? , ? , ? )", (p.id, soupuser, toot.id, mastuser, mastinstance)) sql.commit() print( '--> ', p.id, ' posted!') else: