Archiving to Nextcloud successfully, added some error checking and handling.
This commit is contained in:
parent
f51506cb02
commit
72e4012489
7 changed files with 120 additions and 20 deletions
3
Pipfile
3
Pipfile
|
@ -10,6 +10,7 @@ pipfile = "*"
|
||||||
|
|
||||||
[packages]
|
[packages]
|
||||||
pipfile = "*"
|
pipfile = "*"
|
||||||
"mastodon.py" = "*"
|
|
||||||
tweepy = "*"
|
tweepy = "*"
|
||||||
python-twitter = "*"
|
python-twitter = "*"
|
||||||
|
"mastodon.py" = "*"
|
||||||
|
webdavclient3 = "*"
|
||||||
|
|
|
@ -2,10 +2,19 @@
|
||||||
|
|
||||||
from tweepy import StreamListener
|
from tweepy import StreamListener
|
||||||
from lib.archival import archive_media_status
|
from lib.archival import archive_media_status
|
||||||
|
from lib.echo_nextcloud import nextcloud_upload_media
|
||||||
|
|
||||||
|
|
||||||
class TweetStreamer(StreamListener):
|
class TweetStreamer(StreamListener):
|
||||||
def on_status(self, status):
|
def on_status(self, status):
|
||||||
print(status._json)
|
print(status._json)
|
||||||
archive_media_status(status)
|
archive_filenames = archive_media_status(status)
|
||||||
|
if(archive_filenames):
|
||||||
|
for filename in archive_filenames:
|
||||||
|
print(filename)
|
||||||
|
try:
|
||||||
|
nextcloud_upload_media(filename, status.timestamp_ms)
|
||||||
|
except Exception as e:
|
||||||
|
print('Was unsuccessful in uploading the file.')
|
||||||
|
print(e)
|
||||||
return super().on_status(status)
|
return super().on_status(status)
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from twitter.twitter_utils import parse_media_file
|
from twitter.twitter_utils import parse_media_file
|
||||||
# from twitter import Api
|
|
||||||
|
|
||||||
|
|
||||||
def archive_media(media_url):
|
def archive_media(media_url):
|
||||||
|
@ -11,6 +10,7 @@ def archive_media(media_url):
|
||||||
archive_file.writelines(temp_media_file.readlines())
|
archive_file.writelines(temp_media_file.readlines())
|
||||||
archive_file.close()
|
archive_file.close()
|
||||||
temp_media_file.close()
|
temp_media_file.close()
|
||||||
|
return filename
|
||||||
|
|
||||||
|
|
||||||
def archive_media_status(status):
|
def archive_media_status(status):
|
||||||
|
@ -18,11 +18,15 @@ def archive_media_status(status):
|
||||||
if(not ('retweeted_status' in status._json.keys())):
|
if(not ('retweeted_status' in status._json.keys())):
|
||||||
if('extended_entities' in status._json.keys()):
|
if('extended_entities' in status._json.keys()):
|
||||||
if('media' in status.extended_entities.keys()):
|
if('media' in status.extended_entities.keys()):
|
||||||
|
archive_filenames = []
|
||||||
for media_dict in status.extended_entities['media']:
|
for media_dict in status.extended_entities['media']:
|
||||||
if(media_dict['type'] == 'photo'):
|
if(media_dict['type'] == 'photo'):
|
||||||
archive_media(media_dict['media_url'])
|
archive_filenames.append(
|
||||||
|
archive_media(media_dict['media_url']))
|
||||||
elif(media_dict['type'] == 'video'):
|
elif(media_dict['type'] == 'video'):
|
||||||
archive_media(
|
archive_filenames.append(archive_media(
|
||||||
media_dict['video_info']['variants'][-1:][0]['url'])
|
media_dict['video_info']['variants'][-1:][0]['url']))
|
||||||
|
return archive_filenames
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
print(e)
|
||||||
|
return ''
|
||||||
|
|
|
@ -1,11 +1,27 @@
|
||||||
#!/usr/bin/python3
|
#!/usr/bin/python3
|
||||||
|
|
||||||
from tweepy import OAuthHandler, API
|
from tweepy import OAuthHandler, API
|
||||||
|
from webdav3.client import Client
|
||||||
|
|
||||||
|
|
||||||
def authenticate_twitter(config):
|
def authenticate_twitter(config):
|
||||||
|
try:
|
||||||
auth = OAuthHandler(config['api_key'], config['api_key_secret'])
|
auth = OAuthHandler(config['api_key'], config['api_key_secret'])
|
||||||
auth.set_access_token(config['access_token'],
|
auth.set_access_token(config['access_token'],
|
||||||
config['access_token_secret'])
|
config['access_token_secret'])
|
||||||
twitter_api = API(auth)
|
twitter_api = API(auth)
|
||||||
return twitter_api
|
return twitter_api
|
||||||
|
except Exception as e:
|
||||||
|
print('There was some error attempting to authenticate with Twitter API')
|
||||||
|
print(f'\t{e}')
|
||||||
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
def authenticate_nextcloud(config):
|
||||||
|
try:
|
||||||
|
client = Client(config)
|
||||||
|
return client
|
||||||
|
except Exception as e:
|
||||||
|
print('There was some error attempting to authenticate through WebDAV')
|
||||||
|
print(f'\t{e}')
|
||||||
|
exit(1)
|
||||||
|
|
48
lib/echo_nextcloud.py
Normal file
48
lib/echo_nextcloud.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
from datetime import datetime as dt
|
||||||
|
from lib.setup import import_config_file
|
||||||
|
from lib.authentication import authenticate_nextcloud
|
||||||
|
|
||||||
|
|
||||||
|
def setup_archive_data(timestamp):
|
||||||
|
config = import_config_file()
|
||||||
|
if(config['nextcloud']['webdav_hostname'] and config['nextcloud']['webdav_login'] and config['nextcloud']['webdav_password']):
|
||||||
|
client = authenticate_nextcloud(config['nextcloud'])
|
||||||
|
else:
|
||||||
|
print(
|
||||||
|
'Please fill out the necessary information for using Nextcloud through WebDAV.')
|
||||||
|
print('\thttps://docs.nextcloud.com/server/16/user_manual/files/access_webdav.html')
|
||||||
|
exit(1)
|
||||||
|
ts = dt.fromtimestamp(int(int(timestamp)/1000))
|
||||||
|
date_ts = ts.strftime('%Y_%m_%d')
|
||||||
|
time_ts = ts.strftime('%H_%M_%S')
|
||||||
|
return client, config['nextcloud_upload_path'], date_ts, time_ts
|
||||||
|
|
||||||
|
|
||||||
|
def setup_archive_dir(client, upload_path, date_ts):
|
||||||
|
if(not client.check(upload_path)):
|
||||||
|
client.mkdir(f'{upload_path}')
|
||||||
|
client.mkdir(f'{upload_path}/{date_ts}')
|
||||||
|
else:
|
||||||
|
if(not client.check(f'{upload_path}/{date_ts}')):
|
||||||
|
client.mkdir(f'{upload_path}/{date_ts}')
|
||||||
|
|
||||||
|
|
||||||
|
def attempt_upload_media(client, upload_dir, upload_filename, upload_filetype, archive_filename):
|
||||||
|
file_dne = True
|
||||||
|
count = 1
|
||||||
|
while(file_dne):
|
||||||
|
temp_filename = f'{upload_dir}/{upload_filename}_{count}.{upload_filetype}'
|
||||||
|
if(not client.check(temp_filename)):
|
||||||
|
client.upload_file(temp_filename, f'{archive_filename}')
|
||||||
|
file_dne = False
|
||||||
|
else:
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
|
||||||
|
def nextcloud_upload_media(archive_filename, timestamp):
|
||||||
|
client, upload_path, date_ts, time_ts = setup_archive_data(timestamp)
|
||||||
|
setup_archive_dir(client, upload_path, date_ts)
|
||||||
|
attempt_upload_media(client, f'{upload_path}/{date_ts}',
|
||||||
|
f'{date_ts}_{time_ts}', f'{archive_filename.split(".")[1]}', f'data/{archive_filename}')
|
|
@ -1,6 +1,22 @@
|
||||||
{
|
{
|
||||||
|
"twitter": {
|
||||||
"api_key":"",
|
"api_key":"",
|
||||||
"api_key_secret":"",
|
"api_key_secret":"",
|
||||||
"access_token":"",
|
"access_token":"",
|
||||||
"access_token_secret":""
|
"access_token_secret":""
|
||||||
|
},
|
||||||
|
"nextcloud": {
|
||||||
|
"webdav_hostname": "",
|
||||||
|
"webdav_login": "",
|
||||||
|
"webdav_password": ""
|
||||||
|
},
|
||||||
|
"nextcloud_upload_path": "",
|
||||||
|
"mastodon": {
|
||||||
|
"api_key": "",
|
||||||
|
"api_key_secret": ""
|
||||||
|
},
|
||||||
|
"discord": {
|
||||||
|
"api_key": "",
|
||||||
|
"api_key_secret": ""
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -8,11 +8,17 @@ from tweepy import Stream
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
config = import_config_file()
|
config = import_config_file()
|
||||||
twitter_api = authenticate_twitter(config)
|
if(config['twitter']['api_key'] and config['twitter']['api_key_secret'] and config['twitter']['access_token'] and config['twitter']['access_token_secret']):
|
||||||
tweet_stream_listener = TweetStreamer(twitter_api)
|
twitter_client = authenticate_twitter(config['twitter'])
|
||||||
tweet_stream = Stream(auth=twitter_api.auth,
|
else:
|
||||||
|
print('Please fill out the necessary information for using the Twitter API.')
|
||||||
|
print('\thttps://developer.twitter.com')
|
||||||
|
exit(1)
|
||||||
|
tweet_stream_listener = TweetStreamer(twitter_client)
|
||||||
|
tweet_stream = Stream(auth=twitter_client.auth,
|
||||||
listener=tweet_stream_listener)
|
listener=tweet_stream_listener)
|
||||||
tweet_stream.filter(follow=[twitter_api.me()._json['id_str']])
|
tweet_stream.filter(
|
||||||
|
follow=[twitter_client.me()._json['id_str']], is_async=True)
|
||||||
|
|
||||||
|
|
||||||
if(__name__ == '__main__'):
|
if(__name__ == '__main__'):
|
||||||
|
|
Reference in a new issue