2018-10-08 21:11:51 -04:00
#!/usr/bin/env python3
2018-10-24 22:37:11 -04:00
# toot downloader version two!!
2018-10-08 21:11:51 -04: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/.
from mastodon import Mastodon
from os import path
from bs4 import BeautifulSoup
2019-02-20 22:37:45 -05:00
import os , sqlite3 , signal , sys , json , re , shutil
2018-10-24 22:37:11 -04:00
import requests
2019-01-11 07:58:17 -05:00
import functions
2018-10-08 21:11:51 -04:00
2018-10-29 00:36:21 -04:00
scopes = [ " read:statuses " , " read:accounts " , " read:follows " , " write:statuses " , " read:notifications " ]
2019-02-20 22:37:45 -05:00
try :
cfg = json . load ( open ( ' config.json ' , ' r ' ) )
except :
shutil . copy2 ( " config.sample.json " , " config.json " )
cfg = json . load ( open ( ' config.json ' , ' r ' ) )
2018-10-08 21:11:51 -04:00
2019-02-24 20:17:06 -05:00
#config.json should contain the instance URL, the instance blacklist (for dead/broken instances), and the CW text. if they're not provided, we'll fall back to defaults.
# TODO: this is pretty messy
2019-01-11 07:05:29 -05:00
if ' site ' not in cfg :
cfg [ ' website ' ] = " https://botsin.space "
if ' cw ' not in cfg :
2019-01-11 08:05:45 -05:00
cfg [ ' cw ' ] = None
2019-01-11 08:08:53 -05:00
if ' instance_blacklist ' not in cfg :
cfg [ " instance_blacklist " ] = [
" bofa.lol " ,
" witches.town "
]
2019-02-24 20:17:06 -05:00
if ' learn_from_cw ' not in cfg :
cfg [ ' learn_from_cw ' ] = False
2019-01-11 07:05:29 -05:00
#if the user is using a (very!) old version that still uses the .secret files, migrate to the new method
2018-10-24 22:37:11 -04:00
if os . path . exists ( " clientcred.secret " ) :
2019-02-07 10:45:44 -05:00
print ( " Upgrading to new storage method " )
cc = open ( " clientcred.secret " ) . read ( ) . split ( " \n " )
cfg [ ' client ' ] = {
" id " : cc [ 0 ] ,
" secret " : cc [ 1 ]
}
cfg [ ' secret ' ] = open ( " usercred.secret " ) . read ( ) . rstrip ( " \n " )
os . remove ( " clientcred.secret " )
os . remove ( " usercred.secret " )
2018-10-27 04:28:20 -04:00
2018-10-24 22:37:11 -04:00
if " client " not in cfg :
2019-01-11 07:08:10 -05:00
print ( " No application info -- registering application with {} " . format ( cfg [ ' site ' ] ) )
2018-10-24 22:37:11 -04:00
client_id , client_secret = Mastodon . create_app ( " mstdn-ebooks " ,
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 07:08:10 -05:00
print ( " No user credentials -- logging in to {} " . format ( cfg [ ' site ' ] ) )
2018-10-24 22:37:11 -04:00
client = Mastodon ( client_id = cfg [ ' client ' ] [ ' id ' ] ,
client_secret = cfg [ ' client ' ] [ ' secret ' ] ,
api_base_url = cfg [ ' site ' ] )
2018-10-08 21:11:51 -04:00
2019-01-11 07:08:10 -05: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-24 22:37:11 -04:00
cfg [ ' secret ' ] = client . log_in ( code = input ( " Secret: " ) , scopes = scopes )
2018-10-08 21:11:51 -04:00
2018-10-24 22:37:11 -04:00
json . dump ( cfg , open ( " config.json " , " w+ " ) )
2018-10-08 21:11:51 -04:00
2018-10-24 22:37:11 -04:00
def extract_toot ( toot ) :
2019-01-11 07:58:17 -05:00
toot = functions . extract_toot ( toot )
2018-11-27 06:29:50 -05:00
toot = toot . replace ( " @ " , " @ \u200B " ) #put a zws between @ and username to avoid mentioning
2018-10-24 22:37:11 -04:00
return ( toot )
2018-10-08 21:11:51 -04:00
client = Mastodon (
2018-10-24 22:37:11 -04:00
client_id = cfg [ ' client ' ] [ ' id ' ] ,
client_secret = cfg [ ' client ' ] [ ' secret ' ] ,
access_token = cfg [ ' secret ' ] ,
api_base_url = cfg [ ' site ' ] )
2018-10-08 21:11:51 -04:00
me = client . account_verify_credentials ( )
following = client . account_following ( me . id )
db = sqlite3 . connect ( " toots.db " )
db . text_factory = str
c = db . cursor ( )
2019-02-24 20:17:06 -05:00
c . execute ( " CREATE TABLE IF NOT EXISTS `toots` (id INT NOT NULL UNIQUE PRIMARY KEY, cw INT NOT NULL DEFAULT 0, userid INT NOT NULL, uri VARCHAR NOT NULL, content VARCHAR NOT NULL) WITHOUT ROWID " )
try :
c . execute ( " ALTER TABLE `toots` ADD COLUMN cw INT NOT NULL DEFAULT 0 " )
except :
pass # column already exists
2018-10-08 21:11:51 -04:00
db . commit ( )
def handleCtrlC ( signal , frame ) :
print ( " \n PREMATURE EVACUATION - Saving chunks " )
db . commit ( )
sys . exit ( 1 )
signal . signal ( signal . SIGINT , handleCtrlC )
2019-02-07 10:27:52 -05:00
patterns = {
2019-02-07 10:45:44 -05:00
" handle " : re . compile ( r " ^.*@(.+) " ) ,
" url " : re . compile ( r " https?: \ / \ /(.*) " ) ,
" uri " : re . compile ( r ' template= " ([^ " ]+) " ' ) ,
" pid " : re . compile ( r " [^ \ /]+$ " ) ,
2019-02-07 10:27:52 -05:00
}
2018-10-27 04:28:20 -04:00
2018-10-08 21:11:51 -04:00
for f in following :
last_toot = c . execute ( " SELECT id FROM `toots` WHERE userid LIKE ? ORDER BY id DESC LIMIT 1 " , ( f . id , ) ) . fetchone ( )
if last_toot != None :
last_toot = last_toot [ 0 ]
else :
last_toot = 0
2018-10-24 22:37:11 -04:00
print ( " Harvesting toots for user @ {} , starting from {} " . format ( f . acct , last_toot ) )
#find the user's activitypub outbox
2019-02-24 20:18:38 -05:00
print ( " WebFingering... " )
2019-02-07 10:45:44 -05:00
instance = patterns [ " handle " ] . search ( f . acct )
2018-10-24 22:37:11 -04:00
if instance == None :
2019-02-07 10:45:44 -05:00
instance = patterns [ " url " ] . search ( cfg [ ' site ' ] ) . group ( 1 )
2018-10-24 22:37:11 -04:00
else :
instance = instance . group ( 1 )
2019-01-11 08:08:53 -05:00
if instance in cfg [ ' instance_blacklist ' ] :
print ( " skipping blacklisted instance: {} " . format ( instance ) )
2018-10-25 10:33:57 -04:00
continue
2019-01-11 07:15:05 -05:00
2018-10-24 22:37:11 -04:00
try :
2018-10-27 08:07:38 -04:00
r = requests . get ( " https:// {} /.well-known/host-meta " . format ( instance ) , timeout = 10 )
2019-02-07 10:45:44 -05:00
uri = patterns [ " uri " ] . search ( r . text ) . group ( 1 )
2018-10-24 22:37:11 -04:00
uri = uri . format ( uri = " {} @ {} " . format ( f . username , instance ) )
2018-10-27 08:07:38 -04:00
r = requests . get ( uri , headers = { " Accept " : " application/json " } , timeout = 10 )
2018-10-27 04:28:20 -04:00
j = r . json ( )
2019-02-06 19:53:23 -05:00
for link in j [ ' links ' ] :
if link [ ' rel ' ] == ' self ' :
#this is a link formatted like "https://instan.ce/users/username", which is what we need
uri = link [ ' href ' ]
2018-11-07 00:39:12 -05:00
uri = " {} /outbox?page=true " . format ( uri )
2018-10-27 08:07:38 -04:00
r = requests . get ( uri , timeout = 10 )
2018-10-24 22:37:11 -04:00
j = r . json ( )
except Exception :
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 04:28:20 -04:00
pleroma = False
2018-11-09 06:26:37 -05:00
if ' first ' in j and type ( j [ ' first ' ] ) != str :
2018-11-07 00:39:12 -05:00
print ( " Pleroma instance detected " )
2018-10-27 04:28:20 -04:00
pleroma = True
2018-11-07 00:39:12 -05:00
j = j [ ' first ' ]
2018-11-09 06:49:33 -05:00
else :
2019-02-06 19:53:23 -05:00
print ( " Mastodon/Misskey instance detected " )
2018-11-09 06:49:33 -05:00
uri = " {} &min_id= {} " . format ( uri , last_toot )
2018-11-07 00:39:12 -05:00
r = requests . get ( uri )
j = r . json ( )
2019-01-11 07:08:10 -05:00
print ( " Downloading and saving toots " , end = ' ' , flush = True )
2018-11-07 00:39:12 -05:00
done = False
2018-11-09 06:50:36 -05:00
try :
2018-11-07 00:39:12 -05:00
while not done and len ( j [ ' orderedItems ' ] ) > 0 :
for oi in j [ ' orderedItems ' ] :
2018-11-28 14:36:05 -05:00
if oi [ ' type ' ] != " Create " :
2019-01-11 07:15:05 -05:00
continue #this isn't a toot/post/status/whatever, it's a boost or a follow or some other activitypub thing. ignore
2018-11-28 14:36:05 -05:00
# its a toost baby
content = oi [ ' object ' ] [ ' content ' ]
toot = extract_toot ( content )
# print(toot)
try :
if pleroma :
2018-12-29 18:58:43 -05:00
if c . execute ( " SELECT COUNT(*) FROM toots WHERE uri LIKE ? " , ( oi [ ' object ' ] [ ' id ' ] , ) ) . fetchone ( ) [ 0 ] > 0 :
2018-11-28 14:36:05 -05:00
#we've caught up to the notices we've already downloaded, so we can stop now
2019-01-11 08:02:36 -05:00
#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, "it's called mstdn-ebooks not fediverse-ebooks. pleroma support is an afterthought"
2018-11-28 14:36:05 -05:00
done = True
2019-02-07 10:45:44 -05:00
pid = patterns [ " pid " ] . search ( oi [ ' object ' ] [ ' id ' ] ) . group ( 0 )
2019-02-24 20:28:05 -05:00
c . execute ( " REPLACE INTO toots (id, cw, userid, uri, content) VALUES (?, ?, ?, ?, ?) " , (
2019-01-11 08:05:45 -05:00
pid ,
2019-02-24 20:17:06 -05:00
1 if ( oi [ ' object ' ] [ ' summary ' ] != None and oi [ ' object ' ] [ ' summary ' ] != " " ) else 0 ,
2018-11-28 14:36:05 -05:00
f . id ,
oi [ ' object ' ] [ ' id ' ] ,
toot
2018-11-07 00:39:12 -05:00
)
2018-11-28 14:36:05 -05:00
)
pass
except :
pass #ignore any toots that don't successfully go into the DB
2018-11-07 00:39:12 -05:00
if not pleroma :
r = requests . get ( j [ ' prev ' ] , timeout = 15 )
else :
r = requests . get ( j [ ' next ' ] , timeout = 15 )
j = r . json ( )
print ( ' . ' , end = ' ' , flush = True )
2018-10-24 22:37:11 -04:00
print ( " Done! " )
db . commit ( )
2018-11-09 06:50:36 -05:00
except :
2019-01-11 07:08:10 -05:00
print ( " Encountered an error! Saving toots to database and moving to next followed account. " )
2018-11-09 06:50:36 -05:00
db . commit ( )
2018-11-01 01:27:03 -04:00
print ( " Done! " )
2018-10-08 21:11:51 -04:00
db . commit ( )
db . execute ( " VACUUM " ) #compact db
db . commit ( )
2018-12-29 18:58:43 -05:00
db . close ( )