Skip to main content

Twitter tools

This project contains different tools to help search and analyze in twitter.

The analysis of tweets is often followed by doing the same pipeline for different projects. This toolkit is a compilation and wrapper of many tools to ease the pipeline of analysis in twitter. First of all it provides search utilities, either by searching by a twitter query or by identifier. Then it integrates some models to infer users age, gender, and if it is a person or an organisation. This inference should be used with caution since it is not perfect, but can yield an overview of the type of users analyzed. There is also a location of users inference for Spanish locations based on their location text or description in their Twitter profile. For the text analysis we provide a pipeline for Topic analysis using the LDA algorithm and some sentiment analysis too. Finally we provide a network creation of the tweets and users function for a network analysis.

Credentials

To run this you need to provide your Twitter API credentials in the form of YAML file.

For example:

search_tweets_api:
  endpoint: https://api.twitter.com/2/tweets/search/all
  consumer_key: XXXXXXXXXXXX
  consumer_secret: XXXXXXXXXXXXXXXX
  bearer_token: XXXXXXXXXXXXXXXXX

Searching tweets

You query tweets with search_tweets_by_query.
To have a more detailed for the parameters take a look at the Twitter API

from twitter_tools.search_tools import TweetSearchUtil

tsu = TweetSearchUtil('path_to_yaml_creds')
tweets = tsu.search_tweets_by_query(
    "python OR #python"
    ,tweet_fields="author_id,conversation_id,created_at,id,in_reply_to_user_id,lang,public_metrics,text"
    )

Searching by id

You can also search retrieve tweets and users by their id.

from twitter_tools.search_tools import TweetSearchUtil

tsu = TweetSearchUtil('path_to_yaml_creds')
tweets = tsu.retreive_tweets_by_id(
    ['12341','12342']
    ,tweet_fields="author_id,conversation_id,created_at,id,in_reply_to_user_id,lang,public_metrics,text"
    )

users = tsu.retreive_users_by_id(
    ['4321','4322']
    ,user_fields="created_at,description,id,name,profile_image_url,public_metrics,username"
    )

Twitter Inference

This is a Wrapper of M3Inference but with an ease to use and make a general pipeline with this set of tools.

from twitter_tools.user_inference import TwitterUserInference

users = [{...},...]

tui = TwitterUserInference()

inference = tui.infer_users(users, lang='en')

Users Location

This tool is only available for Spain locations.
To feature other countries, a json in the format as places_spain.json should be added.

This tools checks the location of an user based otheir text location and description when no geolocation is available. Checks for city/country/region words in the user profile to try to identify for its location.

from location.location_detector import LocationDetector

user = {...}

detector = LocationDetector('path_to_places_json')

loc, method = detector.identify_location(user['location'], user['description'])

Topic analysis

This tool will do every step of topic analysis using LDA.

The typical pipeline can be represented by as follows.

from twitter_tools.topic_analysis import TopicAnalysis

tweets = [...]
analyzer = TopicAnalysis(language='es')

tweets_clean = analyser.clean_docs(tweets)
tweets_lemmas = analyser.lemmatize(tweets_clean, 
                                filter_postags=['ADJ', 'ADV', 'NOUN', 'VERB'])
ldamodel, docs_dict = analyzer.topic_analysis(tweets_lemmas,
                                            topics_nb=10, print_words=10)

Sentiment Analysis

Sentiment analysis of text using pretrained models.

from twitter_tools.topic_analysis import TopicAnalysis

tweets = [...]
analyzer = TopicAnalysis(language='es')

sentiments = [analyzer.sentiment_analysis(t) for t in tweets]

Network creation

This tool creates graphs based on the tweets and users interactions.
It can create the user and the tweet graph.

The tweets dict like object must contain at least the following fields: id, retweeted_by, favorited_by.
The users dict like object must contain at least the following fields: id, screen_name.

from twitter_tools.network_tools import create_tweets_network, create_users_network

users = [...]
tweets = [...]

T = create_tweets_network(tweets)
U = create_users_network(users, tweets)

Once the network create you can export it and open the file in Gephi to visualize it and analize it.

import networkx as nx

nx.write_gml(T, "tweets_network.gml")

Metadata

Release files for twitter-toolkit 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for twitter-toolkit 0.1.0
File Size Uploaded Provenance Actions
twitter-toolkit-0.1.0.tar.gz 26.8 kB Without provenance Details for twitter-toolkit-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for twitter-toolkit 0.1.0
File Interpreter ABI Platform Actions
twitter_toolkit-0.1.0-py3-none-any.whl Python 3 none any Details for twitter_toolkit-0.1.0-py3-none-any.whl

Total release size: 53.5 kB

Release files / twitter-toolkit-0.1.0.tar.gz

Download URL twitter-toolkit-0.1.0.tar.gz
Size 26.8 kB
Tags Source
SHA-256 checksum
How to use checksums
e0269723dca373f0f1687ba2885c1e835c726db4a9c66d277db38f328e7dccce
BLAKE2b-256 checksum
How to use checksums
9bdfe397a5302c85dfd79c59b855dadd26f0e20ef690f3f5fb4de2077d760079
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.9 Linux/6.1.8-arch1-1

Release files / twitter_toolkit-0.1.0-py3-none-any.whl

Download URL twitter_toolkit-0.1.0-py3-none-any.whl
Size 26.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bdac6e5b900e03f075cd806713ca07d140b72e594b464d397c465877afd140e2
BLAKE2b-256 checksum
How to use checksums
e9c416be81e9eba75cace7b55990a4b4585c9ee19fa0f08b05dd8e157b57204f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.10.9 Linux/6.1.8-arch1-1

Security for twitter-toolkit 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page