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]:
import pyrebase
from urllib.parse import urlparse

config = {
  "apiKey": "/////",
  "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 [2]:
firebase = pyrebase.initialize_app(config)
auth = firebase.auth()
uid = "////"
password = "////"
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'])

Function to send article data to Firebase


In [3]:
def firebasePush(title, url, authors, date, summary, polarity, subjectivity, keywords, images, videos, text):
    domain = urlparse(url).netloc
    domain = domain.replace(".","")
    date = str(date)
    link = url
    url = url.replace("/","")
    url = url.replace(".","")
    url = url.replace("http:","")
    url = url.replace("https:","")
    url = url.replace(domain,"")
    url = url.split("?",1)[0]
    url = url.split("#",1)[0]
    text = text.replace("\n","")
    data = {
        "title": title,
        "authors": authors,
        "summary": summary,
        "date": date,
        "polarity": polarity,
        "subjectivity": subjectivity,
        "keywords": keywords,
        "images": images,
        "videos": videos,
        "article": text,
        "url": link
    }
    results = db.child("articles").child(domain).child(url).set(data, user['idToken'])

In [ ]:


In [ ]: