Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
BEARER_TOKEN=
2 changes: 2 additions & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.venv
14 changes: 14 additions & 0 deletions backend/Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[source]]
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"

[packages]
tweepy = "*"
python-dotenv = "*"

[dev-packages]
autopep8 = "*"

[requires]
python_version = "3.10"
118 changes: 118 additions & 0 deletions backend/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# CelebrityWall

### 1) Setup the project

install pipenv
```bash
pip install pipenv
```

install python packages

```bash
pipenv install --dev
```

### 2) Set requirement variables
- Create `.env` same as `.env.example`

- Change `username` in `main.py` (Note: until we have list of Celebrities)

### 3) run the script

```bash
pipenv shell
python main.py
```

### 3) More
The python Library we use to call twitter API: https://github.com/tweepy/tweepy
Empty file added backend/__init__.py
Empty file.
Empty file added backend/helpers/__init__.py
Empty file.
48 changes: 48 additions & 0 deletions backend/helpers/user_tweets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

from datetime import datetime, timezone
import tweepy

WANTED_HASHTAGS = ["#mahsaamini", "#مهسا_امینی", "#opiran"]


def get_user_tweets(client: tweepy.Client, user_name: str, start_date: datetime.astimezone, end_date: datetime.astimezone):
"""Get User's Tweets
This endpoint/method returns Tweets composed by a single user,
specified by the requested user ID

:param user_name
:return: [user tweets]
"""

# Get user by username
user = client.get_user(username=user_name)
if not user.data:
# if there is no user, return None
return None
user = user.data

# Get user tweets
# TODO: find a way to filter by hashtag here
users_tweets = client.get_users_tweets(
id=user.id,
end_time=start_date,
start_time=end_date
)
return users_tweets.data


def get_user_tweets_by_hashtag(client: tweepy.Client, user_name: str, start_date: datetime.astimezone, end_date: datetime.astimezone):
"""Get User's tweets which has the hashtag

:param user_name
:return: [Tweet ID]
"""
# call the get_user_tweets function
users_tweets = get_user_tweets(client, user_name, start_date, end_date)
if users_tweets:
tweets_with_hashtag = []
for tweet in users_tweets:
# Check if one of hashtags is on the tweet
if any([wanted_hashtag in tweet.text.lower() for wanted_hashtag in WANTED_HASHTAGS]):
tweets_with_hashtag.append(tweet.id)
return tweets_with_hashtag
24 changes: 24 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import tweepy
from dotenv import dotenv_values
from helpers.user_tweets import get_user_tweets_by_hashtag
from datetime import datetime

# Get the .env values
config = dotenv_values(".env")

bearer_token = config.get("BEARER_TOKEN")
# access_token = config.get("API_KEY")
# access_token_secret = config.get("API_KEY_SECRET")

start_date = datetime.now().astimezone()
end_date = datetime(2022, 9, 16, 0, 0, 0, 0).astimezone()

# set-up tweepy Client
client = tweepy.Client(bearer_token)


# Get user Tweets
# TODO: Loop all the users
username = "FoadHaydri"
tweet_ids = get_user_tweets_by_hashtag(client, username, start_date, end_date)
print(tweet_ids)