Skip to content

Instantly share code, notes, and snippets.

@ecnepsnai
Last active June 17, 2021 20:15
Show Gist options
  • Save ecnepsnai/148ec28f68359337e430 to your computer and use it in GitHub Desktop.
Save ecnepsnai/148ec28f68359337e430 to your computer and use it in GitHub Desktop.
Bing Image of the Day
// Bing Image of the Day for C#
// If you're using this in your project, give this gist a 'Star'!
// Made by Ian Spence with <3 in Vancouver, British Columbia, Canada
using System;
using System.Xml;
using System.Net;
public Uri GetImage() {
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable() == false) {
throw new System.Net.WebException("Could not establish a connection to bing.com");
}
System.Net.WebClient webClient = new System.Net.WebClient();
String result = webClient.DownloadString("http://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=1&mbl=1&mkt=en-ww");
XmlDocument doc = new XmlDocument;
doc.LoadXml(result);
return new Uri(@"http://www.bing.com/" + doc.SelectSingleNode(@"/images/image/url").InnerText);
}
package bing
import (
"encoding/xml"
"fmt"
"net/http"
)
func GetImage() (*string, error) {
resp, err := http.Get("https://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=1&mbl=1&mkt=en-ww")
if err != nil {
return nil, err
}
defer resp.Body.Close()
type bingImgType struct {
URL string `xml:"url"`
}
type bingImgResp struct {
XMLName xml.Name `xml:"images"`
Images []bingImgType `xml:"image"`
}
results := bingImgResp{}
if err := xml.NewDecoder(resp.Body).Decode(&results); err != nil {
return nil, err
}
if len(results.Images) < 0 {
return nil, fmt.Errorf("no images")
}
url := results.Images[0].URL
return &url, nil
}
// Bing Image of the Day for PHP
// If you're using this in your project, give this gist a 'Star'!
// Made by Ian Spence with <3 in Vancouver, British Columbia, Canada
$iotd = simplexml_load_string(file_get_contents('http://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=1&mbl=1&mkt=en-ww'));
$url = 'http://www.bing.com/' . $iotd->image->url;
'* Bing Image of the Day for VB.NET
'* If you're using this in your project, give this gist a 'Star'!
'* Made by Ian Spence with <3 in Vancouver, British Columbia, Canada
Imports System.Xml
Imports System.IO
Public Function GetImage() As Uri
If My.Computer.Network.IsAvailable = False Then
Throw New System.Net.WebException("Could not establish a connection to bing.com")
End If
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://www.bing.com/hpimagearchive.aspx?format=xml&idx=0&n=1&mbl=1&mkt=en-ww")
Dim doc As New XmlDocument
doc.LoadXml(result)
Return New Uri("http://www.bing.com/" & doc.SelectSingleNode("/images/image/url").InnerText)
End Function
@siblee77
Copy link

hey I'm trying to get some help from you , in uwp I donot have webclient() , what can I use there ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment