2018-10-09 01:11:51 +00:00
#!/usr/bin/env python3
2018-10-25 02:37:11 +00:00
# toot downloader version two!!
2018-10-09 01:11:51 +00:00
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
2019-08-15 01:56:27 +00:00
from mastodon import Mastodon , MastodonUnauthorizedError
2021-06-04 21:38:36 +00:00
import sqlite3 , signal , sys , json , re , argparse
2018-10-25 02:37:11 +00:00
import requests
2019-01-11 12:58:17 +00:00
import functions
2018-10-09 01:11:51 +00:00
2019-08-07 03:46:57 +00:00
parser = argparse . ArgumentParser ( description = ' Log in and download posts. ' )
2021-06-04 21:38:36 +00:00
parser . add_argument (
' -c ' , ' --cfg ' , dest = ' cfg ' , default = ' config.json ' , nargs = ' ? ' ,
help = " Specify a custom location for config.json. " )
2019-08-07 03:46:57 +00:00
args = parser . parse_args ( )
2019-05-19 12:31:42 +00:00
scopes = [ " read:statuses " , " read:accounts " , " read:follows " , " write:statuses " , " read:notifications " , " write:accounts " ]
2021-06-04 21:38:36 +00:00
# cfg defaults
2019-05-19 12:31:42 +00:00
2019-04-29 03:59:37 +00:00
cfg = {
" site " : " https://botsin.space " ,
" cw " : None ,
2021-06-04 21:38:36 +00:00
" instance_blacklist " : [ " bofa.lol " , " witches.town " , " knzk.me " ] , # rest in piece
2019-04-29 04:24:52 +00:00
" learn_from_cw " : False ,
" mention_handling " : 1 ,
2019-07-01 07:19:52 +00:00
" max_thread_length " : 15 ,
2021-06-04 21:14:56 +00:00
" strip_paired_punctuation " : False ,
" limit_length " : False ,
" length_lower_limit " : 5 ,
" length_upper_limit " : 50 ,
" overlap_ratio_enabled " : False ,
" overlap_ratio " : 0.7
2019-04-29 03:59:37 +00:00
}
2019-05-19 12:31:42 +00:00
2019-08-14 05:00:35 +00:00
try :
cfg . update ( json . load ( open ( args . cfg , ' r ' ) ) )
except FileNotFoundError :
open ( args . cfg , " w " ) . write ( " {} " )
2019-08-07 03:46:57 +00:00
2020-03-10 06:54:00 +00:00
print ( " Using {} as configuration file " . format ( args . cfg ) )
2020-03-10 06:53:30 +00:00
if not cfg [ ' site ' ] . startswith ( " https:// " ) and not cfg [ ' site ' ] . startswith ( " http:// " ) :
print ( " Site must begin with ' https:// ' or ' http:// ' . Value ' {} ' is invalid - try ' https:// {} ' instead. " . format ( cfg [ ' site ' ] ) )
sys . exit ( 1 )
2018-10-25 02:37:11 +00:00
if " client " not in cfg :
2019-01-11 12:08:10 +00:00
print ( " No application info -- registering application with {} " . format ( cfg [ ' site ' ] ) )
2021-06-04 21:38:36 +00:00
client_id , client_secret = Mastodon . create_app (
" mstdn-ebooks " ,
2018-10-25 02:37:11 +00:00
api_base_url = cfg [ ' site ' ] ,
scopes = scopes ,
website = " https://github.com/Lynnesbian/mstdn-ebooks " )
cfg [ ' client ' ] = {
" id " : client_id ,
" secret " : client_secret
}
if " secret " not in cfg :
2019-01-11 12:08:10 +00:00
print ( " No user credentials -- logging in to {} " . format ( cfg [ ' site ' ] ) )
2021-06-04 21:38:36 +00:00
client = Mastodon (
client_id = cfg [ ' client ' ] [ ' id ' ] ,
client_secret = cfg [ ' client ' ] [ ' secret ' ] ,
2018-10-25 02:37:11 +00:00
api_base_url = cfg [ ' site ' ] )
2018-10-09 01:11:51 +00:00
2019-01-11 12:08:10 +00:00
print ( " Open this URL and authenticate to give mstdn-ebooks access to your bot ' s account: {} " . format ( client . auth_request_url ( scopes = scopes ) ) )
2018-10-25 02:37:11 +00:00
cfg [ ' secret ' ] = client . log_in ( code = input ( " Secret: " ) , scopes = scopes )
2018-10-09 01:11:51 +00:00
2019-08-07 03:46:57 +00:00
json . dump ( cfg , open ( args . cfg , " w+ " ) )
2018-10-09 01:11:51 +00:00
2021-06-04 21:38:36 +00:00
2018-10-25 02:37:11 +00:00
def extract_toot ( toot ) :
2019-01-11 12:58:17 +00:00
toot = functions . extract_toot ( toot )
2021-06-04 21:38:36 +00:00
toot = toot . replace ( " @ " , " @ \u200B " ) # put a zws between @ and username to avoid mentioning
2018-10-25 02:37:11 +00:00
return ( toot )
2018-10-09 01:11:51 +00:00
2021-06-04 21:38:36 +00:00
2018-10-09 01:11:51 +00:00
client = Mastodon (
2018-10-25 02:37:11 +00:00
client_id = cfg [ ' client ' ] [ ' id ' ] ,
2021-06-04 21:38:36 +00:00
client_secret = cfg [ ' client ' ] [ ' secret ' ] ,
2019-02-25 18:30:40 +00:00
access_token = cfg [ ' secret ' ] ,
2018-10-25 02:37:11 +00:00
api_base_url = cfg [ ' site ' ] )
2018-10-09 01:11:51 +00:00
2019-08-15 01:56:27 +00:00
try :
me = client . account_verify_credentials ( )
except MastodonUnauthorizedError :
print ( " The provided access token in {} is invalid. Please delete {} and run main.py again. " . format ( args . cfg , args . cfg ) )
sys . exit ( 1 )
2018-10-09 01:11:51 +00:00
following = client . account_following ( me . id )
db = sqlite3 . connect ( " toots.db " )
2021-06-04 21:38:36 +00:00
db . text_factory = str
2018-10-09 01:11:51 +00:00
c = db . cursor ( )
2019-08-15 16:02:06 +00:00
c . execute ( " CREATE TABLE IF NOT EXISTS `toots` (sortid INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, id VARCHAR NOT NULL, cw INT NOT NULL DEFAULT 0, userid VARCHAR NOT NULL, uri VARCHAR NOT NULL, content VARCHAR NOT NULL) " )
2021-03-13 19:54:32 +00:00
c . execute ( " CREATE TRIGGER IF NOT EXISTS `dedup` AFTER INSERT ON toots FOR EACH ROW BEGIN DELETE FROM toots WHERE rowid NOT IN (SELECT MIN(sortid) FROM toots GROUP BY uri ); END; " )
2020-03-08 09:46:07 +00:00
db . commit ( )
2019-08-15 01:56:27 +00:00
tableinfo = c . execute ( " PRAGMA table_info(`toots`) " ) . fetchall ( )
found = False
columns = [ ]
for entry in tableinfo :
if entry [ 1 ] == " sortid " :
found = True
break
columns . append ( entry [ 1 ] )
if not found :
print ( " Migrating to new database format. Please wait... " )
print ( " WARNING: If any of the accounts your bot is following are Pleroma users, please delete toots.db and run main.py again to create it anew. " )
try :
c . execute ( " DROP TABLE `toots_temp` " )
except :
pass
c . execute ( " CREATE TABLE `toots_temp` (sortid INTEGER UNIQUE PRIMARY KEY AUTOINCREMENT, id VARCHAR NOT NULL, cw INT NOT NULL DEFAULT 0, userid VARCHAR NOT NULL, uri VARCHAR NOT NULL, content VARCHAR NOT NULL) " )
2019-08-14 05:17:38 +00:00
for f in following :
2019-08-15 01:56:27 +00:00
user_toots = c . execute ( " SELECT * FROM `toots` WHERE userid LIKE ? ORDER BY id " , ( f . id , ) ) . fetchall ( )
2021-06-04 21:38:36 +00:00
if user_toots is None :
2019-08-15 01:56:27 +00:00
continue
if columns [ - 1 ] == " cw " :
for toot in user_toots :
c . execute ( " INSERT INTO `toots_temp` (id, userid, uri, content, cw) VALUES (?, ?, ?, ?, ?) " , toot )
2019-08-14 05:17:38 +00:00
else :
2019-08-15 01:56:27 +00:00
for toot in user_toots :
c . execute ( " INSERT INTO `toots_temp` (id, cw, userid, uri, content) VALUES (?, ?, ?, ?, ?) " , toot )
c . execute ( " DROP TABLE `toots` " )
c . execute ( " ALTER TABLE `toots_temp` RENAME TO `toots` " )
2021-03-13 19:54:32 +00:00
c . execute ( " CREATE TRIGGER IF NOT EXISTS `dedup` AFTER INSERT ON toots FOR EACH ROW BEGIN DELETE FROM toots WHERE rowid NOT IN (SELECT MIN(sortid) FROM toots GROUP BY uri ); END; " )
2020-03-08 09:46:07 +00:00
2018-10-09 01:11:51 +00:00
db . commit ( )
2021-06-04 21:38:36 +00:00
2018-10-09 01:11:51 +00:00
def handleCtrlC ( signal , frame ) :
print ( " \n PREMATURE EVACUATION - Saving chunks " )
db . commit ( )
sys . exit ( 1 )
2021-06-04 21:38:36 +00:00
2018-10-09 01:11:51 +00:00
signal . signal ( signal . SIGINT , handleCtrlC )
2019-02-07 15:27:52 +00:00
patterns = {
2019-02-07 15:45:44 +00:00
" handle " : re . compile ( r " ^.*@(.+) " ) ,
" url " : re . compile ( r " https?: \ / \ /(.*) " ) ,
" uri " : re . compile ( r ' template= " ([^ " ]+) " ' ) ,
" pid " : re . compile ( r " [^ \ /]+$ " ) ,
2019-02-07 15:27:52 +00:00
}
2018-10-27 08:28:20 +00:00
2019-02-25 18:30:40 +00:00
2021-10-16 01:50:55 +00:00
def insert_toot ( post , acc , content , cursor ) : # extracted to prevent duplication
2019-02-25 18:30:40 +00:00
cursor . execute ( " REPLACE INTO toots (id, cw, userid, uri, content) VALUES (?, ?, ?, ?, ?) " , (
2021-10-16 01:50:55 +00:00
post [ ' id ' ] ,
1 if ( post [ ' spoiler_text ' ] is not None and post [ ' spoiler_text ' ] != " " ) else 0 ,
2019-02-25 18:30:40 +00:00
acc . id ,
2021-10-16 01:50:55 +00:00
post [ ' uri ' ] ,
content
2019-02-25 18:30:40 +00:00
) )
2018-10-09 01:11:51 +00:00
for f in following :
2019-08-15 01:56:27 +00:00
last_toot = c . execute ( " SELECT id FROM `toots` WHERE userid LIKE ? ORDER BY sortid DESC LIMIT 1 " , ( f . id , ) ) . fetchone ( )
2021-06-04 21:38:36 +00:00
if last_toot is not None :
2018-10-09 01:11:51 +00:00
last_toot = last_toot [ 0 ]
else :
last_toot = 0
2019-05-19 13:06:31 +00:00
print ( " Downloading posts for user @ {} , starting from {} " . format ( f . acct , last_toot ) )
2018-10-25 02:37:11 +00:00
2021-06-04 21:38:36 +00:00
# find the user's activitypub outbox
2019-02-25 01:18:38 +00:00
print ( " WebFingering... " )
2019-02-07 15:45:44 +00:00
instance = patterns [ " handle " ] . search ( f . acct )
2021-06-04 21:38:36 +00:00
if instance is None :
2019-02-07 15:45:44 +00:00
instance = patterns [ " url " ] . search ( cfg [ ' site ' ] ) . group ( 1 )
2018-10-25 02:37:11 +00:00
else :
instance = instance . group ( 1 )
2019-01-11 13:08:53 +00:00
if instance in cfg [ ' instance_blacklist ' ] :
print ( " skipping blacklisted instance: {} " . format ( instance ) )
2018-10-25 14:33:57 +00:00
continue
2019-01-11 12:15:05 +00:00
2018-10-25 02:37:11 +00:00
try :
2021-10-16 01:50:55 +00:00
# download first 20 toots since last toot
posts = client . account_statuses ( f . id , min_id = last_toot )
2019-05-19 13:06:31 +00:00
except :
2018-10-25 02:37:11 +00:00
print ( " oopsy woopsy!! we made a fucky wucky!!! \n (we ' re probably rate limited, please hang up and try again) " )
sys . exit ( 1 )
2018-10-27 08:28:20 +00:00
2019-05-19 13:06:31 +00:00
print ( " Downloading and saving posts " , end = ' ' , flush = True )
2018-11-07 05:39:12 +00:00
done = False
2018-11-09 11:50:36 +00:00
try :
2021-10-16 01:50:55 +00:00
while not done and len ( posts ) > 0 :
for post in posts :
if post [ ' reblog ' ] is not None :
2021-06-04 21:38:36 +00:00
continue # this isn't a toot/post/status/whatever, it's a boost or a follow or some other activitypub thing. ignore
2019-02-25 18:30:40 +00:00
2018-11-28 19:36:05 +00:00
# its a toost baby
2021-10-16 01:50:55 +00:00
content = post [ ' content ' ]
2018-11-28 19:36:05 +00:00
toot = extract_toot ( content )
# print(toot)
try :
2021-10-16 01:50:55 +00:00
if c . execute ( " SELECT COUNT(*) FROM toots WHERE uri LIKE ? " , ( post [ ' id ' ] , ) ) . fetchone ( ) [ 0 ] > 0 :
# we've caught up to the notices we've already downloaded, so we can stop now
# you might be wondering, "lynne, what if the instance ratelimits you after 40 posts, and they've made 60 since main.py was last run? wouldn't the bot miss 20 posts and never be able to see them?" to which i reply, "i know but i don't know how to fix it"
done = True
2019-07-10 00:43:56 +00:00
if ' lang ' in cfg :
2019-05-06 17:14:30 +00:00
try :
2021-10-16 01:50:55 +00:00
if post [ ' language ' ] == cfg [ ' lang ' ] : # filter for language
insert_toot ( post , f , toot , c )
2019-05-06 17:14:30 +00:00
except KeyError :
2021-10-16 01:50:55 +00:00
# JSON doesn't have language, just insert the toot irregardlessly
insert_toot ( post , f , toot , c )
2019-02-25 18:30:40 +00:00
else :
2021-10-16 01:50:55 +00:00
insert_toot ( post , f , toot , c )
2018-11-28 19:36:05 +00:00
pass
except :
2021-06-04 21:38:36 +00:00
pass # ignore any toots that don't successfully go into the DB
2019-05-19 13:06:31 +00:00
2021-10-16 01:50:55 +00:00
# get the next <20 posts
2019-05-19 13:06:31 +00:00
try :
2021-10-16 01:50:55 +00:00
posts = client . account_statuses ( f . id , min_id = posts [ 0 ] [ ' id ' ] )
2019-05-19 13:06:31 +00:00
except requests . Timeout :
print ( " HTTP timeout, site did not respond within 15 seconds " )
2020-03-08 09:57:06 +00:00
except KeyError :
print ( " Couldn ' t get next page - we ' ve probably got all the posts " )
2019-05-19 13:06:31 +00:00
except :
print ( " An error occurred while trying to obtain more posts. " )
2018-11-07 05:39:12 +00:00
print ( ' . ' , end = ' ' , flush = True )
2018-10-25 02:37:11 +00:00
print ( " Done! " )
db . commit ( )
2019-05-06 17:02:42 +00:00
except requests . HTTPError as e :
if e . response . status_code == 429 :
print ( " Rate limit exceeded. This means we ' re downloading too many posts in quick succession. Saving toots to database and moving to next followed account. " )
db . commit ( )
else :
# TODO: remove duplicate code
2019-05-19 13:06:31 +00:00
print ( " Encountered an error! Saving posts to database and moving to next followed account. " )
2019-05-06 17:02:42 +00:00
db . commit ( )
2018-11-09 11:50:36 +00:00
except :
2019-05-19 13:06:31 +00:00
print ( " Encountered an error! Saving posts to database and moving to next followed account. " )
2018-11-09 11:50:36 +00:00
db . commit ( )
2018-11-01 05:27:03 +00:00
print ( " Done! " )
2018-10-09 01:11:51 +00:00
db . commit ( )
2021-06-04 21:38:36 +00:00
db . execute ( " VACUUM " ) # compact db
2018-10-09 01:11:51 +00:00
db . commit ( )
2018-12-29 23:58:43 +00:00
db . close ( )