A python package that fetches tweets and user information in a very pythonic manner.

Overview

Tweetsy

Tweetsy uses Twitter's underlying API to fetch user information and tweets and present it in a human-friendly way. What makes Tweetsy special is that everything is an object here. So it makes it very easier for the end users to store the data however they want, i.e. CSV, HTML or even in cloud database. This will become more clear in the following sections.

Getting user data

This package was originally designed to get user information (i.e. name, number of follower, profile image url) and tweets, given the username of the user. It all starts with the User class of the user module of this package.

>>> # get data from https://twitter.com/DrTedros
>>> from tweetsy.user import User
>>> user = User('DrTedros')
>>> user
<tweetsy.user.User object at 0x0000007B0C5C74F0>
>>> user.name
'Tedros Adhanom Ghebreyesus'

As it can be seen, the when we create an instance of the User object providing the username of a user, we can access relevant data of the user. The full API is given at the end of this page. Here is how we get the profile picture, number of followers of the user and the date when the profile was created.

>>> user.profile_image
{'normal': 'https://pbs.twimg.com/profile_images/1337835478704291842/O1A5QF3x_normal.png', 'medium': 'https://pbs.twimg.com/profile_images/1337835478704291842/O1A5QF3x_200x200.png', 'large': 'https://pbs.twimg.com/profile_images/1337835478704291842/O1A5QF3x_400x400.png'}
>>> user.profile_image['large']
'https://pbs.twimg.com/profile_images/1337835478704291842/O1A5QF3x_400x400.png'
>>> user.follower_count
1573518
>>> user.created_at
datetime.datetime(2010, 9, 12, 13, 9, 27, tzinfo=datetime.timezone.utc)

The created_at attribute returns a python datetime object instead of plain text, which makes it a lot easier to manipulate and use the data for in future.

Getting user tweets

The get_tweets method of the User class fetches the tweets. This method returns a tuple of two elements, the first one is a list of Tweet objects and the later one is cursor, which works as a marker and is used to get the next tweets.

>>> tweets, cursor = user.get_tweets(5)
>>> tweets[0]
<tweetsy.tweet.Tweet object at 0x0000007B0EFFF430>
>>> tweets[0].absolute_url
'https://twitter.com/DrTedros/status/1457104908633640966'
>>> cursor
'HBaAwLmhjM3FuCgAAA=='
>>> next_tweets, next_cursor = user.get_tweets(5, cursor=cursor)

The Tweet Object

The tweet object comes with a lot of attributes.

>>> tweet = tweets[0]
>>> tweet.text
'RT @benphillips76: This photo of Tuvalu's virtual address to the Climate Confer ence says everything that should need to be said.  #COP26 ht.'
>>> tweet.tweet_id
'1457104908633640966'
>>> tweet.source
'Twitter for iPhone'
>>> tweet.family
'retweet'

The family attribute is very crucial. Not all tweets are plain texts or images, some are retweets of another tweet, some are quoted tweets. In this package the retweets and quotes contain their own attributes, which are TweetLink object, which refers to the original parent tweet. A TweetLink instane contains 3 attributes, tweet_id, user_id, username. This is better shown with an example.

>>> tweet.family
'retweet'
>>> tweet.retweet
<tweetsy.tweet.TweetLink object at 0x0000007B0C60FAF0>
>>> tweet.retweet.absolute_url
'https://twitter.com/benphillips76/status/1456629120973017089'

This gives us the tweet by benphillips76, which was retweeted by DrTedros.

The TweetLink contains a method called get_tweet, which can be used to fetch the parent tweet data, i.e. another Tweet instance.

>>> new_tweet = tweet.retweet.get_tweet()
>>> new_tweet
<tweetsy.tweet.Tweet object at 0x0000007B0C5C7550>
>>> new_tweet.retweet_count
8909
>>> new_tweet.media
[{'type': 'photo', 'source': 'https://pbs.twimg.com/media/FDb8W1UXEAAgxiA.jpg', 'url': 'https://twitter.com/benphillips76/status/1456629120973017089/photo/1'}]

So from now on, new_tweet can be treated like any other Tweet object.

Installation

The package hasn't been published in PyPI yet. Since the package is very minimal, one can just simply do this:

C:\Users\USER> git clone https://github.com/binarysakir/tweetsy
C:\Users\USER> cd tweetsy
C:\Users\USER> pip install requirements.txt
C:\Users\USER> python
>>> from tweetsy.user import User

Full description of classes

User

To initialize, provide the username of the Twitter account. Attributes:

  • user_id: User id. The username can change over time but the user id does not.
  • name: Mame of the user profile
  • tweet_count: Total number of tweets of a user.
  • created_at: Time of the account creation. Python datetime object.
  • follower_count: How many accounts are following the user.
  • following_count: How many accounts the user is following.
  • description: Bio of the account.
  • profile_image: Profile image of the user. It's a dict containing the URL of the profile image of 3 different sizes- normal, medium and large.
  • profile_banner: URL of the account banner or the cover picture.
  • media_count: Number of media uploaded by the user.
  • verified: Boolean data type. Methods:
  • get_tweets(count, reply, cursor): count indicates the number tweets to be fetched, default is 20. reply accepts boolean value, default is False. If set to True then it will fetch "Tweets and Replies" section of the profile. cursor is by default None, so it fetches the first count tweets of the profile if not set to anything else. This method returns a tuple of a list of tweets (Tweet objects) and cursor. See usage in above examples. IMPORTANT The get_tweets may or may not return the same number of tweets as count. Generally the number of returned tweets is one more or less than the given argument.
  • serialize(): Returns a dictionary of all the available attributes. It is just an alternative of Python's built-in vars method.

TweetLink

It works as a reference to a tweet without actually fetching the tweet. To initialize, provide the following: tweet_id, user_id, username. Attributes:

  • tweet_id
  • user_id
  • username
  • absolute_url
  • user_absolute_url Methods:
  • get_tweet(): Returns a Tweet object of the tweet. This class can be used to fetch a tweet without using the a User object.

Tweet

This tweet should not be be initialized manually since it takes modified JSON data generated by parse_UserTweetsAndReplies function of parser.py. Attributes:

  • tweet_id
  • user_id
  • username
  • family: It has 6 possible values: vanilla, retweet, reply, quote, poll, null. Vanilla is just a plain original tweet. Retweet, reply and quote indicates their own meaning respectively. The poll family indicates the tweet is a poll. Null indicated the tweet is empty.
  • text: Text of the tweet. Might be truncated in case it's a retweet.
  • lang: Language of the tweet.
  • source: From which device the tweet was posted.
  • favorite_count
  • retweet_count
  • reply_count
  • quote_count
  • created_at
  • media: A dict containing the media URLs of the tweet.
  • hashtags
  • retweet: If the tweet is a retweet, this will be a TweetLink object, otherwise None.
  • quote: If the tweet is a quote, this will be a TweetLink object, otherwise None.
  • poll: If the tweet is a poll, this will be a TweetLink object, otherwise None.
  • replyIf the tweet is a poll, this will be a TweetLink object, otherwise None.
  • absolute_url

It is adviced to fetch the favorite_count, retweet_count, reply_count and the quote_count of a vanilla tweet and not of a quote or retweet tweet.

Bugs

Be sure to report bugs!

TODO

  • Fetch Twitter poll tweet data
  • Fix number of returned tweets problems
  • Get user info from user id

Disclaimer

I wrote this package only for using the knowledge of OOP and API. This package is under MIT license. I am not responsible for any kind of abuse of this package.

Owner
Sakirul Alam
Sakirul Alam
A bot to view Garfield comics directly from Discord and get updates of the comics automatically

Garfield-Bot A bot to view Garfield comics directly from Discord and get updates of the comics automatically. Instructions to use the bot: Invite the

Raghav Sharma 3 Feb 13, 2022
High-Resolution Differential Z-Belt Mod for V0 (with optional Kirigami support)

V0-DBM This is a high-resolution differential pulley system belt mod for the Z-axis on Voron 0 with optional Kirigami Bed support. NOTE: Alpha version

Simon Küppers 11 Jan 07, 2023
Replace sequence_IDs in gff3 based on given genome.fasta

gff-rename Replace the sequence IDs in a gff3 file with a set of provided sequence IDs from a genom.fasta. This is useful when a gff3 file is retrieve

tolkit 1 Nov 12, 2021
The implementation of Learning Instance and Task-Aware Dynamic Kernels for Few Shot Learning

INSTA: Learning Instance and Task-Aware Dynamic Kernels for Few Shot Learning This repository provides the implementation and demo of Learning Instanc

11 Jan 02, 2023
toldium is a modular, fast, reliable and customizable multiplatform bot library for your communities

toldium The easy multiplatform bot toldium is a modular, fast, reliable and customizable multiplatform bot library for your communities, from a commun

Stockdroid Fans 5 Nov 03, 2021
A file-based quote bot written in Python

Let's Write a Python Quote Bot! This repository will get you started with building a quote bot in Python. It's meant to be used along with the Learnin

1 Jan 15, 2022
Georeferencing large amounts of data for free.

Geolocate Georeferencing large amounts of data for free. Special thanks to @brunodepauloalmeida and the whole team for the contributions. How? It's us

Gabriel Gazola Milan 23 Dec 30, 2022
Available slots checker for Spanish Passport

Bot that checks for available slots to make an appointment to issue the Spanish passport at the Uruguayan consulate page

1 Nov 30, 2021
Python 3 tools for interacting with Notion API

NotionDB Python 3 tools for interacting with Notion API: API client Relational database wrapper Installation pip install notiondb API client from noti

Viet Hoang 14 Nov 24, 2022
A free and open-source discord webhook spammer.

Discord-Webhook-Spammer A free and open-source discord webhook spammer. Usage Depending on your python installation your commands may vary. Below are

3 Sep 08, 2021
Matrix trivia bot with python

Matrix-trivia-bot Getting started See SETUP.md for how to setup and run the template project. Project structure A reference of each file included in t

1 Nov 16, 2021
Powerful Telegram userbot to turn your PROFILE PICTURE & LAST NAME into a real time clock & to change your BIO automatically.

DATE_TIME_USERBOT-TeLeTiPs Powerful Telegram userbot to turn your PROFILE PICTURE & LAST NAME into a real time clock & to change your BIO automaticall

53 Jan 05, 2023
Telegram File to Link Fastest Bot , also used for movies streaming

Telegram File Stream Bot ! A Telegram bot to stream files to web. Report a Bug | Request Feature About This Bot This bot will give you stream links fo

Avishkar Patil 194 Jan 07, 2023
Live Weather Updates using Flask and OpenWeather

AuraX Live Weather Updates using Flask and OpenWeather Installation To setup this project on your local machine, first clone this repository and insta

Ayush Gupta 3 Nov 02, 2021
SquireBot is a Discord bot designed to run and manage tournaments entirely within a Discord.

Overview SquireBot is a Discord bot designed to run and manage tournaments entirely within a Discord. The current intended usecase is Magic: the Gathe

7 Nov 29, 2022
Microservice to extract structured information on EVM smart contracts.

Contract Serializer Microservice to extract structured information on EVM smart contract. Why? Modern NFT contracts may have different names for getPr

WeBill.io 8 Dec 19, 2022
自用直播源集合,附带检测与分类功能。

myiptv 自用直播源集合,附带检测与分类功能。 为啥搞 TLDR: 太闲了。 自己有收集直播源的爱好,和录制直播源的需求。 一些软件自带的直播源太过难用。 网上现有的直播源太杂,且缺乏检测。 一些大源缺乏持续更新,如 iptv-org。 使用指南与 TODO 每次进行大更新后都会进行一次 rel

abc1763613206 171 Dec 11, 2022
A discord bot for checking what linked profiles a user has to their Ubisoft account

ubisoft_discord_profiles A Discord bot for checking what linked profiles a user has to their Ubisoft account. This can be setup using an enviromental

Andrei 1 Dec 17, 2021
A taskbar clock for secondary taskbars on Windows 11

ElevenClock A taskbar clock for secondary taskbars on Windows 11. When microsoft's engineers were creating Windows 11, they forgot to add a clock on t

Martí Climent 1.7k Jan 07, 2023
Discord heximals: More colors for your bot

DISCORD-HEXIMALS More colors for your bot ! Support : okimii#0434 COLORS ( 742 )

4 Feb 04, 2022