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?
 
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
 
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
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
 
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 use byte array and pass that while  adding file to picture library
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.