Convert Torrent Magnet link to a .torrent file with c# - c#

Is there a way to do it? I already tried with monotorrent, but due to the lack of up-to-date documentation i coudn't get it to work. I've already tried this with monotorrent, but still i can't find a way to get the .torrent file or even start the download to get the .torrent ...
The following piece of code was made that question as base, but it doesn't save anything to "D:\A" or to "D:\TorrentSave"
private void GerarTorrent(string magnet)
{
MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(new MonoTorrent.MagnetLink(magnet), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave");
MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
clientEngine.Register(b);
clientEngine.StartAll();
b.Start();
}
To generate the .torrent, it doesn't have to be monotorrent, in fact the only usage of this api would be for that, generating .torrent files from a magnet link...
EDIT: Updated code with my attempt on doing what Fᴀʀʜᴀɴ Aɴᴀᴍ said:
private void GerarTorrent(string hash)
{
MonoTorrent.Client.TorrentManager b = new MonoTorrent.Client.TorrentManager(MonoTorrent.InfoHash.FromHex(hash), "D:\\A", new MonoTorrent.Client.TorrentSettings(), "D:\\TorrentSave", new List<List<string>>());
MonoTorrent.Client.EngineSettings engineSettings = new MonoTorrent.Client.EngineSettings();
MonoTorrent.Client.ClientEngine clientEngine = new MonoTorrent.Client.ClientEngine(engineSettings);
clientEngine.Register(b);
clientEngine.StartAll();
b.Start();
}
Hash used = "5FC86BA08451CF4221E0091F31AF1A52C2219009"

You need to pass only the hash and not the entire magnet link to the TorrentManager constructor.
A magnet link looks like this:
magnet:?xt=urn:btih:18981bc9759950b4715ad46adcaf514e6a773cfe
So, a more generalized form:
magnet:?xt=urn:btih:<hash>
You need to extract this <hash> and pass it to the constructor:
manager = new TorrentManager(InfoHash.FromHex(hash), downloadsPath, torrentDefaults, downloadsPathForTorrent, new List<List<string>>());

Related

How to use Google Cloud Speech (V1 API) for speech to text - need to be able to process over 3 hours audio files properly and efficiently

I am looking for documentation and stuff but could not find a solution yet
Installed NuGet package
Also generated API key
However can't find proper documentation how to use API key
Moreover, I want to be able to upload very long audio files
So what would be the proper way to upload up to 3 hours audio files and get their results?
I have 300$ budget so should be enough
Here my so far code
This code currently fails since I have not set the credentials correctly at the moment which I don't know how to
I also have service account file ready to use
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
var speech = SpeechClient.Create();
var config = new RecognitionConfig
{
Encoding = RecognitionConfig.Types.AudioEncoding.Flac,
SampleRateHertz = 48000,
LanguageCode = LanguageCodes.English.UnitedStates
};
var audio = RecognitionAudio.FromStorageUri("1m.flac");
var response = speech.Recognize(config, audio);
foreach (var result in response.Results)
{
foreach (var alternative in result.Alternatives)
{
Debug.WriteLine(alternative.Transcript);
}
}
}
}
I don't want to set environment variable. I have both API key and Service Account json file. How can I manually set?
You need to use the SpeechClientBuilder to create a SpeechClient with custom credentials, if you don't want to use the environment variable. Assuming you've got a service account file somewhere, change this:
var speech = SpeechClient.Create();
to this:
var speech = new SpeechClientBuilder
{
CredentialsPath = "/path/to/your/file"
}.Build();
Note that to perform a long-running recognition operation, you should also use the LongRunningRecognize method - I strongly suspect your current RPC will fail, either explicitly because it's trying to run on a file that's too large, or it'll just time out.
You need to set the environment variable before create the instance of Speech:
Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", "text-tospeech.json");
Where the second param (text-tospeech.json) is your file with credentials generated by Google Api.

Playing Music from resources C#

I want to play some background music inside the launcher for my new project. But I get an error.
private void playlooping()
{
SoundPlayer.PlayLooping(system.Resources.Bgm);
}
//Bgm is the name of the song.
For some reason I get the error
no overload for method 'PlayLooping' Takes 1 arguments
As I mentioned in the comment, PlayLooping does not take any arguments. So you need to specify the sound you want to play somewhere else. This is done either by setting it via the SoundPlayer-Constructor like this:
// via string path
var soundPlayer = new SoundPlayer(#"C:\somePath\someFile.wav");
// via stream
var soundPlayer = new SoundPlayer(musicStream);
So if your resource is a string you should be good to go with:
var soundPlayer = new SoundPlayer(system.Resources.Bgm);
Alternative is to set the string-path or stream after instantiating your SoundPlayer via the Site- or Stream-Properties of your SoundPlayer object:
var soundPlayer = new SoundPlayer();
// via path
soundPlayer.Site = #"C:\path\test.wav";
// via stream
soundPlayer.Stream = someStream;
After setting this up correclty you should be good calling PlayLooping. So your final code should be looking like this (works for me in a test windows forms application, with the soundfile simply put in my debug folder):
var soundPlayer = new SoundPlayer("test.wav");
soundPlayer.PlayLooping();
You can find the full documentation on the SoundPlayer here: https://msdn.microsoft.com/en-us/library/system.media.soundplayer(v=vs.110).aspx

How to create transform for .msi file using c#

I'm trying to create a transform for .msi file in C#. Here is my code:
public static void CreateTransforms(string original_MSI, string backup_MSI, string MSTpath, string query)
{
File.Copy(original_MSI, backup_MSI, true);
using (var origDatabase = new Microsoft.Deployment.WindowsInstaller.Database(original_MSI, DatabaseOpenMode.ReadOnly))
{
using (var database = new Microsoft.Deployment.WindowsInstaller.Database(backup_MSI, DatabaseOpenMode.Direct))
{
//database.Execute("Update `Property` Set `Property`.`Value` = 'Test' WHERE `Property`.`Property` = 'ProductName'");
database.Execute(query);
database.GenerateTransform(origDatabase, MSTpath);
database.CreateTransformSummaryInfo(origDatabase, MSTpath, TransformErrors.None, TransformValidations.None);
}
}
}
I got the following error : "This installation package could not be opened. Contact the application vendor to verify that this is a valid Windows Installer package." in the step create transform summary info. I used "Microsoft.Deployment.WindowsInstaller.dll" library. Any help would be great.
A quick read of this static method looked correct so I created a console app out of it. It works fine for me on my machine. I would look at your calling method and make sure the data being passed is correct. I get really nervous any time I have a method that takes 4 strings as arguments as that leaves a lot to desire in terms of type safety.
when CreateTransforms start, it open database and it does not close it ...
you must commit and close the database before apply a new transform!
database.GenerateTransform(origDatabase, TRANSFORM);
database.CreateTransformSummaryInfo(origDatabase, TRANSFORM, TransformErrors.None, TransformValidations.None);
database.Commit();
database.Close();

Sitecore 7 Lucene.Net.Contrib highlight search results

I am trying to do highlighting on the search results. Here is the relevant part of my code.
QueryScorer scorer = new QueryScorer(q);
Lucene.Net.Search.Highlight.IFormatter formatter = new SimpleHTMLFormatter("<b>", "</b>");
Lucene.Net.Search.Highlight.Highlighter highlighter = new Highlighter(formatter, scorer);
highlighter.TextFragmenter = new SimpleFragmenter(800);
Lucene.Net.Util.Version vers = new Lucene.Net.Util.Version();
vers = Lucene.Net.Util.Version.LUCENE_30;
TokenStream stream = new StandardAnalyzer(vers).TokenStream(string.Empty, new StringReader(text));
string s = string.Empty;
try
{
s = highlighter.GetBestFragments(stream, text, 10, "...");
}
Here, GetBestFragments method throws a System.MissingMethodException.
I have tried to replace the original Lucene.net dll with Lucene.Net.Contrib but this time, I dont know what I should write instead of TokenStream. It doesnt exist in Lucene.Net.Contrib.* dlls.
I am working on existing code and I need to find out how I can rewrite TokenStream class and GetBestFragments method.
Thanx
The problem was something about deployment, that the new compatible Lucene.dll was replaced by the incompatible Sitecore7 dll.
So, if both lucene.net and lucene.net.contrib dll are referenced, it should work.
Not directly the solution to my question, but this source is worth mentioning again. (About lucene.dll versions) : http://laubplusco.net/sitecore-7-lucen-3-0-highlighted-results/

How can I write XML output?

How can I see the XML output of following C# code? I can see that it uses XElement, but where I can locate the XML file or the output?
private void Form1_Load(object sender, EventArgs e)
{
XElement doc = new XElement("searchresults"); // root element
//create
XElement result = new XElement("result",
new XElement("Resulthead", "AltaVista"),
new XElement("ResultURL", "www.altavista.com/"),
new XElement("Description", "AltaVista provides the most comprehensive search experience on the Web! ... "),
new XElement("DisplayURL", "www.altavista.com/")
);
doc.Add(result);
//add another search result
result = new XElement("result",
new XElement("Resulthead", "Dogpile Web Search"),
new XElement("ResultURL", "www.dogpile.com/"),
new XElement("Description", "All the best search engines piled into one. All the best search engines piled into one."),
new XElement("DisplayURL", "www.dogpile.com/")
);
doc.Add(result);
string xmlString = doc.ToString(SaveOptions.DisableFormatting);
}
Your result only exists inside your "xmlString" variable - it's not being written anywhere, neither onto the console / window, nor into a file.
You'll have to add a
doc.Save(#"C:\your-xml-file-name.xml");
line at the end of your method to save the contents to a file on disk.
Make sure to use a full path, or check in your current directory where the app is running (i.e. in (yourappname)\bin\debug, most likely).
Marc
That code is not writing XML anywhere but memory (the xmlString variable).
You could try calling XElement.Save() and get it on a file:
doc.Save(#"filename.xml");
Or use the debugger and look at the variables.
Or, if you prefer, simply put it in a TextBox:
textBox.Text = xmlString;
Be warned it may not be nicely formatted...

Categories