/
movie-sbtitles.cs
127 lines (112 loc) · 4.16 KB
/
movie-sbtitles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
using System.IO;
using System.Net;
namespace Subtitler
{
class Program
{
static void Main(string[] args)
{
string path = @"D:\Sherlock.S03E01.HDTV.x264-ChameE";//\Sherlock.S03E01.HDTV.x264-ChameE.mkv";
string finalPath = string.Empty;
FileInfo[] fileList = new FileInfo[] { };
List<string> patterns = new List<string>();
patterns.Add("*.avi");
patterns.Add("*.mp4");
patterns.Add("*.mkv");
patterns.Add("*.mpg");
patterns.Add("*.mpeg");
DirectoryInfo dir = new DirectoryInfo(path);
foreach (string pattern in patterns)
{
if (dir.GetFiles(pattern).Count() > 0)
{
fileList = dir.GetFiles(pattern);
}
}
finalPath = fileList.First().FullName;
foreach (FileInfo file in fileList)
{
if (file.Length > new FileInfo(finalPath).Length)
finalPath = file.FullName;
}
PythonInstance py = new PythonInstance();
py.CallMethod("somemethod");
var hash = py.CallFunction("get_hash", finalPath);
string url = "http://api.thesubdb.com/?action=download&hash=" + hash + "&language=en";
//WebClient client = new WebClient();
//client.BaseAddress = url;
//client.Headers.Add("UserAgent", "SubDB/1.0 (subtitle-downloader/1.0; http://github.com/manojmj92/subtitle-downloader)");
//byte[] data = client.DownloadData(url);
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "SubDB/1.0 (movie-subtitles/1.0; https://github.com/nalawade41/movie-subtitles)";
WebResponse response = webRequest.GetResponse();
Console.WriteLine(hash);
Console.ReadLine();
}
}
public class PythonInstance
{
private ScriptEngine engine;
private ScriptScope scope;
private ScriptSource source;
private CompiledCode compiled;
private object pythonClass;
public PythonInstance(string className = "PyClass")
{
string code = @"
import sys
sys.path.append(r'C:\Program Files\IronPython 2.7\Lib')
import os
import hashlib
import urllib2
class PyClass:
def __init__(self):
pass
def somemethod(self):
print 'in some method'
def isodd(self, n):
return 1 == n % 2
def get_hash(self,name):
readsize = 64 * 1024
with open(name, 'rb') as f:
size = os.path.getsize(name)
data = f.read(readsize)
f.seek(-readsize, os.SEEK_END)
data += f.read(readsize)
return hashlib.md5(data).hexdigest()
";
//creating engine and stuff
engine = Python.CreateEngine();
scope = engine.CreateScope();
//loading and compiling code
source = engine.CreateScriptSourceFromString(code, Microsoft.Scripting.SourceCodeKind.Statements);
compiled = source.Compile();
//now executing this code (the code should contain a class)
compiled.Execute(scope);
//now creating an object that could be used to access the stuff inside a python script
pythonClass = engine.Operations.Invoke(scope.GetVariable(className));
}
public void SetVariable(string variable, dynamic value)
{
scope.SetVariable(variable, value);
}
public dynamic GetVariable(string variable)
{
return scope.GetVariable(variable);
}
public void CallMethod(string method, params dynamic[] arguments)
{
engine.Operations.InvokeMember(pythonClass, method, arguments);
}
public dynamic CallFunction(string method, params dynamic[] arguments)
{
return engine.Operations.InvokeMember(pythonClass, method, arguments);
}
}
}