firebasePush

This demonstrates how to write to Firebase using the pyrebase library.

Insert %run 'firebasePush.ipynb' after your import statement. s After that, all you have to do is call sendToFirebase to upload the article data to Firebase.

firebasePush(title, url, authors, date, summary, polarity, subjectivity, keywords, images, videos, text)


In [1]:
# -*- coding: utf-8 -*-

#NLP
from pattern.web import Twitter
from textblob import TextBlob
import nltk.data
from nltk.tokenize import word_tokenize, sent_tokenize

#NLTK RESOURCE DOWNLOADING
try:
    nltk.data.find('tokenizers/punkt')
except LookupError:
    nltk.download('punkt')

# set tokenizer model
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')

# insults package
from insults import Insults

Insults.load_model()

In [2]:
import pyrebase

config = {
  "apiKey": "AIzaSyA-chO6lzsMUvp0f1eyY2eQ910OBMXhkg8",
  "authDomain": "fakenewsmustdie.firebaseapp.com",
  "databaseURL": "https://fakenewsmustdie.firebaseio.com",
  "storageBucket": "fakenewsmustdie.appspot.com"
}

Connect to the database

Log in to Firebase with our credentials. The fake-looking credentials are working credentials. Non-authenticated users cannot read or write data. This function must be executed before firebasePush().


In [3]:
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
uid = "tonghuikang@outlook.com"
password = "thk123"
user = auth.sign_in_with_email_and_password(uid, password)
    
db = firebase.database() # reference to the database service
    
def firebaseRefresh():
    global user
    user = auth.refresh(user['refreshToken'])

Grab and evaluate insult level

and then to push to database later


In [4]:
articles_of_a_newspaper = db.child("articles/statestimesreviewcom").get()
articles = articles_of_a_newspaper.val()

# data = articles.items()[0][1]
# # articles.items()[0][0] is the title of the article
# # articles.items()[0][1] is the data of the article
# print(data["polarity"])

import unidecode
import numpy as np

for article_no in range(min(len(articles),5)): # limited to 5
# for article_no in range(len(articles)):
    data = articles.items()[article_no][1]
    text = data["article"]
    
    texty = unidecode.unidecode(text)
#     texty = text.decode('utf-8','ignore').encode("utf-8")
    
    sentence_array = tokenizer.tokenize(texty)
    insult_array = []

    for sentence in sentence_array:
        insult_rating = Insults.rate_comment(sentence)
        insult_array.append(insult_rating)
        
    insult_level = np.percentile(insult_array, 50)
    
#     insult_level = insult_rating = Insults.rate_comment(text)
    print insult_level


0.0552408695033
0.10618687537
0.101973571364
0.150303109611
0.131821347288

In [ ]: