Replace mastodon.py dependency with requests calls
This commit is contained in:
parent
179c904733
commit
9d6d82bd2f
62
lgfbot.py
62
lgfbot.py
|
@ -1,33 +1,59 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import random
|
import random
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
from mastodon import Mastodon
|
|
||||||
import os
|
import os
|
||||||
|
from urllib.parse import urlencode
|
||||||
|
|
||||||
if not os.path.exists('./.lgfbot/'):
|
#Ensure Credentials
|
||||||
os.mkdir('./.lgfbot')
|
parent = os.path.dirname(os.path.realpath(__file__))
|
||||||
|
try:
|
||||||
|
os.mkdir(os.path.join(parent, '.creds'))
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
if not os.path.exists('./.lgfbot/lgf_client.secret'):
|
|
||||||
|
#Create app
|
||||||
|
try:
|
||||||
|
with open(os.path.join(parent, '.creds', 'client.secret'), "x") as clientcreds:
|
||||||
instance = input('Please enter your instance: ')
|
instance = input('Please enter your instance: ')
|
||||||
if instance[:4] != 'http':
|
if not instance[:4] == 'http':
|
||||||
instance = 'https://' + instance
|
instance = 'https://' + instance
|
||||||
Mastodon.create_app('Hourly lgf', api_base_url = instance, to_file = './.lgfbot/lgf_client.secret')
|
response = requests.post(instance + '/api/v1/apps', data={'client_name':'Hourly lgf','scopes':'write:statuses', 'redirect_uris':'urn:ietf:wg:oauth:2.0:oob'})
|
||||||
|
response = response.json()
|
||||||
|
print(response)
|
||||||
|
clientcreds.write(response['client_id'] + "\n")
|
||||||
|
clientcreds.write(response['client_secret'] + "\n")
|
||||||
|
clientcreds.write(instance + "\n")
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
mastodon = Mastodon(
|
#Fetch information from app credential file
|
||||||
client_id = './.lgfbot/lgf_client.secret',
|
with open(os.path.join(parent, '.creds', 'client.secret'), "r") as clientcreds:
|
||||||
)
|
lines = clientcreds.read().split('\n')
|
||||||
|
client_id = lines[0]
|
||||||
|
client_secret = lines[1]
|
||||||
|
instance = lines[2]
|
||||||
|
|
||||||
if not os.path.exists('./.lgfbot/lgf_user.secret'):
|
#Log in to bot account
|
||||||
print('To post, lgfbot needs to generate an access token')
|
try:
|
||||||
username = input('Enter your username: ')
|
with open(os.path.join(parent, '.creds', 'user.secret'), "x") as usercreds:
|
||||||
password = getpass('Enter your password: ')
|
print(instance + '/oauth/authorize?', urlencode({'response_type':'code', 'client_id':client_id, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':'write:statuses'}))
|
||||||
mastodon.log_in(username=username, password=password, scopes=['write:statuses'], to_file='./.lgfbot/lgf_user.secret')
|
code = input("To generate a token to access your account, lgfbot needs an authorization code. Please authorize using the link above and enter the code it provides you \nCode: ")
|
||||||
|
response = requests.post(instance + '/oauth/token', data={'grant_type':'authorization_code', 'code':code, 'client_id':client_id, 'client_secret':client_secret, 'redirect_uri':'urn:ietf:wg:oauth:2.0:oob', 'scope':'write:statuses'})
|
||||||
|
usercreds.write(response.json()['access_token'])
|
||||||
|
except FileExistsError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
#Fetch information from user credential file
|
||||||
|
with open(os.path.join(parent, '.creds', 'user.secret'), "r") as usercreds:
|
||||||
|
lines = usercreds.read().split('\n')
|
||||||
|
token = lines[0]
|
||||||
|
|
||||||
mastodon = Mastodon(
|
session = requests.Session()
|
||||||
access_token = './.lgfbot/lgf_user.secret'
|
session.headers.update({"Authorization" : "Bearer " + token})
|
||||||
)
|
|
||||||
|
|
||||||
with open('adjectives.txt') as f:
|
with open('adjectives.txt') as f:
|
||||||
adjectives = list(map(str.rstrip, f))
|
adjectives = list(map(str.rstrip, f))
|
||||||
|
@ -55,4 +81,4 @@ else:
|
||||||
adjective = random.choice(adjectives)
|
adjective = random.choice(adjectives)
|
||||||
content = postform.format(adjective=adjective, gender=gender, verb=verb)
|
content = postform.format(adjective=adjective, gender=gender, verb=verb)
|
||||||
print(content)
|
print(content)
|
||||||
mastodon.status_post(content, visibility='unlisted')
|
session.post(instance + '/api/v1/statuses', data={'status':content, 'visibility':'direct'})
|
||||||
|
|
Loading…
Reference in New Issue