Esempio n. 1
0
        public Task2(MyNamedTasks Tasks)
            : base(Tasks, "Task2")
        {
            this.Description = @"
            This is a subtask. Task1 depends on our completion.
            This fact makes Task2 more important than Task1
            ";

            this.YieldWork =
                (Task, Input) =>
                {
                    var c = Input.ToFieldBuilder();

                    FileMappedField
                        TorrentName = c,
                        TorrentLink = c,
                        TorrentSize = c,
                        TorrentComments = c;

                    c.FromFile();

                    c[TorrentName] =
                        delegate
                        {

                        };

                    Input.Delete();

                    this.AppendLog(Input.Name);

                    return c.ToFile;
                };
        }
Esempio n. 2
0
        public Task2(MyNamedTasks Tasks)
            : base(Tasks, "Task2")
        {
            this.Description = @"
This is a subtask. Task1 depends on our completion. 
This fact makes Task2 more important than Task1
			"            ;

            this.YieldWork =
                (Task, Input) =>
            {
                var c = Input.ToFieldBuilder();

                FileMappedField
                    TorrentName     = c,
                    TorrentLink     = c,
                    TorrentSize     = c,
                    TorrentComments = c;

                c.FromFile();

                c[TorrentName] =
                    delegate
                {
                };

                Input.Delete();


                this.AppendLog(Input.Name);

                return(c.ToFile);
            };
        }
Esempio n. 3
0
        public Task3_FindTorrents(MyNamedTasks Tasks)
            : base(Tasks, "Task3_FindTorrents")
        {
            var NamedTasks = Tasks;

            this.Description = @"
This task will fetch torrent links and spawn work for them. 
This could be extended to support multiple torrent services. 
Default implmentation will assume <a href='http://piratebay.org/'>piratebay</a>.

This task should not run when there is work for Task4_PrepareMedia 
			"            ;

            this.YieldWork =
                (Task, Input) =>
            {
                // if we fail, we this work item will not be retried
                Input.Delete();

                var DismissedItems = 0;

                BasicPirateBaySearch.Search(
                    k => Memory.Contains(k.Hash),
                    (entry, deferred) =>
                {
                    if (deferred)
                    {
                        DismissedItems++;
                        return;
                    }

                    Memory.Add(entry.Hash);

                    AppendLog("output " + entry.Name);

                    var c = NamedTasks.Task4_PrepareMedia.AddWork(5, entry.Hash).ToFieldBuilder();

                    FileMappedField
                    TorrentName     = c[entry.Name],
                    TorrentLink     = c[entry.TorrentLink],
                    TorrentSize     = c[entry.Size],
                    TorrentComments = c[entry.Link];

                    c.ToFile();
                }
                    );

                if (DismissedItems > 0)
                {
                    AppendLog("dismissed " + DismissedItems);
                }

                return(delegate
                {
                    // this will be called if the task is still active
                });
            };
        }
Esempio n. 4
0
        // task 1 will run at 10 tick interval
        // the default input file will not be removed
        // and this will trigger retriggering


        // as we are generating work items for task2
        // we will wait until task2 is complete

        public Task1(MyNamedTasks Tasks_)
            : base(Tasks_, "Task1")
        {
            // jsc emits bad base ctor call without this workaround, why?
            var Tasks = Tasks_;

            this.Description = @"
task 1 will run at 10 tick interval
the default input file will not be removed
and this will trigger retriggering

as we are generating work items for task2
we will wait until task2 is complete
			"            ;

            this.YieldWork =
                (Task, Input) =>
            {
                var Counter = Task.Counter;

                this.AppendLog("creating work!");

                new Action[]
                {
                    //delegate
                    //{
                    //    var DropZone = Tasks.Task2_AliasSearch.Context.ToDirectory("Input/3");

                    //    DropZone.Create();

                    //    Tasks.Task2_AliasSearch.AddWork(3, Input.Name + "-" + Counter, "dummy work data");
                    //},
                    delegate
                    {
                        Tasks.Task3_FindTorrents.AddWork(50, "piratebay", "");
                    }
                }.InvokeByModulus(Counter);

                Thread.Sleep(2000);


                return(delegate
                {
                    // this will be called if the task is still active
                });
            };
        }
Esempio n. 5
0
        // task 1 will run at 10 tick interval
        // the default input file will not be removed
        // and this will trigger retriggering
        // as we are generating work items for task2
        // we will wait until task2 is complete
        public Task1(MyNamedTasks Tasks_)
            : base(Tasks_, "Task1")
        {
            // jsc emits bad base ctor call without this workaround, why?
            var Tasks = Tasks_;

            this.Description = @"
            task 1 will run at 10 tick interval
            the default input file will not be removed
            and this will trigger retriggering

            as we are generating work items for task2
            we will wait until task2 is complete
            ";

            this.YieldWork =
                (Task, Input) =>
                {
                    var Counter = Task.Counter;

                    this.AppendLog("creating work!");

                    new Action[]
                    {
                        //delegate
                        //{
                        //    var DropZone = Tasks.Task2_AliasSearch.Context.ToDirectory("Input/3");

                        //    DropZone.Create();

                        //    Tasks.Task2_AliasSearch.AddWork(3, Input.Name + "-" + Counter, "dummy work data");
                        //},
                        delegate
                        {
                            Tasks.Task3_FindTorrents.AddWork(50, "piratebay", "");
                        }
                    }.InvokeByModulus(Counter);

                    Thread.Sleep(2000);

                    return delegate
                    {
                        // this will be called if the task is still active
                    };
                };
        }
Esempio n. 6
0
        // change: C:\util\xampplite\apache\conf\httpd.conf

        // http://localhost/jsc/zmovies

        //Alias /jsc/zmovies "C:\work\code.google\zmovies\MovieAgent\MovieAgent\bin\Release\web"
        //<Directory "C:\work\code.google\zmovies\MovieAgent\MovieAgent\bin\Release\web">
        //       Options Indexes FollowSymLinks ExecCGI
        //       AllowOverride All
        //       Order allow,deny
        //       Allow from all
        //</Directory>



        public static void Application_Entrypoint(string Query, Action <string> Fork)
        {
            //DemoMPDB();
            //DemoOMDB();
            //DemoGooTube();
            //DemoCreateTinyArrowsToLetMeGoogleThatForYou();
            //DemoList();
            //DownloadGame();
            //ShowTinyURL();
            //ShowTitleSearch();
            //DemoTinEye();
            //ShowGoogleVideo();
            //ShowExampleDotCom();
            //DemoPosters();
            //DemoFindPosters();
            //DemoFindMovieAlias();

            //ShowPirateBayPosters();
            //ShowPirateBayWithoutVideo();
            //ShowPirateBayFast();
            //ShowPirateBayFastWithMemory();

            //DemoGetPosterViaTinEyeAndStreamItViaPirateBayImage("Shrek", "2007", new MemoryDirectory(new DirectoryInfo("memory")));
            //DemoGetPosterViaTinEyeAndStreamIt("Madea Goes to Jail");
            //DemoGetPosterViaTinEye("Madea Goes to Jail");
            //DemoGetPosterViaTinEye("Shrek 2");
            //DemoGetPosterViaTinEye("Bolt");



            //return;



            var MyTasks = new MyNamedTasks
            {
                Fork = Fork,
            };

            MyTasks.Invoke(Query);
        }
        public Task5_CloneMedia(MyNamedTasks Tasks)
            : base(Tasks, "Task5_CloneMedia")
        {
            var NamedTasks = Tasks;

            this.Description = @"
            Clone poster to imgbay.
            ";

            this.YieldWork =
                (Task, Input) =>
                {
                    var Entry = new Entry().FromFile(Input);

                    Input.Delete();

                    if (string.IsNullOrEmpty(Entry.TinEyeHash))
                    {
                        #region TinEyeHash
                        AppendLog("in " + Entry.PirateBay.Name + " without tineye");

                        BasicTinEyeSearch.Search(Entry.IMDB.MediumPosterImage,
                            h =>
                            {
                                AppendLog("as " + h.Hash);

                                Entry.TinEyeHash = h.Hash;
                                Entry.ToFile(Input);
                            }
                        );
                        #endregion
                    }
                    else if (string.IsNullOrEmpty(Entry.BayImgKey))
                    {
                        #region BayImgKey
                        AppendLog("in " + Entry.PirateBay.Name + " without bayimg");

                        // do we have it in memory?

                        var BayImg = this.Memory[Entry.TinEyeHash].FirstDirectoryOrDefault();

                        if (BayImg == null)
                        {
                            BasicPirateBayImage.Clone(new Uri(Entry.TinEyeImageLink),
                                e =>
                                {
                                    Entry.BayImgKey = e.Key;
                                    Entry.ToFile(Input);

                                    BayImg = this.Memory[Entry.TinEyeHash].CreateSubdirectory(e.Key);
                                    AppendLog("in " + Entry.PirateBay.Name + " now known as " + Entry.BayImgKey);
                                }
                            );
                        }
                        else
                        {
                            Entry.BayImgKey = BayImg.Name;
                            Entry.ToFile(Input);

                            AppendLog("in " + Entry.PirateBay.Name + " already known as " + Entry.BayImgKey);
                        }
                        #endregion

                    }
                    else if (string.IsNullOrEmpty(Entry.YouTubeKey))
                    {
                        AppendLog("in " + Entry.PirateBay.Name + " looking for video...");

                        BasicGoogleVideoCrawler.Search(Entry.YouTubeQuery,
                            (v, src) =>
                            {
                                AppendLog("as " + v);

                                Entry.YouTubeKey = v;
                                Entry.ToFile(Input);
                            }
                        );
                    }
                    else
                    {
                        var NextWork = NamedTasks.Task6_MediaCollector.AddWork(5, Entry.PirateBay.Hash);

                        if (NextWork.Exists)
                            AppendLog("out already exists " + Entry.PirateBay.Name + " as " + Entry.PirateBay.Hash);
                        else
                        {
                            AppendLog("out " + Entry.PirateBay.Name + " as " + Entry.PirateBay.Hash);

                            Entry.ToFile(NextWork);
                        }
                    }

                    // if we fail, we this work item will not be retried
                    //Input.Delete();

                    return delegate
                    {
                        // this will be called if the task is still active
                    };
                };
        }
        public Task4_PrepareMedia(MyNamedTasks Tasks)
            : base(Tasks, "Task4_PrepareMedia")
        {
            var NamedTasks = Tasks;
            this.Description = @"
            We now should have the torrent, name, seeders, leechers. We must now get the poster, trailer, rating, tagline and hash.
            Uses memory to store tineye poster hash.
            ";

            this.YieldWork =
                (Task, Input) =>
                {

                    var c = Input.ToFieldBuilder();

                    FileMappedField
                        TorrentName = c,
                        TorrentLink = c,
                        TorrentSize = c,
                        TorrentComments = c,
                        IMDBKey = c,
                        IMDBTitle = c,
                        IMDBYear = c,
                        IMDBRaiting = c,
                        IMDBRuntime = c,
                        IMDBGenre = c,
                        IMDBTagline = c,
                        OMDBSearched = c,
                        MPDBSearched = c,
                        PosterLink = c,
                        TinEyeHash = c,
                        BayImageLink = c,
                        BayImageTinyLink = c,
                        //BayImageTinySourceLink = c,
                        YouTubeKey = c,
                        TorrentLinkTinyLink = c,
                        TaskComplete = c;

                    c.FromFile();

                    Input.Delete();

                    //AppendLog("in " + TorrentName.Value);

                    #region IMDBKey
                    c[IMDBKey] = delegate
                    {
                        var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                        AppendLog("looking for imdb key");

                        BasicIMDBAliasSearch.SearchSingle(TorrentSmartName.Title, TorrentSmartName.Year,
                            imdb =>
                            {
                                AppendLog("imdb key found");
                                IMDBKey.Value = imdb.Key;
                            }
                        );
                    };
                    #endregion

                    #region IMDBTitle
                    c[IMDBTitle] = delegate
                    {
                        AppendLog("looking for imdb for details");

                        BasicIMDBCrawler.Search(IMDBKey.Value,
                            imdb =>
                            {
                                AppendLog("imdb details found");

                                IMDBTitle.Value = imdb.Title;
                                IMDBYear.Value = imdb.Year;
                                IMDBRaiting.Value = imdb.UserRating;
                                IMDBRuntime.Value = imdb.Runtime;
                                IMDBTagline.Value = imdb.Tagline;
                                IMDBGenre.Value = string.Join("|", imdb.Genres);

                                PosterLink.Value = imdb.MediumPosterImage;
                            }
                        );
                    };
                    #endregion

                    #region PosterLink
                    c[PosterLink] = delegate
                    {
                        AppendLog("looking for posters...");

                        c[OMDBSearched] = delegate
                        {
                            AppendLog("looking for posters... omdb");
                            OMDBSearched.Value = "true";

                            BasicOMDBCrawler.SearchSingle(IMDBTitle.Value, IMDBYear.Value,
                                omdb =>
                                {
                                    omdb.GetPoster(
                                        poster =>
                                        {
                                            AppendLog("looking for posters... omdb... found!");
                                            PosterLink.Value = poster;
                                        }
                                    );
                                }
                            );
                        };

                        c[MPDBSearched] = delegate
                        {
                            AppendLog("looking for posters... mpdb");

                            BasicMPDBCrawler.SearchSingle(IMDBTitle.Value, IMDBYear.Value,
                                mpdb =>
                                {
                                    mpdb.GetPoster(
                                        poster =>
                                        {
                                            AppendLog("looking for posters... mpdb... found!");
                                            PosterLink.Value = poster;
                                        }
                                    );
                                }
                            );
                        };
                    };
                    #endregion

                    #region TinEyeHash
                    c[TinEyeHash] = delegate
                    {
                        AppendLog("looking for tineye hash...");
                        BasicTinEyeSearch.Search(PosterLink.Value,
                            tineye =>
                            {
                                AppendLog("looking for tineye hash... found");
                                TinEyeHash.Value = tineye.Hash;

                                var Memory = this.Memory[TinEyeHash.Value].FirstDirectoryOrDefault();

                                if (Memory != null)
                                {
                                    BayImageLink.Value = "_";
                                    //BayImageTinyLink.Value = "_";
                                    BayImageTinyLink.Value = "http://tinyurl.com/" + Memory.Name;
                                }
                            }
                        );
                    };
                    #endregion

                    #region BayImageLink
                    c[BayImageLink] = delegate
                    {
                        AppendLog("updating bay image...");

                        var tineye = new BasicTinEyeSearch.Entry { Hash = TinEyeHash.Value };

                        BasicPirateBayImage.Clone(tineye.QueryLink.ToUri(),
                            bayimg =>
                            {
                                AppendLog("updating bay image... done");
                                BayImageLink.Value = bayimg.Image.ToString();
                            }
                        );

                    };
                    #endregion

                    #region BayImageTinyLink
                    c[BayImageTinyLink] = delegate
                    {
                        AppendLog("looking for tinyurl...");
                        BasicTinyURLCrawler.Search(BayImageLink.Value,
                            tinyurl =>
                            {
                                AppendLog("looking for tinyurl... found");
                                BayImageTinyLink.Value = tinyurl.Alias;
                            }
                        );
                    };
                    #endregion

                    //#region BayImageTinySourceLink
                    //c[BayImageTinySourceLink] = delegate
                    //{
                    //    BasicTinyURLCrawler.Search("http://i.tinysrc.mobi/" + BayImageTinyLink.Value,
                    //        tinyurl =>
                    //        {
                    //            BayImageTinySourceLink.Value = tinyurl.Alias;

                    //            this.Memory[TinEyeHash.Value].CreateSubdirectory(tinyurl.AliasKey);
                    //        }
                    //    );
                    //};
                    //#endregion

                    #region YouTubeKey
                    c[YouTubeKey] = delegate
                    {
                        var Query = IMDBTitle.Value;
                        var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                        var SeasonAndEpisode = TorrentSmartName.SeasonAndEpisode;

                        if (!string.IsNullOrEmpty(SeasonAndEpisode))
                            Query += " " + SeasonAndEpisode;
                        else
                            Query += " trailer";

                        BasicGoogleVideoCrawler.Search(Query,
                            (key, src) =>
                            {
                                YouTubeKey.Value = key;
                            }
                        );
                    };
                    #endregion

                    #region TorrentLinkTinyLink
                    c[TorrentLinkTinyLink] = delegate
                    {
                        BasicTinyURLCrawler.Search(TorrentLink.Value,
                            torrent =>
                            {
                                TorrentLinkTinyLink.Value = torrent.Alias;
                            }
                        );
                    };
                    #endregion

                    c[TaskComplete] = delegate
                    {
                        var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                        var YouTubeLink = "http://www.youtube.com/v/" + YouTubeKey + @"&hl=en&fs=1";
                        var YouTubeImage = "http://img.youtube.com/vi/" + YouTubeKey + @"/0.jpg";

                        var SmartTitle = "";

                        if (!string.IsNullOrEmpty(TorrentSmartName.SeasonAndEpisode))
                            SmartTitle = "TV show: " + IMDBTitle.Value + " " + TorrentSmartName.SeasonAndEpisode + " " + IMDBYear.Value;
                        else
                            SmartTitle = "Movie: " + IMDBTitle.Value + " " + IMDBYear.Value;

                        var NextWork = NamedTasks.Task6_MediaCollector.AddWork(5, Input.Name);

                        using (var w = new StreamWriter(NextWork.OpenWrite()))
                        {
                            w.WriteLine("<item>");
                            w.WriteLine("<title>" + SmartTitle + "</title>");
                            w.WriteLine("<link>" + TorrentLinkTinyLink.Value + "</link>");

                            #region description
                            w.WriteLine("<description><![CDATA[");

                            w.WriteLine(@"
            <object width='640' height='385'>
            <param name='movie' " + YouTubeLink.ToAttributeString("value") + @"></param>
            <param name='allowFullScreen' value='true'></param>
            <param name='allowscriptaccess' value='always'></param>
            <embed " + YouTubeLink.ToAttributeString("src") + @" type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='640' height='385'></embed>
            </object>
                            ");

                            w.WriteLine(
                                new IHTMLAnchor
                                {
                                    Style = new IStyle { @float = "left" },
                                    URL = YouTubeLink,
                                    innerHTML = new IHTMLImage
                                    {
                                        alt = YouTubeKey.Value,
                                        align = "left",
                                        src = YouTubeImage
                                    }
                                }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLAnchor
                                {
                                    URL = BasicIMDBCrawler.ToLink(IMDBKey.Value),
                                    Title = SmartTitle,
                                    innerHTML = new IHTMLImage
                                    {
                                        align = "right",
                                        src = BayImageTinyLink.Value,
                                    }
                                }.ToString()
                            );

                            w.WriteLine(
                                "<h2>" + SmartTitle.ToLink("http://piratebay.org" + TorrentComments.Value) + "</h2>"
                            );

                            w.WriteLine(
                                new IHTMLElement { innerHTML = IMDBRaiting.Value, title = "raiting" }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLElement { innerHTML = (IMDBRuntime.Value + ", " + TorrentSize.Value.Replace("&nbsp;", " ")), title = "runtime" }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLElement { innerHTML = IMDBTagline.Value, title = "tagline" }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLElement { innerHTML = IMDBGenre.Value.Replace("|", ", "), title = "genres" }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLElement { innerHTML = TorrentSmartName.SeasonAndEpisode, title = "episode" }.ToString()
                            );

                            w.WriteLine(
                                new IHTMLAnchor
                                {
                                    URL = TorrentLinkTinyLink.Value,
                                    Title = SmartTitle,
                                    innerHTML = new IHTMLImage
                                    {
                                        src = "http://static.thepiratebay.org/img/dl.gif"
                                    }.ToString() + " " + TorrentName.Value
                                }.ToString()
                            );

                            w.WriteLine(" ]]></description>");
                            #endregion

                            #region category
                            if (string.IsNullOrEmpty(TorrentSmartName.SeasonAndEpisode))
                                w.WriteLine("<category>Movies</category>");
                            else
                                w.WriteLine("<category>TV shows</category>");

                            var Genres = IMDBGenre.Value.Split(new[] { '|' });

                            foreach (var g in Genres)
                            {
                                w.WriteLine("<category>" + g + "</category>");
                            }
                            #endregion

                            w.WriteLine("<media:thumbnail url='" + BayImageTinyLink.Value + "' />");
                            w.WriteLine("<media:content " + YouTubeLink.ToAttributeString("url") + @" type='application/x-shockwave-flash' />");
                            w.WriteLine("<media:description type='plain'>" + SmartTitle + " | " + IMDBRaiting.Value + " | " + IMDBTagline.Value + " | " + IMDBGenre.Value + "</media:description>");

                            w.WriteLine("</item>");
                        }
                    };

                    return c.ToFileWhenDirty;
                };
        }
        public Task6_MediaCollector(MyNamedTasks Tasks)
            : base(Tasks, "Task6_MediaCollector")
        {
            this.NamedTasks  = Tasks;
            this.Description = @"
We all information. Now we must collect all entries to generate a feed.
A new feed should not be created faster than 3 hours.
			"            ;

            this.ArgumentHandlers.Add(
                (Task, Arguments) =>
            {
                return(false);
            }
                );

            #region YieldWork
            this.YieldWork =
                (Task, Input) =>
            {
                if (this.Feed.Exists)
                {
                    var AgeHours = (DateTime.Now - this.Feed.LastWriteTime).Hours;


                    if (AgeHours < 12)
                    {
                        this.AppendLog("Media feed is " + AgeHours + " hours old");
                        return(null);
                    }

                    this.AppendLog("We need to update the feed...");
                }
                else
                {
                    this.AppendLog("Creating media feed for the first time");
                }

                using (var w = new StreamWriter(this.Feed.OpenWrite()))
                {
                    w.BaseStream.SetLength(0);

                    w.WriteLine(@"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<rss version='2.0' xmlns:media='http://search.yahoo.com/mrss' xmlns:atom='http://www.w3.org/2005/Atom'>
  <channel>
    <title>zmovies</title>
    <description>zmovies can haz entertainment</description>
    <link>http://zproxy.planet.ee/zmovies</link>
						"                        );

                    foreach (var i in this.ActiveInputPools)
                    {
                        foreach (var j in i.Files)
                        {
                            w.WriteLine(File.ReadAllText(j.FullName));
                        }
                    }

                    w.WriteLine(@"
	  </channel>
</rss>");
                }

                foreach (var i in this.ActiveInputPools)
                {
                    foreach (var j in i.Files)
                    {
                        j.Delete();
                    }
                }

                return(delegate
                {
                    // this will be called if the task is still active
                });
            };
            #endregion
        }
Esempio n. 10
0
        public Task5_CloneMedia(MyNamedTasks Tasks)
            : base(Tasks, "Task5_CloneMedia")
        {
            var NamedTasks = Tasks;

            this.Description = @"
Clone poster to imgbay.
			"            ;

            this.YieldWork =
                (Task, Input) =>
            {
                var Entry = new Entry().FromFile(Input);

                Input.Delete();

                if (string.IsNullOrEmpty(Entry.TinEyeHash))
                {
                    #region TinEyeHash
                    AppendLog("in " + Entry.PirateBay.Name + " without tineye");

                    BasicTinEyeSearch.Search(Entry.IMDB.MediumPosterImage,
                                             h =>
                    {
                        AppendLog("as " + h.Hash);

                        Entry.TinEyeHash = h.Hash;
                        Entry.ToFile(Input);
                    }
                                             );
                    #endregion
                }
                else if (string.IsNullOrEmpty(Entry.BayImgKey))
                {
                    #region BayImgKey
                    AppendLog("in " + Entry.PirateBay.Name + " without bayimg");

                    // do we have it in memory?

                    var BayImg = this.Memory[Entry.TinEyeHash].FirstDirectoryOrDefault();

                    if (BayImg == null)
                    {
                        BasicPirateBayImage.Clone(new Uri(Entry.TinEyeImageLink),
                                                  e =>
                        {
                            Entry.BayImgKey = e.Key;
                            Entry.ToFile(Input);

                            BayImg = this.Memory[Entry.TinEyeHash].CreateSubdirectory(e.Key);
                            AppendLog("in " + Entry.PirateBay.Name + " now known as " + Entry.BayImgKey);
                        }
                                                  );
                    }
                    else
                    {
                        Entry.BayImgKey = BayImg.Name;
                        Entry.ToFile(Input);

                        AppendLog("in " + Entry.PirateBay.Name + " already known as " + Entry.BayImgKey);
                    }
                    #endregion
                }
                else if (string.IsNullOrEmpty(Entry.YouTubeKey))
                {
                    AppendLog("in " + Entry.PirateBay.Name + " looking for video...");



                    BasicGoogleVideoCrawler.Search(Entry.YouTubeQuery,
                                                   (v, src) =>
                    {
                        AppendLog("as " + v);

                        Entry.YouTubeKey = v;
                        Entry.ToFile(Input);
                    }
                                                   );
                }
                else
                {
                    var NextWork = NamedTasks.Task6_MediaCollector.AddWork(5, Entry.PirateBay.Hash);

                    if (NextWork.Exists)
                    {
                        AppendLog("out already exists " + Entry.PirateBay.Name + " as " + Entry.PirateBay.Hash);
                    }
                    else
                    {
                        AppendLog("out " + Entry.PirateBay.Name + " as " + Entry.PirateBay.Hash);


                        Entry.ToFile(NextWork);
                    }
                }

                // if we fail, we this work item will not be retried
                //Input.Delete();

                return(delegate
                {
                    // this will be called if the task is still active
                });
            };
        }
        public Task4_PrepareMedia(MyNamedTasks Tasks)
            : base(Tasks, "Task4_PrepareMedia")
        {
            var NamedTasks = Tasks;

            this.Description = @"
We now should have the torrent, name, seeders, leechers. We must now get the poster, trailer, rating, tagline and hash.
Uses memory to store tineye poster hash.
			"            ;

            this.YieldWork =
                (Task, Input) =>
            {
                var c = Input.ToFieldBuilder();

                FileMappedField
                    TorrentName      = c,
                    TorrentLink      = c,
                    TorrentSize      = c,
                    TorrentComments  = c,
                    IMDBKey          = c,
                    IMDBTitle        = c,
                    IMDBYear         = c,
                    IMDBRaiting      = c,
                    IMDBRuntime      = c,
                    IMDBGenre        = c,
                    IMDBTagline      = c,
                    OMDBSearched     = c,
                    MPDBSearched     = c,
                    PosterLink       = c,
                    TinEyeHash       = c,
                    BayImageLink     = c,
                    BayImageTinyLink = c,
                //BayImageTinySourceLink = c,
                    YouTubeKey          = c,
                    TorrentLinkTinyLink = c,
                    TaskComplete        = c;

                c.FromFile();

                Input.Delete();

                //AppendLog("in " + TorrentName.Value);

                #region IMDBKey
                c[IMDBKey] = delegate
                {
                    var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                    AppendLog("looking for imdb key");

                    BasicIMDBAliasSearch.SearchSingle(TorrentSmartName.Title, TorrentSmartName.Year,
                                                      imdb =>
                    {
                        AppendLog("imdb key found");
                        IMDBKey.Value = imdb.Key;
                    }
                                                      );
                };
                #endregion

                #region IMDBTitle
                c[IMDBTitle] = delegate
                {
                    AppendLog("looking for imdb for details");

                    BasicIMDBCrawler.Search(IMDBKey.Value,
                                            imdb =>
                    {
                        AppendLog("imdb details found");

                        IMDBTitle.Value   = imdb.Title;
                        IMDBYear.Value    = imdb.Year;
                        IMDBRaiting.Value = imdb.UserRating;
                        IMDBRuntime.Value = imdb.Runtime;
                        IMDBTagline.Value = imdb.Tagline;
                        IMDBGenre.Value   = string.Join("|", imdb.Genres);

                        PosterLink.Value = imdb.MediumPosterImage;
                    }
                                            );
                };
                #endregion

                #region PosterLink
                c[PosterLink] = delegate
                {
                    AppendLog("looking for posters...");

                    c[OMDBSearched] = delegate
                    {
                        AppendLog("looking for posters... omdb");
                        OMDBSearched.Value = "true";

                        BasicOMDBCrawler.SearchSingle(IMDBTitle.Value, IMDBYear.Value,
                                                      omdb =>
                        {
                            omdb.GetPoster(
                                poster =>
                            {
                                AppendLog("looking for posters... omdb... found!");
                                PosterLink.Value = poster;
                            }
                                );
                        }
                                                      );
                    };

                    c[MPDBSearched] = delegate
                    {
                        AppendLog("looking for posters... mpdb");

                        BasicMPDBCrawler.SearchSingle(IMDBTitle.Value, IMDBYear.Value,
                                                      mpdb =>
                        {
                            mpdb.GetPoster(
                                poster =>
                            {
                                AppendLog("looking for posters... mpdb... found!");
                                PosterLink.Value = poster;
                            }
                                );
                        }
                                                      );
                    };
                };
                #endregion

                #region TinEyeHash
                c[TinEyeHash] = delegate
                {
                    AppendLog("looking for tineye hash...");
                    BasicTinEyeSearch.Search(PosterLink.Value,
                                             tineye =>
                    {
                        AppendLog("looking for tineye hash... found");
                        TinEyeHash.Value = tineye.Hash;

                        var Memory = this.Memory[TinEyeHash.Value].FirstDirectoryOrDefault();

                        if (Memory != null)
                        {
                            BayImageLink.Value = "_";
                            //BayImageTinyLink.Value = "_";
                            BayImageTinyLink.Value = "http://tinyurl.com/" + Memory.Name;
                        }
                    }
                                             );
                };
                #endregion

                #region BayImageLink
                c[BayImageLink] = delegate
                {
                    AppendLog("updating bay image...");

                    var tineye = new BasicTinEyeSearch.Entry {
                        Hash = TinEyeHash.Value
                    };

                    BasicPirateBayImage.Clone(tineye.QueryLink.ToUri(),
                                              bayimg =>
                    {
                        AppendLog("updating bay image... done");
                        BayImageLink.Value = bayimg.Image.ToString();
                    }
                                              );
                };
                #endregion

                #region BayImageTinyLink
                c[BayImageTinyLink] = delegate
                {
                    AppendLog("looking for tinyurl...");
                    BasicTinyURLCrawler.Search(BayImageLink.Value,
                                               tinyurl =>
                    {
                        AppendLog("looking for tinyurl... found");
                        BayImageTinyLink.Value = tinyurl.Alias;
                    }
                                               );
                };
                #endregion

                //#region BayImageTinySourceLink
                //c[BayImageTinySourceLink] = delegate
                //{
                //    BasicTinyURLCrawler.Search("http://i.tinysrc.mobi/" + BayImageTinyLink.Value,
                //        tinyurl =>
                //        {
                //            BayImageTinySourceLink.Value = tinyurl.Alias;

                //            this.Memory[TinEyeHash.Value].CreateSubdirectory(tinyurl.AliasKey);
                //        }
                //    );
                //};
                //#endregion

                #region YouTubeKey
                c[YouTubeKey] = delegate
                {
                    var Query            = IMDBTitle.Value;
                    var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                    var SeasonAndEpisode = TorrentSmartName.SeasonAndEpisode;

                    if (!string.IsNullOrEmpty(SeasonAndEpisode))
                    {
                        Query += " " + SeasonAndEpisode;
                    }
                    else
                    {
                        Query += " trailer";
                    }


                    BasicGoogleVideoCrawler.Search(Query,
                                                   (key, src) =>
                    {
                        YouTubeKey.Value = key;
                    }
                                                   );
                };
                #endregion

                #region TorrentLinkTinyLink
                c[TorrentLinkTinyLink] = delegate
                {
                    BasicTinyURLCrawler.Search(TorrentLink.Value,
                                               torrent =>
                    {
                        TorrentLinkTinyLink.Value = torrent.Alias;
                    }
                                               );
                };
                #endregion

                c[TaskComplete] = delegate
                {
                    var TorrentSmartName = new BasicFileNameParser(TorrentName.Value);

                    var YouTubeLink  = "http://www.youtube.com/v/" + YouTubeKey + @"&hl=en&fs=1";
                    var YouTubeImage = "http://img.youtube.com/vi/" + YouTubeKey + @"/0.jpg";

                    var SmartTitle = "";

                    if (!string.IsNullOrEmpty(TorrentSmartName.SeasonAndEpisode))
                    {
                        SmartTitle = "TV show: " + IMDBTitle.Value + " " + TorrentSmartName.SeasonAndEpisode + " " + IMDBYear.Value;
                    }
                    else
                    {
                        SmartTitle = "Movie: " + IMDBTitle.Value + " " + IMDBYear.Value;
                    }

                    var NextWork = NamedTasks.Task6_MediaCollector.AddWork(5, Input.Name);

                    using (var w = new StreamWriter(NextWork.OpenWrite()))
                    {
                        w.WriteLine("<item>");
                        w.WriteLine("<title>" + SmartTitle + "</title>");
                        w.WriteLine("<link>" + TorrentLinkTinyLink.Value + "</link>");

                        #region description
                        w.WriteLine("<description><![CDATA[");

                        w.WriteLine(@"
	<object width='640' height='385'>
		<param name='movie' "         + YouTubeLink.ToAttributeString("value") + @"></param>
		<param name='allowFullScreen' value='true'></param>
		<param name='allowscriptaccess' value='always'></param>
		<embed "         + YouTubeLink.ToAttributeString("src") + @" type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='640' height='385'></embed>
	</object>
							"                            );

                        w.WriteLine(
                            new IHTMLAnchor
                        {
                            Style = new IStyle {
                                @float = "left"
                            },
                            URL       = YouTubeLink,
                            innerHTML = new IHTMLImage
                            {
                                alt   = YouTubeKey.Value,
                                align = "left",
                                src   = YouTubeImage
                            }
                        }.ToString()
                            );

                        w.WriteLine(
                            new IHTMLAnchor
                        {
                            URL       = BasicIMDBCrawler.ToLink(IMDBKey.Value),
                            Title     = SmartTitle,
                            innerHTML = new IHTMLImage
                            {
                                align = "right",
                                src   = BayImageTinyLink.Value,
                            }
                        }.ToString()
                            );

                        w.WriteLine(
                            "<h2>" + SmartTitle.ToLink("http://piratebay.org" + TorrentComments.Value) + "</h2>"
                            );

                        w.WriteLine(
                            new IHTMLElement {
                            innerHTML = IMDBRaiting.Value, title = "raiting"
                        }.ToString()
                            );

                        w.WriteLine(
                            new IHTMLElement {
                            innerHTML = (IMDBRuntime.Value + ", " + TorrentSize.Value.Replace("&nbsp;", " ")), title = "runtime"
                        }.ToString()
                            );

                        w.WriteLine(
                            new IHTMLElement {
                            innerHTML = IMDBTagline.Value, title = "tagline"
                        }.ToString()
                            );

                        w.WriteLine(
                            new IHTMLElement {
                            innerHTML = IMDBGenre.Value.Replace("|", ", "), title = "genres"
                        }.ToString()
                            );

                        w.WriteLine(
                            new IHTMLElement {
                            innerHTML = TorrentSmartName.SeasonAndEpisode, title = "episode"
                        }.ToString()
                            );



                        w.WriteLine(
                            new IHTMLAnchor
                        {
                            URL       = TorrentLinkTinyLink.Value,
                            Title     = SmartTitle,
                            innerHTML = new IHTMLImage
                            {
                                src = "http://static.thepiratebay.org/img/dl.gif"
                            }.ToString() + " " + TorrentName.Value
                        }.ToString()
                            );


                        w.WriteLine(" ]]></description>");
                        #endregion

                        #region category
                        if (string.IsNullOrEmpty(TorrentSmartName.SeasonAndEpisode))
                        {
                            w.WriteLine("<category>Movies</category>");
                        }
                        else
                        {
                            w.WriteLine("<category>TV shows</category>");
                        }

                        var Genres = IMDBGenre.Value.Split(new[] { '|' });

                        foreach (var g in Genres)
                        {
                            w.WriteLine("<category>" + g + "</category>");
                        }
                        #endregion

                        w.WriteLine("<media:thumbnail url='" + BayImageTinyLink.Value + "' />");
                        w.WriteLine("<media:content " + YouTubeLink.ToAttributeString("url") + @" type='application/x-shockwave-flash' />");
                        w.WriteLine("<media:description type='plain'>" + SmartTitle + " | " + IMDBRaiting.Value + " | " + IMDBTagline.Value + " | " + IMDBGenre.Value + "</media:description>");

                        w.WriteLine("</item>");
                    }
                };

                return(c.ToFileWhenDirty);
            };
        }
        public Task6_MediaCollector(MyNamedTasks Tasks)
            : base(Tasks, "Task6_MediaCollector")
        {
            this.NamedTasks = Tasks;
            this.Description = @"
            We all information. Now we must collect all entries to generate a feed.
            A new feed should not be created faster than 3 hours.
            ";

            this.ArgumentHandlers.Add(
                (Task, Arguments) =>
                {

                    return false;
                }
            );

            #region YieldWork
            this.YieldWork =
                (Task, Input) =>
                {
                    if (this.Feed.Exists)
                    {
                        var AgeHours = (DateTime.Now - this.Feed.LastWriteTime).Hours;

                        if (AgeHours < 12)
                        {
                            this.AppendLog("Media feed is " + AgeHours + " hours old");
                            return null;
                        }

                        this.AppendLog("We need to update the feed...");
                    }
                    else
                        this.AppendLog("Creating media feed for the first time");

                    using (var w = new StreamWriter(this.Feed.OpenWrite()))
                    {
                        w.BaseStream.SetLength(0);

                        w.WriteLine(@"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
            <rss version='2.0' xmlns:media='http://search.yahoo.com/mrss' xmlns:atom='http://www.w3.org/2005/Atom'>
              <channel>
            <title>zmovies</title>
            <description>zmovies can haz entertainment</description>
            <link>http://zproxy.planet.ee/zmovies</link>
                        ");

                        foreach (var i in this.ActiveInputPools)
                        {
                            foreach (var j in i.Files)
                            {
                                w.WriteLine(File.ReadAllText(j.FullName));
                            }
                        }

                        w.WriteLine(@"
              </channel>
            </rss>");

                    }

                    foreach (var i in this.ActiveInputPools)
                    {
                        foreach (var j in i.Files)
                        {
                            j.Delete();
                        }
                    }

                    return delegate
                    {
                        // this will be called if the task is still active
                    };
                };
            #endregion
        }