Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9fd4ebd

Browse files
Merge pull request avinashkranjan#669 from Ayushjain2205/Tweet_fetch
Tweet fetch
2 parents acd172c + 466f5d2 commit 9fd4ebd

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

‎Tweet-Fetcher/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Tweet Fetcher
2+
There are 2 scripts in this project-
3+
1. fetcher.py - This script is used to fetch tweets by a user specified hashtags and then store these tweets in a SQL database
4+
2. display.py - This script is used to display the tweets from the database to the terminal
5+
6+
## Setup instructions
7+
In order to run this script, you need to have Python and pip installed on your system. After you're done installing Python and pip, run the following command from your terminal to install the requirements from the same folder (directory) of the project.
8+
```
9+
pip install -r requirements.txt
10+
```
11+
12+
After satisfying all the requirements for the project, Open the terminal in the project folder and run
13+
```
14+
python fetcher.py
15+
python display.py
16+
```
17+
or
18+
```
19+
python3 translator.py
20+
python3 display.py
21+
```
22+
depending upon the python version. Make sure that you are running the command from the same virtual environment in which the required modules are installed.
23+
24+
## Output
25+
![Sample output of fetcher script](https://i.postimg.cc/9QRQNqzd/fetcher.png)
26+
![Sample output of display script](https://i.postimg.cc/C52VjmGW/display.png)
27+
28+
## Author
29+
[Ayush Jain](https://github.com/Ayushjain2205)

‎Tweet-Fetcher/display.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sqlite3
2+
from sqlite3 import Error
3+
4+
# Function to connect to the SQL Database
5+
def sql_connection():
6+
try:
7+
con = sqlite3.connect('tweetsDatabase.db')
8+
return con
9+
except Error:
10+
print(Error)
11+
12+
con = sql_connection()
13+
14+
# Function to Fetch tweets from database
15+
def sql_fetch(con):
16+
searchHashtag=input("\nEnter hashtag whose tweets you want to display :")
17+
isEmptySearch=True
18+
cursorObj = con.cursor()
19+
cursorObj.execute('SELECT * FROM tweets') # SQL search query
20+
rows = cursorObj.fetchall()
21+
22+
print("\n")
23+
24+
for row in rows:
25+
# Check if row has searched HashTag
26+
if(searchHashtag in row):
27+
print(row[1]+'\n')
28+
isEmptySearch=False
29+
30+
if(isEmptySearch):
31+
print("\nNo tweets with #"+searchHashtag+" fetched into database \n")
32+
33+
sql_fetch(con)

‎Tweet-Fetcher/fetcher.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import tweepy
2+
import sqlite3
3+
from sqlite3 import Error
4+
5+
# Set Twitter API KEYS
6+
consumer_key = '<Your key>'
7+
consumer_secret = '<Your key>'
8+
access_key='<Your key>'
9+
access_secret = '<Your key>'
10+
11+
# Initialize Twitter OAuth
12+
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
13+
auth.set_access_token(access_key, access_secret)
14+
api = tweepy.API(auth,wait_on_rate_limit=True)
15+
16+
# Function to connect to the SQL Database
17+
def sql_connection():
18+
try:
19+
con = sqlite3.connect('tweetsDatabase.db')
20+
return con
21+
except Error:
22+
print(Error)
23+
24+
# Function to create table
25+
def sql_table(con):
26+
cursorObj = con.cursor()
27+
cursorObj.execute("CREATE TABLE IF NOT EXISTS tweets(hashtag text, tweet text)")
28+
con.commit()
29+
30+
# Call functions to connect to database and create table
31+
con = sql_connection()
32+
sql_table(con)
33+
34+
# Function to insert into table
35+
def sql_insert(con, entities):
36+
cursorObj = con.cursor()
37+
cursorObj.execute('INSERT INTO tweets(hashtag, tweet) VALUES(?, ?)', entities)
38+
con.commit()
39+
40+
# Take input for hashtag to search and number of tweets to fetch
41+
hashTag = input("\nEnter hashtag to search: ")
42+
numberOfTweets=int(input("How many tweets do you want to fetch? "))
43+
search_words = "#"+hashTag
44+
new_search = search_words + " -filter:retweets"
45+
46+
# Call twitter API and pass above parameters
47+
try:
48+
for tweet in tweepy.Cursor(api.search,q=new_search,count=5,lang="en", since_id=0).items(numberOfTweets):
49+
entities = (hashTag,tweet.text)
50+
# Insert tweet into database
51+
sql_insert(con, entities)
52+
print("Saved successfully in Database")
53+
except Error:
54+
print(Error)

‎Tweet-Fetcher/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tweepy

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /