Huseyin Sahin
Huseyin Sahin

Reputation: 221

How to update firebase realtime database periodically?

I have an asynctask that parses html, takes some content from it and than puts them into firebase realtime database (as a json) My code is working if you run the code by yourself but since the data is changing, i want from the server to run the code periodically, lets say for every 5 minutes, to update my database. Is this possible in firebase? If not, can you explain alternative ways of doing this (where a server executes your code periodically on its own)?

static class fetchdata extends AsyncTask<String, Void, DatabaseReference>{

    private DatabaseReference hurriyetDatabase = FirebaseDatabase.getInstance().getReference("HurriyetNews");

    @Override
    protected DatabaseReference doInBackground(String... urls) {

        String url = urls[0];

        hurriyetDatabase.removeValue();

        try {
            Document wholePage = Jsoup.connect(url).get();
            Elements links = wholePage.select(
                    "div.swiper-slide a[href][href*=/gundem/], " +
                            "div.swiper-slide a[href][href*=/dunya/], " +
                            "div.swiper-slide a[href][href*=/ekonomi/], " +
                            "div.swiper-slide a[href][href*=/teknoloji/]");

            for (int i=0; i< links.size(); i++){
                Element link = links.get(i);
                String newsUrl = link.attr("abs:href");
                Document currentnews = Jsoup.connect(newsUrl).get();

                String category = currentnews
                        .select("meta[property=article:section]")
                        .first()
                        .attr("content");

                String title = currentnews
                        .select("meta[property=og:title]")
                        .first()
                        .attr("content");

                String description = currentnews
                        .select("meta[property=og:description]")
                        .first()
                        .attr("content");

                String imageUrl = currentnews
                        .select("meta[property=og:image]")
                        .first()
                        .attr("content");

                String lastUpdateTime = currentnews
                        .select("time")
                        .first()
                        .attr("datetime");

                NewsEntry currentNews = new NewsEntry(
                        "Hürriyet", category, title, newsUrl, imageUrl, lastUpdateTime, description);


                String id = hurriyetDatabase.push().getKey();
                hurriyetDatabase.child(id).setValue(currentNews);

            }


        } catch (IOException e) {
            e.printStackTrace();
        }

        return hurriyetDatabase;
    }
}

I want to use this json as a database that the clients of the mobile app will query from it with respect to their desires.

Upvotes: 1

Views: 1164

Answers (2)

Alex Mamo
Alex Mamo

Reputation: 138924

Is this possible in firebase?

Yes it is, using Cloud Functions for Firebase that allows you to automatically run backend code. This can also work in response to events triggered by Firebase features and HTTPS requests. But note that a single cloud function can only respond to changes that happens at a single location at a time.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599491

On Firebase you could accomplish this with Cloud Functions, which run in response to events that happen within Firebase or outside of it.

For this specific case, you're looking to run code on a schedule, so have a look at Cloud Functions for Firebase trigger on time?

Upvotes: 1

Related Questions