#Shivam Kapadia
import tweepy
import pandas as pd
from sklearn.preprocessing import LabelBinarizer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.cross_validation import train_test_split
from sklearn.svm import SVC
from sklearn.model_selection import cross_val_score
import numpy as np
API_KEY=""
API_SECRET=""
auth=tweepy.AppAuthHandler(API_KEY,API_SECRET)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
cnn = tweepy.Cursor(api.user_timeline, id="CNN",tweet_mode="extended")
fox = tweepy.Cursor(api.user_timeline, id="FOXNEWS",tweet_mode="extended")
tweet_CNN=[]
tweet_FOX=[]
for tweet in cnn.items(500):
    tweet_CNN.append([tweet.full_text, tweet.user.screen_name])
for tweet in fox.items(500):
    tweet_FOX.append([tweet.full_text, tweet.user.screen_name])
tweets=tweet_CNN+tweet_FOX
df = pd.DataFrame(tweets, columns=["Text", "User"])
lb = LabelBinarizer()
User_Fitted = lb.fit_transform(df['User']) #0 is Cnn, 1 is FoxNews
new = pd.DataFrame(User_Fitted, columns=['Sources'], index=df.index)
df=df.join(new)
df = df.drop('User', 1)
dfcsv=df.to_csv('CNNvsFOXNEWS_final.csv',index=False, sep=',', encoding='utf-8')
y=df["Sources"]
textsplit=df['Text']
tm = TfidfVectorizer(lowercase=True, stop_words="english",max_df=0.70, min_df=1)
X = tm.fit_transform(textsplit)
X = X.toarray()
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.20)
clf = SVC(kernel="linear")
clf.fit(X_train, y_train)
y_pred = clf.predict(X_test)
cv_scores = cross_val_score(clf,X_train, y_train, cv=5)
print(np.mean(cv_scores))