Fetch images from instagram

I am extremely new to  sharepoint. I wish to create daily job to fetch #tag images from instagram and load it in list
I have created daily job to add task to already created list like below. I have added this code in Execute method.

SPList thisTaskList = oSPWeb.Lists["MyTaskList"];

SPListItem tasks = thisTaskList.Items.Add();

tasks["Title"] = "New task" + DateTime.Now.ToString();;

tasks.Update();

Open in new window


Now i think i need to use sharepoint server side api to connect to instagram api and fetch images and load in list.

How can i achieve this? I have googled a lot but no help available.
kirannandedkarAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kirannandedkarAuthor Commented:
I think my question is valid and i have put some efforts to find solution. I am extremely new to sharepoint.
0
kirannandedkarAuthor Commented:
Hello may i know if there is anyone to help me here. I dont need full fledged solution here
0
Rainer JeschorCommented:
Hi,
some questions upfront:
- Which SharePoint version and edition?
- What do you mean with "load image in list" ?
Do you want to create list entries with the image metadata OR upload images as attachment to a list item OR upload images to a document library?
- What do you mean with "#tag images"?
How many images do you expect to get per day (just to consider the sizing / proper list/library structuring)?

Thanks.
Rainer
0
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

kirannandedkarAuthor Commented:
I am using sharepoint 2013 . I meant load images in document library and then upload in list entries. #tag images are one which we search for particular tag in instagram and it gives images connected to that tag. I expect to get around 500 images per day. Please help me
0
kirannandedkarAuthor Commented:
I have this code below which successfully gives me images. But i dont know how to save those in document library.

do
        {
            WebRequest webRequest = null;
            if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
                webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/tags/{0}/media/recent?access_token={1}", strtagName, strAccessToken));
            else
                webRequest = HttpWebRequest.Create(nextPageUrl);

            var responseStream = webRequest.GetResponse().GetResponseStream();
            Encoding encode = System.Text.Encoding.Default;


            using (StreamReader reader = new StreamReader(responseStream, encode))
            {
                JToken token = JObject.Parse(reader.ReadToEnd());
                var pagination = token.SelectToken("pagination");

                if (pagination != null && pagination.SelectToken("next_url") != null)
                {
                    nextPageUrl = pagination.SelectToken("next_url").ToString();
                }
                else
                {
                    nextPageUrl = null;
                }

                var images = token.SelectToken("data").ToArray();

                foreach (var image in images)
                {
                    imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();

                    if (String.IsNullOrEmpty(imageUrl))
                        Console.WriteLine("broken image URL");

                    var imageResponse = HttpWebRequest.Create(imageUrl).GetResponse().GetResponseStream();

                    var imageId = image.SelectToken("id");
                    object outputDir = null;

                    using (var imageWriter = new StreamWriter(String.Format("{0}\\{1}.jpg", outputDir, imageId)))
                    {
                        imageResponse.CopyTo(imageWriter.BaseStream);
                        imageResponse.Flush();
                        Console.WriteLine("copied {0}", imageId);

                    }

                    return imageUrl;

                }


            }

        }
        while (!String.IsNullOrEmpty(nextPageUrl));

Open in new window

0
Walter CurtisSharePoint AEDCommented:
You don't need to use SharePoint to grab the images. You should be able to download the images you want using something besides SharePoint. Once you have the images, you can use PowerShell to upload to what ever document library you want. No need to complicate grabbing images with SharePoint.

Hope that helps
0
tapiwabCommented:
Hi

can you have a look on the link below to authenticate on instagram and have the images
http://www.c-sharpcorner.com/UploadFile/raj1979/how-to-authenticate-and-get-data-using-instagram-api/


code to add the images to SharePoint

String fileToUpload = @"C:\YourFile.txt";
String sharePointSite = "http://yoursite.com/sites/Research/";
String documentLibraryName = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointSite))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        if (!System.IO.File.Exists(fileToUpload))
            throw new FileNotFoundException("File not found.", fileToUpload);                    

        SPFolder myLibrary = oWeb.Folders[documentLibraryName];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(fileToUpload);
        FileStream fileStream = File.OpenRead(fileToUpload);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit
        myLibrary.Update();
    }
}
0
kirannandedkarAuthor Commented:
Hi @tapiweb

What to put in variable fileToUpload is my question. Because i dont store image in any folder. I get directly from instagram. Please check my code  where i get data from instagram .
0
tapiwabCommented:
fileToUpload should be image url on you code, i suppose
0
kirannandedkarAuthor Commented:
Will that make any sense? Because in my code i m already getting imageurl and writing to streamwriter. Can u tell exactly where?
0
tapiwabCommented:
hi
the way I understood is you are  getting the images from instagram and you need to add the images to SharePoint picture library.


there a different Arguments you can use to add, on my above code the line that add image to the library is
SPFile spfile = myLibrary.Files.Add(String urloffile, stream file/ byte[] file, bool overwrite);

Note the myLibrary is a SharePoint picture library or document library not a  list
0
kirannandedkarAuthor Commented:
is it possible for you to combine mine and your answer and give me one complete answer? My code fetches images from instagram and your code uploads it to document library.
0
kirannandedkarAuthor Commented:
Looks like you are not understanding what i am asking. In your code you pass hard coded file name from your c drive. I dont have any images stored. I am directly getting from instagram in form of stream. Now i want to upload that to document library. Please help
0
kirannandedkarAuthor Commented:
I solved my issue myself. I am using byte array. Here is code

var webClient = new WebClient();
byte[] converttobyte= webClient.DownloadData(imageUrl);


var imageId = image.SelectToken("id");

String sharePointSite = "http://mysite/";
String documentLibraryName = "ImageLibrary";

using (var site = new SPSite(sharePointSite))
{
      using (SPWeb web = site.OpenWeb())
      {
           SPPictureLibrary pics = (SPPictureLibrary)web.Lists[documentLibraryName];

            pics.RootFolder.Files.Add((string) imageId + ".jpg", converttobyte, true);


       }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kirannandedkarAuthor Commented:
I use byte array and pass that while  adding file to picture library
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Images and Photos

From novice to tech pro — start learning today.