-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvdb.php
executable file
·155 lines (120 loc) · 3.05 KB
/
tvdb.php
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
class Tvdb
{
private $workers = null;
private $series = null;
public function __construct()
{
$this->series = array();
$this->workers = array();
}
public function __destruct()
{
$this->series = null;
}
public function downloads_series_data( $path, $dirs )
{
print "Fetching TVDB metadata...\n";
$current_date = new DateTime();
$width = 50;
foreach ( $dirs as $directory )
{
print ".";
$width++;
if( $width % 50 == 0) print ( $width - 50 ) . "\n";
$this->wait_limit_workers( 3 );
$current_series = substr( $directory , strlen( $path ) + 1 );
$this->workers[ $current_series ] = new TvdbWorker( $current_series );
$this->workers[ $current_series ]->start();
}
print ($width - 50) . "\n";
$this->wait_for_workers();
$this->parse_series_data();
print "Done.\n";
}
public function parse_series_data()
{
print "Parsing TVDB metadata...\n";
$current_date = new DateTime();
$width = 50;
$notdone = 1;
foreach( $this->workers as $worker )
{
if( $worker->is_processed() == false )
{
$xml = $worker->get_xml();
$nseries = new Series( $worker->get_series() );
$this->series[ $worker->get_series() ] = $nseries;
if( $xml !== null )
{
$tree = new SimpleXMLElement( $xml );
$episodes = $tree->xpath( './/Episode' );
foreach( $episodes as $episode )
{
$epname = trim( $episode->xpath( './/EpisodeName' )[0] );
$epseasonnum = trim( $episode->xpath( './/SeasonNumber' )[0] );
$epaireddate = trim( $episode->xpath( './/FirstAired' )[0] );
$epepisodenum = trim( $episode->xpath( './/EpisodeNumber' )[0] );
$airdate = $epaireddate == "" ? new DateTime( "2050-01-01" ) : new DateTime( $epaireddate );
if( $airdate <= $current_date && $epseasonnum > 0 )
{
$season = $nseries->get_season( $epseasonnum );
$newepisode = new Episode( array( $epepisodenum , $epname ) );
$season->add_episode( $newepisode );
}
}
}
}
}
}
private function wait_for_workers()
{
sleep( 1 );
print "Waiting for threads";
$count = 2;
while( $count > 1 )
{
$count = 0;
foreach( $this->workers as $worker )
{
if( $worker->is_working() == true )
{
$count++;
}
}
usleep( 50000 );
print ".";
}
usleep( 50000 );
print "\n";
}
private function wait_limit_workers( $num )
{
$count = $num + 1;
while( $count > $num )
{
$count = 0;
foreach( $this->workers as $worker )
{
if( $worker->is_working() == true )
{
$count++;
}
}
usleep( 20000 );
}
usleep( 20000 );
}
public function get_series()
{
return $this->series;
}
public function free_workers()
{
for( $x =0; $x < count( $this->workers ); $x++ )
{
unset( $this->workers[ $x ] );
}
gc_collect_cycles();
}
}