Commit 08bc35d3 authored by Tristan Smith's avatar Tristan Smith
Browse files

Worked on the code some more; having issues

issue with the download code in ToyPics\Class.cs ~downloadVid()
parent 9e3c2fff
......@@ -31,6 +31,9 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup>
<StartupObject>Something.Test</StartupObject>
</PropertyGroup>
<ItemGroup>
<Reference Include="CommandLine">
<HintPath>..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll</HintPath>
......@@ -50,11 +53,18 @@
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Test.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ToyPics\ToyPics.csproj">
<Project>{6cca1e51-ed9a-408c-9328-64367b366cab}</Project>
<Name>ToyPics</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
......
<Configurations active="Default"><Configuration name="Default"><GuestCommandRemote val="project command through a shared folder"></GuestCommandRemote><ShareFoldersRemote val=""></ShareFoldersRemote><RemoteDebugMonitor val="C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Remote Debugger\x86\msvsmon.exe"></RemoteDebugMonitor><MonitorName val="VMDebug"></MonitorName><RemoteVM val=""></RemoteVM><StartMode val="No"></StartMode><TerminationModeRemote val="No operation"></TerminationModeRemote><CopyFilesRemote val=""></CopyFilesRemote><PreRemoteCommandLine val=""></PreRemoteCommandLine><PostRemoteCommandLine val=""></PostRemoteCommandLine><RecordingToReplay val=""></RecordingToReplay><ReplayVM val=""></ReplayVM><BaseSnapshotForRecording val=""></BaseSnapshotForRecording><CopyFilesRecord val=""></CopyFilesRecord><PreRecordCommandLine val=""></PreRecordCommandLine><PostRecordCommandLine val=""></PostRecordCommandLine><TerminationModeRecord val="No operation"></TerminationModeRecord><InstanceToDebug val=""></InstanceToDebug><GuestCommandReplay val="project command through a shared folder"></GuestCommandReplay><ShareFoldersRecord val=""></ShareFoldersRecord><RemoteReplayFlag val=""></RemoteReplayFlag><RemoteMachine val=""></RemoteMachine><RemoteReplayVM val=""></RemoteReplayVM><RemoteRecordingToReplay val=""></RemoteRecordingToReplay><RemoteReplayPasscode val=""></RemoteReplayPasscode><HostSearchPath val=""></HostSearchPath></Configuration></Configurations>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ToyPics;
namespace Something
{
class Test
{
static void Main()
{
using (ToyPics.Class get = new ToyPics.Class("https://videos.toypics.net/view/3300/butt-bouncing-on-chance~/", @"D:\Personal\Desktop\", false))
{
get.downloadVid();
}
}
}
}
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Net;
using HtmlAgilityPack;
namespace ToyPics
{
class Class
public class Class : IDisposable
{
private Double ClassVersion = 0.1;
private HtmlWeb hw = new HtmlWeb();
private UIDGen uid = new UIDGen();
private HtmlDocument doc;
private string saveLocation;
private string pageURL;
private Boolean singleVideo = false;
/// <summary>
/// Generate video uri
/// sdahjklf
/// </summary>
/// <param name="link">Single video page</param>
/// <param name="strStart">.toypics.net/flvideo/</param>
/// <param name="strEnd">.mp4</param>
/// <returns></returns>
public static Uri getVidUri(HtmlNode link, string strStart, string strEnd)
/// <param name="videoPage">link to page</param>
/// <param name="saveLocation">video save location</param>
/// <param name="userPage">is the page a userPage</param>
public Class(string videoPage, string saveLocation, Boolean userPage)
{
// get javascript source
string jsSorce = link.InnerText.ToString();
this.init();
// make sure js code has what we need
//Uri video = getVidUri(link, @".toypics.net/flvideo/", ".mp4");
if (jsSorce.Contains(strStart) && jsSorce.Contains(strEnd))
{
// string triming
int Start = jsSorce.IndexOf(strStart, 0) + strStart.Length;
int End = jsSorce.IndexOf(strEnd, Start);
string video = jsSorce.Substring(Start, End - Start);// this `should` be the video link, or atlest the name
/*
* example //static1.toypics.net/flvideo/4ca4238a0b/14112522021748591567.mp4
* we would get back the video file name w/out extension and the directory - [4ca4238a0b/14112522021748591567]
* I'm unsure if the directory is important
* the javascript player does have some other options that could be deemed useful, unfortunately a proper title isnt one of them
*/
return new Uri(String.Format(@"http://static1.toypics.net/flvideo/{0}.mp4", video));// create a uri from the found string
// I normaly force HTTPS but static1 isnt setup for that
}
else
{
return null;
}
this.pageURL = videoPage;
this.doc = hw.Load(videoPage);
this.singleVideo = !userPage;
this.saveLocation = saveLocation;
}
public void Dispose()
{
this.hw = null;
this.doc = null;
this.uid = null;
}
private void init()
{
this.hw.UserAgent = String.Format("ToyPisDownloader [task=pageDownload] [version={0}] [uid={1}] [github/teh-random-name/toypicsDL]", this.ClassVersion, this.uid.userUID());
this.hw.UsingCache = false;
}
/// <summary>
/// Generate video uri; override function
/// Generate video uri
/// </summary>
/// <param name="link">Single video page</param>
/// <returns></returns>
public static Uri getVidUri(HtmlNode link)
private Uri getVidUri(HtmlNode link)
{
// get javascript source
string jsSorce = link.InnerText.ToString();
......@@ -77,42 +82,50 @@ namespace ToyPics
}
}
private static void downloadVid(string page)
/// <summary>
/// Download Video
/// </summary>
public void downloadVid()
{
HtmlWeb hw = new HtmlWeb();// HtmlAgilityPack
HtmlDocument doc = hw.Load(page);// load uri
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//script"))//loop every <script> tag
foreach (HtmlNode link in this.doc.DocumentNode.SelectNodes("//script"))//loop every <script> tag
{
if (Regex.IsMatch(link.InnerText, "(static1.toypics.net)"))// find javascript video player init code
{
try
{
Uri video = getVidUri(link, @".toypics.net/flvideo/", ".mp4");// find video source
Uri video = this.getVidUri(link);// find video source
using (WebClient dl = new WebClient())
{
var tmp1 = this.ClassVersion; //dev
var tmp2 = this.uid.uid; //dev
string tmp3 = this.uid.userUID(); //dev; same as above
// this is causing a NullRef exceptionl i have no fucking clue what could be causing this
dl.ResponseHeaders.Add("UserAgent", String.Format("ToyPisDownloader [task=videoDownload] [version={0}] [uid={1}] [github/teh-random-name/toypicsDL]", tmp1, tmp2));
//dl.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);// dev
dl.DownloadFileAsync(video, "video.mp4");// dev
Console.WriteLine("Done");// dev
Console.ReadKey();// dev
return;
dl.DownloadFile(video, String.Concat(this.saveLocation, this.getTitle(this.pageURL), ".mp4"));// dev
}
}
catch (Exception ex)
catch (Exception)
{
Console.WriteLine(ex);
throw;
}
}
}
}
private static string getTitle(string page)
private string getTitle(string page)
{
HtmlWeb hw = new HtmlWeb();// HtmlAgilityPack
HtmlDocument doc = hw.Load(page);// load uri
string link = doc.DocumentNode.SelectSingleNode("//*[@id='view-video-content']/*[@class='section bg2']/*[@class='hd']").InnerText; // the true title is stored in a <div> tag with the class of hd-l (HD-L) but i keep getting an exception if i try /*[@class='hd-l']
//for now im just going to trim the returned string
return link.Trim();
string invalidChars = Regex.Escape(new string(Path.GetInvalidFileNameChars()));
string invalidRegStr = String.Format( @"([{0}]*\.+$)|([{0}]+)", invalidChars );
return Regex.Replace(link.Trim(), invalidRegStr, "_");
}
}
}
......@@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ToyPics</RootNamespace>
<AssemblyName>ToyPicsClass</AssemblyName>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
......@@ -20,6 +21,8 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<DocumentationFile>bin\Debug\ToyPicsClass.XML</DocumentationFile>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
......@@ -28,6 +31,7 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack">
......@@ -35,6 +39,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Management" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
......@@ -44,6 +49,7 @@
<ItemGroup>
<Compile Include="Class.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UIDGen.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
using System;
using System.IO;
using System.Management;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Diagnostics;
/* http://stackoverflow.com/q/9039212/4366411
* modifed from above to only get model and cpuid in a md5 hash
*/
namespace ToyPics
{
class UIDGen
{
private string appdataFolder = String.Format(@"{0}\ShortCord\toypicsDL\", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
public string uid;
public UIDGen()
{
try
{
while (true)
{
string file = String.Concat(appdataFolder, "uid");
if (this.tempExists()) // if folder exists
{
if (this.tempExists(file)) // if file exists
{
this.uid = File.ReadAllText(file); // read file
break;
// this should break the while loop cause uid is nolonger null
}
else
{
using (StreamWriter writer = new StreamWriter(file)) // create file and write to it
{
writer.Write(this.HardwareProfile());
writer.Close();
break;
}
}
}
else // folder doesnt exist; so create it and make the uid file
{
Directory.CreateDirectory(appdataFolder);
File.Create(file);
Thread.Sleep(1000);
}
}
}
catch
{
throw;
}
}
public string userUID()
{
return this.uid;
}
private bool tempExists()
{
return Directory.Exists(appdataFolder);
}
private bool tempExists(string file)
{
return File.Exists(file);
}
private string HardwareProfile()
{
try {
using (MD5 md5 = MD5.Create())
{
byte[] inputBytes = Encoding.ASCII.GetBytes(String.Format("{0}{1}", this.GetComputerModel(), this.GetCpuId()));
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("X2"));
}
return sb.ToString();
}
}
catch
{
throw;
}
}
private string GetComputerModel()
{
Console.WriteLine("GetComputerModel");
var s1 = new ManagementObjectSearcher("select * from Win32_ComputerSystem");
foreach (ManagementObject oReturn in s1.Get())
{
Debug.WriteLine(oReturn["Model"].ToString().Trim());
return oReturn["Model"].ToString().Trim();
}
return string.Empty;
}
private string GetCpuId()
{
Console.WriteLine("GetCpuId");
var managClass = new ManagementClass("win32_processor");
var managCollec = managClass.GetInstances();
foreach (ManagementObject managObj in managCollec)
{
//Get only the first CPU's ID
Debug.WriteLine(managObj.Properties["processorID"].Value.ToString());
return managObj.Properties["processorID"].Value.ToString();
}
return string.Empty;
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment