GithubHelp home page GithubHelp logo

kjaymiller / az-queue-tweeter Goto Github PK

View Code? Open in Web Editor NEW
1.0 3.0 1.0 105 KB

Send Tweets via Azure Storage Queues

Home Page: https://pypi.org/project/azqueuetweeter/

License: MIT License

Python 100.00%
azure azure-storage python twitter hacktoberfest

az-queue-tweeter's Introduction

AZ-Queue-Tweeter

Send images to Twitter using text from Azure Queue Storage

Installation

Install via PyPI pip install azqueuetweeter

Usage instructions

Setup Azure authentication:

First, as a general rule, you should not store credentials in your code; a better option is to store them in environment variables and retrieve them with os.environ.get('ENV_NAME').

Here's what you'll need for Azure storage authentication:

from azqueuetweeter import storage

sa = storage.Auth(connection_string="CONNECTION-STRING", queue_name="YOUR-QUEUE-NAME")

The connection_string comes from the Azure portal. The queue_name is whatever your named your queue. You can either create that programmatically using the Azure client library, or more easily, using the Azure Storage Explorer app.

Setup Twitter authentication:

You'll need a few more details for Twitter authentication:

from azqueuetweeter import twitter

ta = twitter.Auth(
    consumer_key='CONSUMER-KEY',
    consumer_secret='CONSUMER-SECRET',
    access_token='ACCOUNT-ACCESS-TOKEN',
    access_token_secret='ACCOUNT-ACCESS-TOKEN-SECRET')

The consumer_key is also known as the API key and is provided to you in the Twitter developer portal. Similarly, the consumer_secret is also known as the API secret and is provided in the same place.

The access_token and access_token_secret credentials are for the Twitter account that will actually be the tweet author. If you're sending from the same account as the one that signed up for Twitter API access, then you can get those strings from the Twitter developer portal.If you're sending from a different account, you will need to do 3-legged OAuth to get that account's credentials.

To do 3-legged OAuth, first complete the User authentication set up on the app settings page in the Twitter developer portal. Then use the tweepy package to programmatically go through the flow.

First, get an authorization URL for your app:

>>> oauth1_user_handler = tweepy.OAuth1UserHandler(
    "CONSUMER-KEY", "CONSUMER-SECRET",
    callback="http://pamelafox.github.io"
)
>>> print(oauth1_user_handler.get_authorization_url())
https://api.twitter.com/oauth/authorize?oauth_token=OAUTH-TOKEN

Then visit that URL using the desired account for tweeting, confirm authorization of the app, and see the app redirect to a URL like:

https://registeredwebsite.com/?oauth_token=OAUTH-TOKEN&oauth_verifier=OAUTH_VERIFIER

Put the OAUTH-VERIFIER value into the next call:

access_token, access_token_secret = oauth1_user_handler.get_access_token(
    "OAUTH-VERIFIER"
)

Now you have the access_token and access_token_secret needed for the twitter.Auth constructor above.

Construct a Queue Tweeter

Construct a QueueTweeter using the authentication objects:

from azqueuetweeter import QueueTweeter
qt = QueueTweeter(storage_auth=sa, twitter_auth=ta)

Queue up messages

You can add messages to the Queue either manually with the Azure Storage Explorer app or programmatically using the QueueTweeter.queue_message method.

To queue up a message with a simple string:

qt.queue_message('Hello world!')

You might also find it useful to queue up stringified JSON:

import json
qt.queue_message(json.dumps({"text": "Whats your fav Python web framework?", "poll_options": ["Flask", "Django", "FastAPI", "All of em!"], "poll_duration_minutes": 60*24}))

Later, you can transform your queued messages into tweet content, so you can store the information in the queue however works for you.

Send messages as tweets

Now you can send tweets using the QueueTweeter.send_next_message method.

If the queued message contains exactly the text that you want tweeted, then you can call it with no arguments:

qt.send_next_message()

To confirm the tweet contents first, you can specify preview_mode=True :

qt.send_next_message(preview_mode=True)

To transform the queued message content first, specify a message_transformer function that returns back a dict with the same arguments as tweepy's create_tweet function. The most important argument is "text" but many other arguments are also possible.

This example adds a hashtag to the queued message string:

qt.send_next_message(message_transformer=lambda msg: {"text": msg + " #Python"})

This example deserializes a serialized JSON string message:

import json

qt.queue_message(json.dumps({"text": "Whats your fav Python web framework?", "poll_options": ["Flask", "Django", "FastAPI", "All of em!"], "poll_duration_minutes": 60*24}))
qt.send_next_message(message_transformer=lambda msg: json.loads(msg))

It's also possible to upload an image to the tweet, as long as your Twitter account is approved for "elevated access". Your message_transformer must return the image bytes in a "file" key and provide a filename (with an extension) in the "filename" key..


import io
from PIL import Image

img = Image.open("Python_logo_icon.png")
img_bytes = io.BytesIO()
img.save(img_bytes, format="PNG")
img_bytes.seek(0)

qt.queue_message("I want a stuffed Python logo!")
qt.send_next_message(preview_mode=False, message_transformer=lambda msg: {"text": msg, "filename": "python.jpg", "file": img_bytes})

Development guide

Start a virtual environment:

python3 -m venv .venv
source .venv/bin/activate

Install poetry:

python -m pip install poetry

Install project dependencies:

poetry install

az-queue-tweeter's People

Contributors

kjaymiller avatar pamelafox avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

Forkers

pamelafox

az-queue-tweeter's Issues

Make pip package

I think it'd be good to have a structure like:

  • queuetweeter
    • __init__.py (with QueueTweeter class)
    • storage.py
    • twitter.py

Then people could use like:

from queuetweeter import QueueTweeter, storage, twitter

qt = QueueTweeter(storage.Auth(..), twitter.Auth(..))

It's probably best if you do the packaging so its under your account.

how much tweepy auth should we be responsible for

currently twitter.py has both auth and client. It's important to note in the currently implementation of the Twitter API - you will need both is loading an image but only client if you are just sending a text tweet.

Should this project be responsible for the auth points and pass them into tweepy or should we enforce the user adding tweepy and their auth + client to the project?

Possible Solutions

Make Auth a class that can output to both the client and auth as needed.

# Authenticate to Twitter
access_token = os.environ.get("TWITTER_ACCESS_TOKEN")
access_token_secret = os.environ.get("TWITTER_ACCESS_SECRET")
consumer_key = os.environ.get("TWITTER_CONSUMER_KEY")
consumer_secret = os.environ.get("TWITTER_CONSUMER_SECRET")
auth = tweepy.OAuth1UserHandler(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret,
)
api = tweepy.API(auth)
# Create API 2.0 object
client = tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret,
)

Test with Python 3.9

I know that the new Azure functions model is coming soon but until it is out. The project will need to be compatible with python 3.9.

  • Test against 3.9, 3.10 with tox
  • Update pyproject.toml

Should `storage.py` be called `queue.py`

Currently the option for imports in storage.py is just connection_string and load_queue.

If the file were named queue.py then the import would signify the limited scope of the function.

from queue import connection_string, load # load was previously load_queue

Adds auto queue creation if the queue doesn't exist

Azure Storage queue SDK will fail to create the message if the queue doesn't exist. We can pass a

Currently we let the SDK create the message in its normal flow

def queue_message(self, message):
    self.queue.send_message(message)

We should create a check to ensure the queue exists and create it if it does.

from azure.core.exceptions import ResourceExistsError

def queue_message(self, message):
    try:
        queue.create_queue()
    
    except ResourceExistsError:
        pass
       
    queue.send_message(message)

Replace Poetry with Setuptools

Poetry is fine but is another dependency and point of pause for beginners.

We can use setuptools and a default. This moves us to the traditional venv + pip workflow that is still most common.

test that custom objects are passed over defaults

The logic as currently built checks for the lack of a custom object instead of the assumption that a default is provided. This is to ensure that you can provide a custom object that will take priority over a default one.

I'm not sure how to test this logic. I need to check the client object that is calling Client.create_tweet

https://github.com/kjaymiller/Az-Queue-Tweeter/blob/ee05ef87782fc5777df7118568dd1b76fce1b5f6/tests/test_twitter.py#L79-L82

Write tests

Test files already made, nothing in them.

I suggest using pytest to write them. Can use coverage as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.