How can I download a lot of audio (.ogg) files from Wikimedia Commons? Is it possible using the Mediawiki API?
You can use MediaWiki API to get the url download links not only for .ogg but also to any other image or media file uploaded on Wikimedia Commons. From the response you can easy download each one file. Here is an example in C#:
private static void GetFiles(List<string> fileNames)
{
//Get HTML request with all file names
var url = "https://commons.wikimedia.org/w/api.php?action=query&format=xml" +
"&prop=imageinfo&iiprop=url&titles=File:" + string.Join("|File:", fileNames);
using (var webResponse = (HttpWebResponse)WebRequest.Create(url).GetResponse())
{
using (var reader = new StreamReader(webResponse.GetResponseStream()))
{
var response = reader.ReadToEnd();
//Get all file url links by parsing the XML response
var links = XElement.Parse(response).Descendants("ii")
.Select(x => x.Attribute("url").Value);
foreach (var link in links)
{
//Save the current file on the disk
using (var client = new WebClient())
{
var fileName = link.Substring(link.LastIndexOf("/") + 1);
client.DownloadFile(link, fileName);
}
}
}
}
}
Usage:
//list of files to download
var fileNames = new List<string>() {
"Flag of France.svg", "Black scorpion.jpg", "Stop.png", //image
"Jingle Bells.ogg", "Bach Astier 15.flac", //audio
"Cable Car.webm", "Lion.ogv", //video
"Animalibrí.gif", //animation
};
GetFiles(fileNames);
Note: The API has limit for the files:
Maximum number of values is 50 (500 for bots).
So, if you need to download more files, you will have to split the list in parts and to create another requests.
Related
I have Web API service:
[ActionName("download")]
[HttpGet]
public HttpResponseMessage Download()
{
var stream = new FileStream(HostingEnvironment.MapPath("~/tmp/") + "doc.pdf", FileMode.Open);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(stream)
};
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = document.Name + "." + document.AssociatedApplication.Extension
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return result;
}
Bot's code:
if (message.Text.StartsWith("/d"))
{
var contentType = "application/pdf";
var attachment = new Attachment(contentType, "https://localhost/api/documents.download");
var response = await client.GetAsync("https://localhost/api/documents.download");
var data = await response.Content.ReadAsByteArrayAsync();
System.IO.File.WriteAllBytes(HostingEnvironment.MapPath("~/tmp/") + document.Name + "." + document.Extension, data);
var stream = System.IO.File.ReadAllBytes(HostingEnvironment.MapPath("~/tmp/") + document.Name + "." + document.Extension);
attachment.Content = stream;
var msg = message.CreateReplyMessage("This is your document: ");
msg.Attachments = new[] { attachment };
await context.PostAsync(msg);
}
If I change content type on the server and client to "image/png" and send PNG image from server to client then this sample works perfect - in the Bot Framework Emulator I got text "This is your document: " and received image.
But if I try to send PDF document with content type "application/pdf" or "application/octet-stream" and get it on the client with content type "application/pdf" then on the Bot Framework Emulator I got message like that:
This is your document: (https://localhost/api/documents.download)
Is this possible to get in the conversation "real" document instead of link for download (how it works with images)?
PS: This question works only for "image/png" or similar content types.
2 things:
1. it doesn't look like you are setting the content type for the attachment (the code above is using "")
2. Content is not for pushing media files. Our messages are limited to 256k serialized json. If you want to send a document or image you send an attachment with url pointing to the file and contenttype for the file
3. Not all channels have semantics for files other than images and they represent them as links. We use the contenttype to determine if we can do something channel specific for a given attachment.
What is the best way to resize an uploaded image in MVC 6? I'd like to store multiple variants of an image (such as small, large, etc.) to be able to choose which to display later.
Here's my code for the action.
[HttpPost]
public async Task<IActionResult> UploadPhoto()
{
if (Request.Form.Files.Count != 1)
return new HttpStatusCodeResult((int)HttpStatusCode.BadRequest);
IFormFile file = Request.Form.Files[0];
// calculate hash
var sha = System.Security.Cryptography.SHA256.Create();
byte[] hash = sha.ComputeHash(file.OpenReadStream());
// calculate name and patch where to store the file
string extention = ExtentionFromContentType(file.ContentType);
if (String.IsNullOrEmpty(extention))
return HttpBadRequest("File type not supported");
string name = WebEncoders.Base64UrlEncode(hash) + extention;
string path = "uploads/photo/" + name;
// save the file
await file.SaveAsAsync(this.HostingEnvironment.MapPath(path));
}
I would suggest using Image Processor library.
http://imageprocessor.org/imageprocessor/
Then you can just do something along the lines of:
using (var imageFactory = new ImageFactory())
using (var fileStream = new FileStream(path))
{
file.Value.Seek(0, SeekOrigin.Begin);
imageFactory.FixGamma = false;
imageFactory.Load(file.Value)
.Resize(new ResizeLayer(new Size(264, 176)))
.Format(new JpegFormat
{
Quality = 100
})
.Quality(100)
.Save(fileStream);
}
Where file.Value is your file that was uploaded (the stream) (I don't know what it is in MVC, this is code I use in a Nancy project)
I'm developing a app where i have to display filenames in a listbox, files are created by the user and are stored in a directory created using isolated storage. i'm new to windows phone programming. i'm not finding enough resources for isolated storage file access??? Plzzz help
Code for Binding to the list:
private void bindList()
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileList = appStorage.GetFileNames("/NotesForBible");
listPicker1.ItemsSource = fileList;
}
COde for adding the file:
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
appStorage.CreateDirectory("NotesForBible");
if (!appStorage.FileExists(fileName))
{
using (var file = appStorage.CreateFile("NotesForBible/" + fileName ))
{
using (var writer = new StreamWriter(file))
{
writer.WriteLine(fileContent);
}
}
}
I'm not able to view the files created in the listbox
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
To get an array of all filenames:
string directory = "whatever";
string[] filenames = store.GetFileNames(directory);
For further information: http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile.aspx
I am trying to save the stream of image data to a file. I was able to save it to Pictures library though.
But I want to save it to a file in the root of my application/ project.
I was trying the below but it doesn't work.
using (MediaLibrary mediaLibrary = new MediaLibrary())
mediaLibrary.SavePicture(#"\DefaultScreen.jpg", stream);
In this case you should use LocalStorage.
Here is a simple solution to do this:
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoStore.FileExists(fileName)
{
var sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
using (var br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
string strBaseDir = string.Empty;
const string DelimStr = "/";
char[] delimiter = DelimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
// Recreate the directory structure
for (int i = 0; i < dirsPath.Length - 1; i++)
{
strBaseDir = Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
{
bw.Write(data);
}
}
}
}
Here you can find all info about data in Windows Phone:
http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff402541(v=vs.105).aspx
In Windows Phone 7 how can I save a BitmapImage to local storage? I need to save the image for caching and reload if it is requested again in the next few days.
If you save the file into IsolatedStorage you can set a relative path to view it from there.
Here's a quick example saving a file that was included in the XAP (as a resource) into Isolated Storage.
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!isoStore.FileExists(fileName)
{
var sr = Application.GetResourceStream(new Uri(fileName, UriKind.Relative));
using (var br = new BinaryReader(sr.Stream))
{
byte[] data = br.ReadBytes((int)sr.Stream.Length);
string strBaseDir = string.Empty;
const string DelimStr = "/";
char[] delimiter = DelimStr.ToCharArray();
string[] dirsPath = fileName.Split(delimiter);
// Recreate the directory structure
for (int i = 0; i < dirsPath.Length - 1; i++)
{
strBaseDir = Path.Combine(strBaseDir, dirsPath[i]);
isoStore.CreateDirectory(strBaseDir);
}
using (BinaryWriter bw = new BinaryWriter(isoStore.CreateFile(fileName)))
{
bw.Write(data);
}
}
}
}
You may also be interested in the image caching converters created by Ben Gracewood and Peter Nowaks. They both show saving images into isolated storage and loading them from there.
Another approach I've used is to pass the stream you retrieve for the image in your xap straight into an isolated storage file. Not a lot of moving parts.
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication()) {
var bi = new BitmapImage();
bi.SetSource(picStreamFromXap);
var wb = new WriteableBitmap(bi);
using (var isoFileStream = isoStore.CreateFile("pic.jpg"))
Extensions.SaveJpeg(wb, isoFileStream, wb.PixelWidth, wb.PixelHeight, 0, 100);
}