2018-09-06 15:15:08 +02:00
|
|
|
import helpers
|
|
|
|
import requests
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
def getTweets():
|
|
|
|
""" Get list of tweets, with tweet ID and content, from configured Twitter account URL.
|
|
|
|
|
|
|
|
This function relies on BeautifulSoup to extract the tweet IDs and content of all tweets on the specified page.
|
|
|
|
|
|
|
|
The data is returned as a list of dictionaries that can be used by other functions.
|
|
|
|
"""
|
|
|
|
|
|
|
|
all_tweets = []
|
|
|
|
|
|
|
|
url = helpers._config('tweets.source_account_url')
|
|
|
|
|
|
|
|
if not url:
|
|
|
|
|
|
|
|
helpers._error('getTweets() => The source Twitter account URL (' + url + ') was incorrect. Could not retrieve tweets.')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
headers = {}
|
|
|
|
headers['accept-language'] = 'en-US,en;q=0.9'
|
|
|
|
headers['dnt'] = '1'
|
|
|
|
headers['user-agent'] = helpers._config('gen.APP_NAME')
|
|
|
|
|
|
|
|
data = requests.get(url)
|
|
|
|
|
|
|
|
html = BeautifulSoup(data.text, 'html.parser')
|
|
|
|
|
|
|
|
timeline = html.select('#timeline li.stream-item')
|
|
|
|
|
|
|
|
if timeline is None:
|
|
|
|
|
|
|
|
helpers._error('getTweets() => Could not retrieve tweets from the page. Please make sure the source Twitter account URL (' + url + ') is correct.')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2018-09-06 19:28:49 +02:00
|
|
|
helpers._info('getTweets() => Fetched tweets for ' + url + '.')
|
2018-09-06 15:15:08 +02:00
|
|
|
|
|
|
|
for tweet in timeline:
|
|
|
|
|
|
|
|
tweet_id = tweet['data-item-id']
|
2019-04-30 18:02:00 +02:00
|
|
|
|
|
|
|
try:
|
2018-09-06 15:15:08 +02:00
|
|
|
|
2019-04-30 18:02:00 +02:00
|
|
|
tweet_text = tweet.select('p.tweet-text')[0].get_text()
|
|
|
|
|
|
|
|
except:
|
|
|
|
|
|
|
|
helpers._info('getTweets() => No tweet text found. Moving on...')
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
2018-09-06 15:15:08 +02:00
|
|
|
all_tweets.append({"id": tweet_id, "text": tweet_text})
|
|
|
|
|
|
|
|
return all_tweets if len(all_tweets) > 0 else None
|
|
|
|
|
|
|
|
|
|
|
|
def tootTheTweet(tweet):
|
|
|
|
""" Receieve a dictionary containing Tweet ID and text... and TOOT!
|
|
|
|
|
|
|
|
This function relies on the requests library to post the content to your Mastodon account (human or bot).
|
|
|
|
|
|
|
|
A boolean success status is returned.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
tweet {dictionary} -- Dictionary containing the "id" and "text" of a single tweet.
|
|
|
|
"""
|
|
|
|
|
|
|
|
host_instance = helpers._config('toots.host_instance')
|
|
|
|
token = helpers._config('toots.app_secure_token')
|
|
|
|
|
2018-09-06 19:27:41 +02:00
|
|
|
tweet_id = tweet['id']
|
|
|
|
|
2018-09-06 15:15:08 +02:00
|
|
|
if not host_instance:
|
|
|
|
|
|
|
|
helpers._error('tootTheTweet() => Your host Mastodon instance URL (' + host_instance + ') was incorrect.')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not token:
|
|
|
|
|
|
|
|
helpers._error('tootTheTweet() => Your Mastodon access token was incorrect.')
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
headers = {}
|
|
|
|
headers['Authorization'] = 'Bearer ' + token
|
2018-09-06 19:27:41 +02:00
|
|
|
headers['Idempotency-Key'] = tweet_id
|
2018-09-06 15:15:08 +02:00
|
|
|
|
|
|
|
data = {}
|
|
|
|
data['status'] = tweet['text']
|
|
|
|
data['visibility'] = 'public'
|
|
|
|
|
|
|
|
tweet_check_file_path = helpers._config('toots.cache_path') + tweet['id']
|
|
|
|
tweet_check_file = Path(tweet_check_file_path)
|
|
|
|
if tweet_check_file.is_file():
|
|
|
|
|
2018-10-07 13:23:36 +02:00
|
|
|
helpers._info('tootTheTweet() => Tweet ' + tweet_id + ' was already posted. Reposting...')
|
2018-09-06 15:15:08 +02:00
|
|
|
|
2019-02-28 08:59:08 +01:00
|
|
|
return False
|
|
|
|
|
2018-09-06 15:15:08 +02:00
|
|
|
else:
|
|
|
|
|
2019-05-10 19:13:24 +02:00
|
|
|
tweet['text'].encoding('utf-8')
|
|
|
|
|
|
|
|
tweet_check = open(tweet_check_file_path, mode='w')
|
2018-09-06 15:15:08 +02:00
|
|
|
tweet_check.write(tweet['text'])
|
|
|
|
tweet_check.close()
|
|
|
|
|
2019-02-28 08:59:08 +01:00
|
|
|
helpers._info('tootTheTweet() => New tweet ' + tweet_id + ' => "' + tweet['text'] + '".')
|
2018-09-06 15:15:08 +02:00
|
|
|
|
|
|
|
response = requests.post(
|
|
|
|
url=host_instance + '/api/v1/statuses', data=data, headers=headers)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
|
2018-09-06 19:27:41 +02:00
|
|
|
helpers._info('tootTheTweet() => OK. Posted tweet ' + tweet_id + 'to Mastodon.')
|
2018-09-06 15:15:08 +02:00
|
|
|
helpers._info('tootTheTweet() => Response: ' + response.text)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
2018-09-06 19:27:41 +02:00
|
|
|
helpers._info('tootTheTweet() => FAIL. Could not post tweet ' + tweet_id + 'to Mastodon.')
|
2018-09-06 15:15:08 +02:00
|
|
|
helpers._info('tootTheTweet() => Response: ' + response.text)
|
|
|
|
|
|
|
|
return False
|