Grabbing followers of followers like a chain from Twitter - python

Suppose I have a example Twitter account whose username is #testaccount and I want to grab the followers of the account #testaccount and with that also want to grab the followers of the users who follow #testaccount.
My problem is I don't know how to store this and map this into a database or into dict/list. If yes then how can I do it? Because I have to work with all data including the followers of followers.
For just an example say I have some data that looks like:
I want to store all these data in a way so that I can access each of them under each followers.

I would make a custom class, TwitterAccount:
class TwitterAccount:
def __init__(self, name):
this.name = name
this.followers = []
def addFollowers(self, followers):
this.followers.extend(followers)
# Will return a list of the followers.
def getFollowers(self):
return this.followers
# more functionality can be added
And you can use it like this:
acc0 = TwitterAccount("BradPitt")
acc1 = TwitterAccount("BradPittFan1")
acc2 = TwitterAccount("BradPittFan2")
acc3 = TwitterAccount("BradPittFan1Mother")
acc0.addFollowers([acc1,acc2])
acc1.addFollowers([acc3])
# more code
More on classes:
A nice tutorial and intro
Official docs

Miguel Grinberg has a very good tutorial on flask, where he also goes into detail on the follower/followed structure using SQL databases: https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-viii-followers
His solution is construct two tables, a user table which contains the user ID and posts, etc., and a auxiliary table, which maps user IDs (followed) to other user IDs (followers):
Image from https://blog.miguelgrinberg.com/static/images/mega-tutorial/ch08-followers-schema.png

Related

Is there a way to search user/screen names in tweepy (or any other API package) using partial names like using contains or %?

So I am able to lookup users using the below command...
Search = 'Amazon'
users = api.search_users(q = Search)
results = []
for user in users:
name = user.name
screen_name= user.screen_name
results.append([name, screen_name])
results = pd.DataFrame(results)
results.columns = ['name', 'screen_name']
results
...and I was wonder if there was a way to use some form of contains/islike/% lookup when I only know part of the name. So for instance. If I was looking for Amazon, could I do something where I state theat
api.search_users(q is like 'Amaz%')
Furthermore, I believe that the search_users function is looking up by the screen name. Is there a function that looks it up by the user name instead?
There is no Twitter API function for this (wildcard or contains lookup, or user name instead of screen name), so there is no way for Tweepy or other libraries to offer the functionality.
We are as persian and other languages users cant use it cuz we just cant show our exact name inside an english alphabet user_id and we allways use and search people with their unicode display names.

How use Flask LinkCol on database query data results?

I have been working with Flask for a little while and developed a database manager application for managing internal data for the department I work in (I am not a "real" developer but have learned by doing/necessity). The application works great as a basic CRUD app. In it I have used Flask forms and tables including the LinkCol column for linking to different functions within the app. In those cases, I am using an 'Edit' or 'Delete' clickable link; meaning every column in the table has an extra cell with that word. Clicking either of those takes the user to the corresponding page where they can take appropriate action on that item.
Currently I am trying to use LinkCol so that data returned from the database can be the clickable link that links to another function rather than every table having an additional column (for example, "Go to Account"). So, when my table populates with an account number, the user can click the actual account number in the table to go to a different page to view additional data about that specific account. I read the docs and looked at the examples on the creators page regarding overwriting things in the classes but haven't been able to figure it out. I have written a few of my own classes, but am not an expert on the matter by any means. I also have been unable to find anything else that might help including on SO which is always my go to resource for figuring things out.
Here is my table definition using a LinkCol that renders incorrectly and puts 'Account #' in each cell instead of the dynamic data I would like. Changing to Col gives correct display but I want it to be clickable:
class AccountsResults(MainTable):
AccNo = LinkCol('Account #','dbhome_bp.route.account_detail', url_kwargs=
dict(AccNo='AccNo',StatusCode='Active',ServiceAddress1=\
'ServiceAddress1'))
TnCount = Col('Total TNs')
ServiceAddress1 = Col('Service Address')
ServiceCity = Col('Service City')
ServiceZip = Col('Service Zip Code')
MasterServiceDate = Col('Master Service Date')
Active = Col('Currently Active')
Here is an example of what I know I can do versus what I would like to do.
Can do ('Go To Account' is clickable in the Go To Account column):
Go To Account
Account #
Service Address
Etc.
Go To Account
12345678
1234 Anywhere
Yup
Go To Account
12345679
5678 Somewhere
Nah
Would like to do (Account numbers are clickable in Account # column ):
Account #
Service Address
Etc.
12345678
1234 Anywhere
Yup
12345679
5678 Somewhere
Nah
Does anyone know how to make the dynamic data returned from a query the clickable link for a LinkCol in Flask Table?
Any help is greatly appreciated.
Assuming an Sqlalchemy model Account with PK column id and a route defined as follows:
accounts = Blueprint('accounts', __name__)
#accounts.route("/accounts/<int:account_id>")
def detail(self, account_id):
_account = Account.get_or_404(account_id)
# show account details
return render('account-details.html', account=_account)
Define your account link column as follows:
account = LinkCol(
name='Account #',
attr='id',
endpoint='accounts.detail',
url_kwargs=dict(account_id='id'),
)
The parameter meanings are as follows:
name # the column header text
attr # the name of the attribute of the account object to render as the text in the <td> cell
endpoint # the endpoint to link to
url_kwargs # the arguments that get passed to the url_for function along with the endpoint
The links would be constructed as if you had done a url_for for each row in the table as follows:
_row_account_detail_url = url_for('accounts.detail', account_id=row_id)
Example Table definition:
class AccountTable(Table):
account = LinkCol(
name='Account #',
attr='id',
endpoint='accounts.detail',
url_kwargs=dict(account_id='id'),
)
service_address = Col(
'Service Address',
attr='service_address'
)
# other columns

Facebook SDK for Python - getting all page likes of a Facebook profile

Using an access token from the Facebook Graph API Explorer (https://developers.facebook.com/tools/explorer), with access scope which includes user likes, I am using the following code to try to get all the likes of a user profile:
myfbgraph = facebook.GraphAPI(token)
mylikes = myfbgraph.get_connections(id="me", connection_name="likes")['data']
for like in mylikes:
print like['name'], like['category']
...
However this is always giving me only 25 likes, whereas I know that the profile I'm using has 42 likes. Is there some innate limit operating here, or what's the problem in getting ALL the page likes of a user profile?
Per the Graph documention:
When you make an API request to a node or edge, you will usually not
receive all of the results of that request in a single response. This
is because some responses could contain thousands and thousands of
objects, and so most responses are paginated by default.
https://developers.facebook.com/docs/graph-api/using-graph-api/v2.2#paging
Well, this appears to work (a method, which accepts a user's facebook graph):
def get_myfacebook_likes(myfacebook_graph):
myfacebook_likes = []
myfacebook_likes_info = myfacebook_graph.get_connections("me", "likes")
while myfacebook_likes_info['data']:
for like in myfacebook_likes_info['data']:
myfacebook_likes.append(like)
if 'next' in myfacebook_likes_info['paging'].keys():
myfacebook_likes_info = requests.get(myfacebook_likes_info['paging']['next']).json()
else:
break
return myfacebook_likes
The above answers will work, but pretty slowly for anything with many likes. If you just want the count for number of likes, you can get it much more efficiently with total_likes:
myfacebook_likes_info = graph.get_connections(post['id'], 'likes?summary=1')
print myfacebook_likes_info["summary"]["total_count"]

Random page of followers of a Twitter user

Consider a Twitter user who has 1 million followers. I want to collect a random page of followers of this user. Is there any way to do this? I don't want to get the list of all followers because it would exhaust my tokens.
I'm looking for something like this:
follower_ids = api.followers_ids(user_id, page=page_index)
where page_index is a random page.
Thanks.
From the documentation, you can get the amount of followers from a user, e.g. twitterdev:
https://api.twitter.com/1.1/users/show.json?screen_name=twitterdev
This returns, amongst other things, followers_count:
{
...
"followers_count": 143916,
...
}
From there you can work out how which pages the random followers are on.
Just to be clear, I've never used twitter, or the API, and have just searched for you.
you can select for example 20 ids from all ids like this:
SCREEN_NAME= "the_user_name"
followersIds = api.followers_ids(SCREEN_NAME)
print followersIds[:20]

Number of entities returned from a GQL Query

I'm new to Python and currently working on my first Google App Engine application. In my program I have an 'Inbox' database model which contains string proporties like to_user, from_user, title, content, etc. When a user logs in to my app I want to be able to count the number of messages that were sent to him/her, this way I can display it as "New Messages(x)". I feel like I currently am using a work around because I can't find a better way.
user = users.get_current_user()
inbox = Inbox.gql('WHERE to_user = :to_user', to_user=user.nickname())
count = 0
for note in inbox:
count = count+1
I tried using len(inbox) but that gave me an error. Thanks for your time.
In your specific case, where the number of New Messages will probably be small, I would not bother to create a counter upfront as suggested here.
I would go with a simpler solution using count() function:
user = users.get_current_user()
inbox = Inbox.gql('WHERE to_user = :to_user', to_user=user.nickname())
count = inbox.count()

Categories

Resources