0

I'm working on a school project right now which is based on the Vine website. Basically the goal of my project is to display on a screen many videos (from vines url) playing one by one.

First I use the following process : I put an Iframe in my html code and then I update the src of this Iframe every 6 seconds, for the moment this step works perfectly ! Every 6000 milliseconds videos are switching between them.

If this step works it's because I already have my three links stored as Strings in an array and then I simply called a function which chose a random index in this array.

BUT now I encounter a bigger problem, what I want to do is to add working links (by "working" I obviously mean valid, when I try to reach the URL it does not return 404) into my array, and the issue is that I don't really know how to handle this but I guess there is no thousands solutions.

My main idea was a kind of brute force : I wanted to generate random links and tried every generated links' connection. The function which creates the random links is done but my issue is when it comes to check the connectivity.. I tried a XML method from this thread then I found other topics about it and I ended up with it :

function testUrlConnection(urlCheck){

"use strict";

var xhr = new XMLHttpRequest();

xhr.open('HEAD', file, true);   
xhr.setRequestHeader("Access-Control-Allow-Origin","*")
xhr.send();

console.log(xhr.status); //return 0 no matter what

        if (xhr.status != "404") {
            console.log(urlCheck + " connection exist!");
    } else {
            console.log(urlCheck+ " connection doesn't exist!");
        }
}

Thus I tried a lot of modifications but I discovered that it was an Origin or Domain issue.

Sorry for making it long but actually this is not really a code issue but more a way of handling this, do you think this "brute force" idea can be pursued or do I need to handle this in any other way ? If yes which advice can you give me ?

Thank you, NEAK

Community
  • 1
  • 1
NexusI9
  • 1
  • 1
  • 1
  • Any type of web scraping will probably be against the terms of service of the site you're scraping from and may eventually get your IP rate-limited anyway. If there's a legitimate API you can use to grab links, use that. It's infeasible to generate random links and hope they work. – bvpx Dec 19 '16 at 18:34
  • 1
    You might search for an Vine Api... – Jonas Wilms Dec 19 '16 at 18:56
  • @bvpx Yes that's what I thought.. I just looked for it and it doesn't seem to be lost ! – NexusI9 Dec 19 '16 at 19:03

1 Answers1

0

While this idea works I think you should try a different approach, like infinite scrolling. Like Facebook, it works so that as a user is scrolling down the page more and more content gets loaded. This means you don't have to load a bunch of content at once, but you have to keep track of where the user on the page.

Regardless of what way you load the videos, you have to get valid urls to your videos. It sounds like you got the URL generator, and to make sure the URL doesn't return a 404 I'd do something like this: Checking if a URL is broken in Javascript.

Good luck!

Community
  • 1
  • 1
Thomas Juranek
  • 343
  • 2
  • 16
  • Then I think the iframe will work well. Just generate an array full of valid urls on the page load, and just change the iframe source and you should be good. @NEAK – Thomas Juranek Dec 19 '16 at 19:14
  • Thank you Thomas the idea of scrolling looks very interesting, nevertheless the goal of my project was to create a kind of weird TV which plays itself automatically. I will try the JQuery way very soon ! But I hope it's not outdated.. EDIT: I just tried the JQuery code, it's doesn't seem to work too, I get the same error "XMLHttpRequest cannot load [myUrl]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1' is therefore not allowed access." – NexusI9 Dec 19 '16 at 19:29
  • Because you are running your site on the localhost (I am assuming through IIS), you need to do this: http://stackoverflow.com/questions/28547288/no-access-control-allow-origin-header-is-present-on-the-requested-resource-err – Thomas Juranek Dec 19 '16 at 19:50
  • I did change my /etc/hosts file through the terminal so in ended up with this : 127.0.0.1 localhost vine.co But when I opened my html file in the Browser (Chrome), nothing appear.. But I didn't really understood the part when thanix wrote : "Then if you'd access your script using yourdomain.com instead of localhost, the call should succeed." I guess that's why it failed – NexusI9 Dec 19 '16 at 20:34
  • You should end up with something like this: 127.0.0.1 localhost neakwebsite.com – Thomas Juranek Dec 19 '16 at 20:41
  • Alright my bad I just understood, I don't have any domain for the moment and I think I will remain on a localhost connection, so I guess this is a dead end ? – NexusI9 Dec 19 '16 at 20:59
  • No, it can be any domain name. The domain name just means that when you go to somedomain.com on your computer, it will pull up the page on your local host. You could say "127.0.0.1 localhost google.com" and have the source code be "

    Hello World

    ". Then when you go to google.com it will pull up your hello world page, not the actual google.com search engine.
    – Thomas Juranek Dec 19 '16 at 21:03
  • Okay thank you. I forgot to mentioned that I used to use the Live View from Dreamweaver to open my html file in Chrome and I ended up with an url like this : [http://127.0.0.1:50813/preview/app/index.html] and obviously the error showed [Origin 'http://127.0.0.1:50813'....] but when I open my html from the finder directly the error shows [Origin 'null'...], so I will look for "linking" my html file to my local host because I guess that's where the problem comes from – NexusI9 Dec 19 '16 at 21:37