Class: MediaWikiLyrics
- Defined in:
- lib/wiki_lyrics/mediawikilyrics.rb
Defined Under Namespace
Classes: AlbumData, SongData, TrackData
Constant Summary collapse
- @@NAME =
"WL".freeze()
- @@VERSION =
"0.13.4".freeze()
- @@AUTH_INTERVAL =
check authorization every hour (it’s only really checked if the user tries to submit something)
60*60
- @@REDIRECT_FOLLOWUPS =
3
- @@SEARCH_RESULT_TITLE =
"title".freeze()
- @@SEARCH_RESULT_URL =
"url".freeze()
- @@FIND_TEMPLATE_START =
0
- @@FIND_TEMPLATE_NAME =
1
- @@FIND_TEMPLATE_PARAM =
2
- @@FIND_TEMPLATE_PARAM_VALUE =
3
- @@months =
DATE FUNCTIONS
[ "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE", "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER" ]
Instance Attribute Summary collapse
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Attributes inherited from Lyrics
Class Method Summary collapse
- .build_album_edit_url(artist, album, year, cleanup = true) ⇒ Object
- .build_album_link(artist, album, year, cleanup = true) ⇒ Object
- .build_album_rawdata_url(artist, album, year, cleanup = true) ⇒ Object
- .build_album_search_url(artist, album, year) ⇒ Object
- .build_album_url(artist, album, year, cleanup = true) ⇒ Object
- .build_date(year, month, day) ⇒ Object
- .build_link(article, display = nil) ⇒ Object
- .build_rawdata_url(article) ⇒ Object
- .build_song_add_url(request, cleanup = true) ⇒ Object
- .build_song_edit_url(artist, title, cleanup = true) ⇒ Object
- .build_song_link(artist, title, cleanup = true) ⇒ Object
- .build_song_rawdata_url(artist, title, cleanup = true) ⇒ Object
- .build_song_search_url(artist, title) ⇒ Object
- .build_song_url(artist, title, cleanup = true) ⇒ Object
- .build_url(article) ⇒ Object
-
.cleanup_article(article, capitalize = true) ⇒ Object
GENERAL FUNCTIONS.
- .cleanup_title_token(title, downcase = false) ⇒ Object
- .fetch_content_page(page_url, follow_redirects = @@REDIRECT_FOLLOWUPS) ⇒ Object
- .find_album_art_url(artist, album, year) ⇒ Object
- .find_album_page_url(artist, album, year) ⇒ Object
- .find_song_page_url(artist, title) ⇒ Object
- .get_sort_letter(title) ⇒ Object
- .get_sort_name(title) ⇒ Object
- .get_sort_name!(title) ⇒ Object
-
.parse_album_link(link) ⇒ Object
ALBUM FUNCTIONS.
- .parse_album_url(url) ⇒ Object
- .parse_date(date) ⇒ Object
- .parse_link(link) ⇒ Object
- .parse_search_results(page_body, content_matches = false) ⇒ Object
-
.parse_song_link(link) ⇒ Object
SONG FUNCTIONS.
- .parse_song_url(url) ⇒ Object
- .parse_template(template_text) ⇒ Object
- .parse_url(url) ⇒ Object
- .prepare_image_file(image_path, size_limit = 153600) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
- #authorized? ⇒ Boolean
- #build_album_art_description(artist, album, year, cleanup = true) ⇒ Object
-
#build_album_art_name(artist, album, year, extension = "jpg", cleanup = true) ⇒ Object
ALBUM ART FUNCTIONS.
- #build_album_edit_url(artist, album, year, cleanup = true) ⇒ Object
- #build_album_link(artist, album, year, cleanup = true) ⇒ Object
- #build_album_page(reviewed, artist, album, year, month, day, tracks, album_art) ⇒ Object
- #build_album_rawdata_url(artist, album, year, cleanup = true) ⇒ Object
- #build_album_search_url(artist, album, year) ⇒ Object
- #build_album_url(artist, album, year, cleanup = true) ⇒ Object
- #build_link(article, display = nil) ⇒ Object
- #build_lyrics_fetch_data(request) ⇒ Object
- #build_rawdata_url(article) ⇒ Object
- #build_song_add_url(request, cleanup = true) ⇒ Object
- #build_song_edit_url(artist, title, cleanup = true) ⇒ Object
- #build_song_link(artist, title, cleanup = true) ⇒ Object
- #build_song_page(reviewed, artist, title, album, year, credits, lyricist, lyrics) ⇒ Object
- #build_song_rawdata_url(artist, title, cleanup = true) ⇒ Object
- #build_song_search_url(artist, title) ⇒ Object
- #build_song_url(artist, title, cleanup = true) ⇒ Object
- #build_suggestions_fetch_data(request) ⇒ Object
- #build_tracks(album_data) ⇒ Object
- #build_url(article) ⇒ Object
- #cleanup_article(article) ⇒ Object
- #cleanup_title_token(title, downcase = false) ⇒ Object
- #cleanup_title_token!(title, downcase = false) ⇒ Object
- #control_page ⇒ Object
- #fetch_content_page(url, follow_redirects = @@REDIRECT_FOLLOWUPS) ⇒ Object
- #fetch_lyrics_page(fetch_data) ⇒ Object
- #find_album_art_name(artist, album, year) ⇒ Object
- #find_album_art_url(artist, album, year) ⇒ Object
- #find_album_page_url(artist, album, year) ⇒ Object
- #find_song_page_url(artist, title) ⇒ Object
- #get_session_params ⇒ Object
- #get_sort_letter(title) ⇒ Object
- #get_sort_name(title) ⇒ Object
- #get_sort_name!(title) ⇒ Object
-
#initialize(cleanup_lyrics = true, logger = nil, username = nil, password = nil) ⇒ MediaWikiLyrics
constructor
A new instance of MediaWikiLyrics.
- #logged_in? ⇒ Boolean
- #login(username = @username, password = @password, force = false) ⇒ Object
- #logout ⇒ Object
- #lyrics_from_url(request, url) ⇒ Object
- #lyrics_page_valid?(request, page_body, page_url) ⇒ Boolean
- #parse_album_link(link) ⇒ Object
- #parse_album_url(url) ⇒ Object
- #parse_link(link) ⇒ Object
- #parse_search_results(page_body, content_matches = false) ⇒ Object
- #parse_song_link(link) ⇒ Object
- #parse_song_url(url) ⇒ Object
- #parse_suggestions(request, page_body, page_url) ⇒ Object
- #parse_template(template) ⇒ Object
- #parse_url(url) ⇒ Object
- #prepare_image_file(image_path, size_limit = 153600) ⇒ Object
- #restore_session(session_file) ⇒ Object
- #restore_session_params(username, password, cookie, last_auth_check = 0, authorized = false) ⇒ Object
- #save_session(session_file) ⇒ Object
-
#submit_album_page(album_data, image_path = nil, allow_page_overwrite = true, skip_initial_page_search = false, show_review_dialog = true, must_review = false) ⇒ Object
use skip_initial_page_search when you know the page doesn’t exists (i.e. when you have already searched it).
- #submit_page(page_url, page_content, summary = "", watch = true, retries = 1, auto_login = true) ⇒ Object
- #submit_redirect_page(page_url, link, summary = nil) ⇒ Object
-
#submit_song_page(song_data, edit_url = nil, skip_initial_page_search = false, show_review_dialog = true, must_review = false) ⇒ Object
if edit_url is not nil, it’s assumed that we’re trying to overwrite (edit) a page, otherwise it’s assummed that we’re trying to create a new page and so overwritting won’t be allowed use skip_initial_page_search when you know the page doesn’t exists (i.e. when you have already searched it).
- #upload_cover_image(image_path, artist, album, year, watch = true) ⇒ Object
- #upload_file(src_file, dst_file, mime_type, description = "", watch = true, auto_login = true) ⇒ Object
- #version ⇒ Object
Methods inherited from Lyrics
#build_google_feeling_lucky_url, build_google_feeling_lucky_url, #decrease_tabulation_level, #fetch_suggestions_page, #increase_tabulation_level, #known_url?, known_url?, #log, #log?, #logger=, #lyrics_direct_search, #lyrics_from_suggestions, #lyrics_full_search, lyrics_test_data, #lyrics_test_data, #notify, #site_host, #site_name, #suggestions, #suggestions_page_valid?, #suggestions_test_data, suggestions_test_data, #verbose_log?
Constructor Details
#initialize(cleanup_lyrics = true, logger = nil, username = nil, password = nil) ⇒ MediaWikiLyrics
Returns a new instance of MediaWikiLyrics.
147 148 149 150 151 152 153 154 155 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 147 def initialize( cleanup_lyrics=true, logger=nil, username=nil, password=nil ) super( cleanup_lyrics, logger ) @logged_in = false @cookie = nil @last_auth_check = 0 # last time we checked the control page @authorized = false @username = username ? username.clone().freeze() : nil @password = password ? password.clone().freeze() : nil end |
Instance Attribute Details
#password ⇒ Object (readonly)
Returns the value of attribute password.
145 146 147 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 145 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
145 146 147 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 145 def username @username end |
Class Method Details
.build_album_edit_url(artist, album, year, cleanup = true) ⇒ Object
1337 1338 1339 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1337 def MediaWikiLyrics.build_album_edit_url( artist, album, year, cleanup=true ) return build_album_url( artist, album, year, cleanup ) + "&action=edit" end |
.build_album_link(artist, album, year, cleanup = true) ⇒ Object
1291 1292 1293 1294 1295 1296 1297 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1291 def MediaWikiLyrics.build_album_link( artist, album, year, cleanup=true ) if cleanup artist = cleanup_title_token( artist ) album = cleanup_title_token( album ) end return build_link( "#{artist}:#{album} (#{year})" ) end |
.build_album_rawdata_url(artist, album, year, cleanup = true) ⇒ Object
1329 1330 1331 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1329 def MediaWikiLyrics.build_album_rawdata_url( artist, album, year, cleanup=true ) return build_album_url( artist, album, year, cleanup ) + "&action=raw" end |
.build_album_search_url(artist, album, year) ⇒ Object
1345 1346 1347 1348 1349 1350 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1345 def MediaWikiLyrics.build_album_search_url( artist, album, year ) artist = cleanup_title_token( artist ) album = cleanup_title_token( album ) search_string = CGI.escape( "#{artist}:#{album} (#{year})" ) return "http://#{site_host()}/index.php?redirs=1&search=#{search_string}&fulltext=Search&limit=500" end |
.build_album_url(artist, album, year, cleanup = true) ⇒ Object
1317 1318 1319 1320 1321 1322 1323 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1317 def MediaWikiLyrics.build_album_url( artist, album, year, cleanup=true ) if cleanup artist = cleanup_title_token( artist ) album = cleanup_title_token( album ) end return build_url( "#{artist}:#{album} (#{year})" ) end |
.build_date(year, month, day) ⇒ Object
1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1443 def MediaWikiLyrics.build_date( year, month, day ) year, month, day = year.to_i(), month.to_i(), day.to_i() if month != 0 month = @@months[month-1].to_s() month = month.slice( 0..0 ).upcase() + month.slice( 1..-1 ).downcase() end if day != 0 && month != 0 && year != 0 return "#{month} #{day}, #{year}" elsif month != 0 && year != 0 return "#{month}, #{year}" elsif year != 0 return year.to_s() else return "" end end |
.build_link(article, display = nil) ⇒ Object
1114 1115 1116 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1114 def MediaWikiLyrics.build_link( article, display=nil ) return display ? "[[#{article}|#{display}]]" : "[[#{article}]]" end |
.build_rawdata_url(article) ⇒ Object
1143 1144 1145 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1143 def MediaWikiLyrics.build_rawdata_url( article ) return build_url( article ) + "&action=raw" end |
.build_song_add_url(request, cleanup = true) ⇒ Object
1224 1225 1226 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1224 def MediaWikiLyrics.build_song_add_url( request, cleanup=true ) return build_song_url( request.artist, request.title, cleanup ) + "&action=edit" end |
.build_song_edit_url(artist, title, cleanup = true) ⇒ Object
1216 1217 1218 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1216 def MediaWikiLyrics.build_song_edit_url( artist, title, cleanup=true ) return build_song_url( artist, title, cleanup ) + "&action=edit" end |
.build_song_link(artist, title, cleanup = true) ⇒ Object
1170 1171 1172 1173 1174 1175 1176 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1170 def MediaWikiLyrics.build_song_link( artist, title, cleanup=true ) if cleanup artist = cleanup_title_token( artist ) title = cleanup_title_token( title ) end return build_link( "#{artist}:#{title}" ) end |
.build_song_rawdata_url(artist, title, cleanup = true) ⇒ Object
1208 1209 1210 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1208 def MediaWikiLyrics.build_song_rawdata_url( artist, title, cleanup=true ) return build_song_url( artist, title, cleanup ) + "&action=raw" end |
.build_song_search_url(artist, title) ⇒ Object
1232 1233 1234 1235 1236 1237 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1232 def MediaWikiLyrics.build_song_search_url( artist, title ) artist = cleanup_title_token( artist ) title = cleanup_title_token( title ) search_string = CGI.escape( "#{artist}:#{title}" ) return "http://#{site_host()}/index.php?redirs=1&search=#{search_string}&fulltext=Search&limit=500" end |
.build_song_url(artist, title, cleanup = true) ⇒ Object
1196 1197 1198 1199 1200 1201 1202 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1196 def MediaWikiLyrics.build_song_url( artist, title, cleanup=true ) if cleanup artist = cleanup_title_token( artist ) title = cleanup_title_token( title ) end return build_url( "#{artist}:#{title}" ) end |
.build_url(article) ⇒ Object
1134 1135 1136 1137 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1134 def MediaWikiLyrics.build_url( article ) # return "http://#{site_host()}/index.php?title=#{CGI.escape( article )}" return "http://#{site_host()}/index.php?title=#{CGI.escape( article.gsub( " ", "_" ) )}" end |
.cleanup_article(article, capitalize = true) ⇒ Object
GENERAL FUNCTIONS
1080 1081 1082 1083 1084 1085 1086 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1080 def MediaWikiLyrics.cleanup_article( article, capitalize=true ) article = article.gsub( "_", " " ) article.strip!() article.squeeze!( " " ) Strings.capitalize!( article, false, true ) if capitalize return article end |
.cleanup_title_token(title, downcase = false) ⇒ Object
1007 1008 1009 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1007 def MediaWikiLyrics.cleanup_title_token( title, downcase=false ) return cleanup_title_token!( String.new( title ), downcase ) end |
.fetch_content_page(page_url, follow_redirects = @@REDIRECT_FOLLOWUPS) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 184 def MediaWikiLyrics.fetch_content_page( page_url, follow_redirects=@@REDIRECT_FOLLOWUPS ) response, page_url = HTTP.fetch_page_get( page_url ) page_url = page_url.sub( "&action=raw", "" ) page_body = response ? response.body() : nil return nil, page_url if page_body == nil # work around redirects pages count = 0 articles = [ parse_url( page_url ) ] while ( count < follow_redirects && (md = /#redirect ?('''|)\[\[([^\]]+)\]\]('''|)/i.match( page_body )) ) article = cleanup_article( md[2] ) if articles.include?( article ) # circular redirect found return nil, url else articles << article end response, page_url = HTTP.fetch_page_get( build_rawdata_url( article ) ) page_url = page_url.sub( "&action=raw", "" ) page_body = response ? response.body() : nil return nil, page_url if page_body == nil count += 1 end page_body = "" if page_body.strip() == "/* Empty */" return page_body, page_url end |
.find_album_art_url(artist, album, year) ⇒ Object
1400 1401 1402 1403 1404 1405 1406 1407 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1400 def MediaWikiLyrics.find_album_art_url( artist, album, year ) if album_art_name = find_album_art_name( artist, album, year ) album_art_name.gsub!( " ", "_" ) return "http://#{site_host()}/index.php?title=Image:#{CGI.escape(album_art_name)}" else return nil end end |
.find_album_page_url(artist, album, year) ⇒ Object
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1356 def MediaWikiLyrics.find_album_page_url( artist, album, year ) url = build_album_rawdata_url( artist, album, year ) body, url = fetch_content_page( url ) if ! Strings.empty?( body ) # page exists return url else artist = Strings.normalize( cleanup_title_token( artist ) ) album = Strings.normalize( cleanup_title_token( album ) ) normalized_target = "#{artist}:#{album} #{year}" # NOTE: normalize removes the parenthesis response, url = HTTP.fetch_page_get( build_album_search_url( artist, album, year ) ) return nil if response == nil || response.body() == nil parse_search_results( response.body(), true ).each() do |result| normalized_result = result[@@SEARCH_RESULT_TITLE].split( ":" ).each() do |token| Strings.normalize!( token ) end.join( ":" ) return result[@@SEARCH_RESULT_URL] if normalized_target == normalized_result end return nil end end |
.find_song_page_url(artist, title) ⇒ Object
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1243 def MediaWikiLyrics.find_song_page_url( artist, title ) url = build_song_rawdata_url( artist, title ) body, url = fetch_content_page( url ) if ! Strings.empty?( body ) # page exists return url else artist = Strings.normalize( cleanup_title_token( artist ) ) title = Strings.normalize( cleanup_title_token( title ) ) normalized_target = "#{artist}:#{title}" response, url = HTTP.fetch_page_get( build_song_search_url( artist, title ) ) return nil if response == nil parse_search_results( response.body(), true ).each() do |result| normalized_result = result[@@SEARCH_RESULT_TITLE].split( ":" ).each() do |token| Strings.normalize!( token ) end.join( ":" ) return result[@@SEARCH_RESULT_URL] if normalized_target == normalized_result end return nil end end |
.get_sort_letter(title) ⇒ Object
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1061 def MediaWikiLyrics.get_sort_letter( title ) title = get_sort_name( title ) title = title.strip() if title.index( /^[0-9]/ ) == 0 return "0-9" else # return title.slice( 0, 1 ) return title.unpack( "U*" ).slice( 0, 1 ).pack( "U*" ) end end |
.get_sort_name(title) ⇒ Object
1019 1020 1021 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1019 def MediaWikiLyrics.get_sort_name( title ) return get_sort_name!( String.new( title ) ) end |
.get_sort_name!(title) ⇒ Object
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1027 def MediaWikiLyrics.get_sort_name!( title ) title.gsub!( /\[[^\]\[]*\]/, "" ) Strings.downcase!( title ) title.gsub!( /[·\.,;:"`´¿\?¡!\(\)\[\]{}<>#\$\+\*%\^]/, "" ) title.gsub!( /[\\\/_-]/, " " ) title.gsub!( "&", "and" ) title.squeeze!( " " ) title.strip!() title.gsub!( /^a /, "" ) # NOTE: only valid for English title.gsub!( /^an /, "" ) title.gsub!( /^the /, "" ) title.gsub!( /^el /, "" ) title.gsub!( /^le /, "" ) title.gsub!( /^la /, "" ) title.gsub!( /^l'([aeiou])/, "\\1" ) title.gsub!( /^los /, "" ) title.gsub!( /^las /, "" ) title.gsub!( /^les /, "" ) title.gsub!( "'", "" ) Strings.remove_vocal_accents!( title ) Strings.titlecase!( title, false, false ) return title end |
.parse_album_link(link) ⇒ Object
ALBUM FUNCTIONS
1277 1278 1279 1280 1281 1282 1283 1284 1285 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1277 def MediaWikiLyrics.parse_album_link( link ) article, display = parse_link( link ) return nil, nil, nil if article == nil if (md = /^([^:]+):(.+) \(([0-9]{4,4})\)$/.match( article )) return md[1], md[2], md[3] else return nil, nil, nil end end |
.parse_album_url(url) ⇒ Object
1303 1304 1305 1306 1307 1308 1309 1310 1311 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1303 def MediaWikiLyrics.parse_album_url( url ) article = parse_url( url ) return nil, nil, nil if article == nil if (md = /^([^:]+):(.+) \(([0-9]{4,4})\)$/.match( article )) return md[1], md[2], md[3] else return nil, nil, nil end end |
.parse_date(date) ⇒ Object
1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1421 def MediaWikiLyrics.parse_date( date ) day, month, year = nil, nil, nil if (md = /^(#{@@months.join( "|" )})( \d\d?|), (\d\d\d\d)$/i.match( date )) month, day, year = @@months.index( Strings.upcase( md[1] ) ) + 1, md[2].strip().to_i(), md[3].to_i() elsif (md = /^(\d\d? |)(#{@@months.join( "|" )}) (\d\d\d\d)$/i.match( date )) day, month, year = md[1].strip().to_i(), @@months.index( Strings.upcase( md[2] ) ) + 1, md[3].to_i() elsif /^(\d\d\d\d)-(\d\d)-(\d\d)$/.match( date ) year, month, day = md[1].to_i(), md[2].to_i(), md[3].to_i() elsif (md = /^(\d\d\d\d)$/.match( date )) year = md[1].to_i() end year = nil if year == 0 month = nil if month == 0 day = nil if day == 0 return year, month, day end |
.parse_link(link) ⇒ Object
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1092 def MediaWikiLyrics.parse_link( link ) if (md = /^ *\[\[([^\|\]]+)\]\] *$/.match( link )) article = cleanup_article( md[1] ) return article, article if ! article.empty? end if (md = /^ *\[\[([^\|\]]+)\|([^\]]*)\]\] *$/.match( link )) article = cleanup_article( md[1] ) if ! article.empty? display = cleanup_article( md[2], false ) return article, display if ! display.empty? end end return nil, nil end |
.parse_search_results(page_body, content_matches = false) ⇒ Object
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 796 def MediaWikiLyrics.parse_search_results( page_body, content_matches=false ) results = [] return results if page_body == nil page_body.tr_s!( " \n\r\t", " " ) page_body.gsub!( /\ ?<\/?span[^>]*> ?/, "" ) return results if ! page_body.sub!( /^.*<a name="(Article|Page)_title_matches">/, "" ) && ! page_body.sub!( /^.*<a name="No_(article|page)_title_matches">/, "" ) page_body.sub!( /<a name="No_(article|page)_text_matches">.*$/, "" ) if ! content_matches return results if ! page_body.gsub!( /<form id="powersearch" method="get" action="[^"]+">.*$/, "" ) page_body.gsub!( /<\/[uo]l> ?<p( [^>]*|)>View \(previous .*$/, "" ) page_body.split( "<li>" ).each() do |entry| if (md = /<a href="([^"]*index\.php\/|[^"]*index\.php\?title=|\/)([^"]*)" title="([^"]+)"/.match( entry )) result = { @@SEARCH_RESULT_URL => "http://#{site_host()}/index.php?title=#{md[2]}", @@SEARCH_RESULT_TITLE => md[3] } results << result if ! content_matches || ! results.include?( result ) end end return results end |
.parse_song_link(link) ⇒ Object
SONG FUNCTIONS
1156 1157 1158 1159 1160 1161 1162 1163 1164 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1156 def MediaWikiLyrics.parse_song_link( link ) article, display = parse_link( link ) return nil, nil if article == nil if (md = /^([^:]+):(.+)$/.match( article )) return md[1], md[2] else return nil, nil end end |
.parse_song_url(url) ⇒ Object
1182 1183 1184 1185 1186 1187 1188 1189 1190 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1182 def MediaWikiLyrics.parse_song_url( url ) article = parse_url( url ) return nil, nil if article == nil if (md = /^([^:]+):(.+)$/.match( article )) return md[1], md[2] # artist, song title else return nil, nil end end |
.parse_template(template_text) ⇒ Object
982 983 984 985 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 982 def MediaWikiLyrics.parse_template( template_text ) index, template_data = parse_template_rec( template_text, 0 ) return template_data end |
.parse_url(url) ⇒ Object
1122 1123 1124 1125 1126 1127 1128 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1122 def MediaWikiLyrics.parse_url( url ) if (md = /(https?:\/\/#{site_host()}\/|)(index.php\?title=|wiki\/|)([^&]+)(&.*|)$/.match( url )) return cleanup_article( CGI.unescape( md[3] ) ) # article title else return nil end end |
.prepare_image_file(image_path, size_limit = 153600) ⇒ Object
991 992 993 994 995 996 997 998 999 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 991 def MediaWikiLyrics.prepare_image_file( image_path, size_limit=153600 ) 4.times() do |trynumb| system( "convert", "-quality", (100-trynumb*10).to_s(), image_path, "/tmp/AlbumArt.jpg" ) return nil, nil if $? != 0 size = FileTest.size?( "/tmp/AlbumArt.jpg" ) return "/tmp/AlbumArt.jpg", "image/jpeg" if (size ? size : 0) <= size_limit end return nil, nil end |
.version ⇒ Object
137 138 139 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 137 def MediaWikiLyrics.version() return @@VERSION end |
Instance Method Details
#authorized? ⇒ Boolean
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 306 def () # debug( "AUTHORIZED INITIAL VALUE: #{@authorized}" ) return @authorized if (Time.new().to_i() - @last_auth_check) < @@AUTH_INTERVAL body, url = fetch_content_page( build_rawdata_url( control_page() ) ) return false if ! body # debug( "CONTROL PAGE:\n#{body}" ) @last_auth_check = Time.new().to_i() control_data = {} body.split( "\n" ).each() do |line| if (md = /^([^=]+)=(.*)$/.match( line )) control_data[md[1].strip()] = md[2].strip() end end if control_data["BlockedVersions"].to_s().strip().split( /\s*;\s*/ ).include?( version() ) notify( I18n.get( "wiki.control.versionblocked", version() ) ) @authorized = false # debug( "VERSION BLOCKED" ) elsif control_data["BlockedUsers"].to_s().strip().downcase().split( /\s*;\s*/ ).include?( @username.to_s().downcase() ) notify( I18n.get( "wiki.control.userblocked", @username ) ) @authorized = false # debug( "USER BLOCKED" ) else if (last_version = control_data["LastVersion"].to_s().strip().split( "." )).size > 0 curr_version = self.version().split( "." ) curr_version.size.times() do |idx| if curr_version[idx].to_i() > last_version[idx].to_i() break elsif curr_version[idx].to_i() < last_version[idx].to_i() notify( I18n.get( "wiki.control.updated", control_data["LastVersion"] ) ) break end end end @authorized = true # debug( "SUBMIT ALLOWED" ) end return @authorized end |
#build_album_art_description(artist, album, year, cleanup = true) ⇒ Object
1392 1393 1394 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1392 def build_album_art_description( artist, album, year, cleanup=true ) return self.class.build_album_art_description( artist, album, year, cleanup ) end |
#build_album_art_name(artist, album, year, extension = "jpg", cleanup = true) ⇒ Object
ALBUM ART FUNCTIONS
1388 1389 1390 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1388 def build_album_art_name( artist, album, year, extension="jpg", cleanup=true ) return self.class.build_album_art_name( artist, album, year, extension, cleanup ) end |
#build_album_edit_url(artist, album, year, cleanup = true) ⇒ Object
1341 1342 1343 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1341 def build_album_edit_url( artist, album, year, cleanup=true ) return self.class.build_album_edit_url( artist, album, year, cleanup ) end |
#build_album_link(artist, album, year, cleanup = true) ⇒ Object
1299 1300 1301 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1299 def build_album_link( artist, album, year, cleanup=true ) return self.class.build_album_link( artist, album, year, cleanup ) end |
#build_album_page(reviewed, artist, album, year, month, day, tracks, album_art) ⇒ Object
584 585 586 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 584 def build_album_page( reviewed, artist, album, year, month, day, tracks, album_art ) return self.class.build_album_page( reviewed, artist, album, year, month, day, tracks, album_art ) end |
#build_album_rawdata_url(artist, album, year, cleanup = true) ⇒ Object
1333 1334 1335 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1333 def build_album_rawdata_url( artist, album, year, cleanup=true ) return self.class.build_album_rawdata_url( artist, album, year, cleanup ) end |
#build_album_search_url(artist, album, year) ⇒ Object
1352 1353 1354 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1352 def build_album_search_url( artist, album, year ) return self.class.build_album_search_url( artist, album, year ) end |
#build_album_url(artist, album, year, cleanup = true) ⇒ Object
1325 1326 1327 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1325 def build_album_url( artist, album, year, cleanup=true ) return self.class.build_album_url( artist, album, year, cleanup ) end |
#build_link(article, display = nil) ⇒ Object
1118 1119 1120 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1118 def build_link( article, display=nil ) return self.class.build_link( article, display ) end |
#build_lyrics_fetch_data(request) ⇒ Object
165 166 167 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 165 def build_lyrics_fetch_data( request ) return FetchPageData.new( build_song_rawdata_url( request.artist, request.title ) ) end |
#build_rawdata_url(article) ⇒ Object
1147 1148 1149 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1147 def build_rawdata_url( article ) return self.class.build_rawdata_url( article ) end |
#build_song_add_url(request, cleanup = true) ⇒ Object
1228 1229 1230 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1228 def build_song_add_url( request, cleanup=true ) return self.class.build_song_add_url( request, cleanup ) end |
#build_song_edit_url(artist, title, cleanup = true) ⇒ Object
1220 1221 1222 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1220 def build_song_edit_url( artist, title, cleanup=true ) return self.class.build_song_edit_url( artist, title, cleanup ) end |
#build_song_link(artist, title, cleanup = true) ⇒ Object
1178 1179 1180 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1178 def build_song_link( artist, title, cleanup=true ) return self.class.build_song_link( artist, title, cleanup ) end |
#build_song_page(reviewed, artist, title, album, year, credits, lyricist, lyrics) ⇒ Object
694 695 696 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 694 def build_song_page( reviewed, artist, title, album, year, credits, lyricist, lyrics ) self.class.build_song_page( reviewed, artist, title, album, year, credits, lyricist, lyrics ) end |
#build_song_rawdata_url(artist, title, cleanup = true) ⇒ Object
1212 1213 1214 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1212 def build_song_rawdata_url( artist, title, cleanup=true ) return self.class.build_song_rawdata_url( artist, title, cleanup ) end |
#build_song_search_url(artist, title) ⇒ Object
1239 1240 1241 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1239 def build_song_search_url( artist, title ) return self.class.build_song_search_url( artist, title ) end |
#build_song_url(artist, title, cleanup = true) ⇒ Object
1204 1205 1206 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1204 def build_song_url( artist, title, cleanup=true ) return self.class.build_song_url( artist, title, cleanup ) end |
#build_suggestions_fetch_data(request) ⇒ Object
240 241 242 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 240 def build_suggestions_fetch_data( request ) return FetchPageData.new( build_song_search_url( request.artist, request.title ) ) end |
#build_tracks(album_data) ⇒ Object
580 581 582 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 580 def build_tracks( album_data ) return self.class.build_tracks( album_data ) end |
#build_url(article) ⇒ Object
1139 1140 1141 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1139 def build_url( article ) return self.class.build_url( article ) end |
#cleanup_article(article) ⇒ Object
1088 1089 1090 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1088 def cleanup_article( article ) return self.class.cleanup_article( article ) end |
#cleanup_title_token(title, downcase = false) ⇒ Object
1011 1012 1013 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1011 def cleanup_title_token( title, downcase=false ) return self.class.cleanup_title_token( title, downcase ) end |
#cleanup_title_token!(title, downcase = false) ⇒ Object
1015 1016 1017 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1015 def cleanup_title_token!( title, downcase=false ) return self.class.cleanup_title_token!( title, downcase ) end |
#control_page ⇒ Object
157 158 159 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 157 def control_page() return self.class.control_page() end |
#fetch_content_page(url, follow_redirects = @@REDIRECT_FOLLOWUPS) ⇒ Object
219 220 221 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 219 def fetch_content_page( url, follow_redirects=@@REDIRECT_FOLLOWUPS ) self.class.fetch_content_page( url, follow_redirects ) end |
#fetch_lyrics_page(fetch_data) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 223 def fetch_lyrics_page( fetch_data ) return nil, nil if fetch_data.url == nil log( "Fetching lyrics page... ", 0 ) page_body, page_url = fetch_content_page( fetch_data.url ) if page_body log( "OK" ) log( response.body(), 2 ) if verbose_log? else log( "ERROR" ) end return page_body, page_url end |
#find_album_art_name(artist, album, year) ⇒ Object
1396 1397 1398 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1396 def find_album_art_name( artist, album, year ) return self.class.find_album_art_name( artist, album, year ) end |
#find_album_art_url(artist, album, year) ⇒ Object
1409 1410 1411 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1409 def find_album_art_url( artist, album, year ) return self.class.find_album_art_url( artist, album, year ) end |
#find_album_page_url(artist, album, year) ⇒ Object
1381 1382 1383 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1381 def find_album_page_url( artist, album, year ) return self.class.find_album_page_url( artist, album, year ) end |
#find_song_page_url(artist, title) ⇒ Object
1269 1270 1271 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1269 def find_song_page_url( artist, title ) return self.class.find_song_page_url( artist, title ) end |
#get_session_params ⇒ Object
388 389 390 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 388 def get_session_params() return (@username ? @username.clone() : nil), (@password ? @password.clone() : nil), (@cookie ? @cookie.clone() : nil), @last_auth_check, @authorized end |
#get_sort_letter(title) ⇒ Object
1072 1073 1074 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1072 def get_sort_letter( title ) return self.class.get_sort_letter( title ) end |
#get_sort_name(title) ⇒ Object
1023 1024 1025 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1023 def get_sort_name( title ) return self.class.get_sort_name( title ) end |
#get_sort_name!(title) ⇒ Object
1057 1058 1059 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1057 def get_sort_name!( title ) return self.class.get_sort_name!( title ) end |
#logged_in? ⇒ Boolean
161 162 163 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 161 def logged_in?() return @logged_in end |
#login(username = @username, password = @password, force = false) ⇒ Object
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 264 def login( username=@username, password=@password, force=false ) return true if ! force && @logged_in && username == @username && password == @password @username, @password = username, password notify = ! @logged_in || ! force if ! @username || ! @password notify( I18n.get( "wiki.login.error", @username ? @username : "<NONE>" ) ) if notify return @logged_in = false end notify( I18n.get( "wiki.login.attempt", @username ) ) if notify headers = { "Keep-Alive"=>"300", "Connection"=>"keep-alive" } resp, url = HTTP.fetch_page_get( "http://#{site_host()}/index.php?title=Special:Userlogin", headers ) @cookie = resp.response["set-cookie"].split( "; " )[0] params = { "wpName"=>@username, "wpPassword"=>@password, "wpLoginattempt"=>"Log In" } headers.update( { "Cookie"=>@cookie } ) resp, url = HTTP.fetch_page_post( "http://#{site_host()}/index.php?title=Special:Userlogin&action=submitlogin", params, headers, -1 ) data = resp.body() data.gsub!( /[ \t\n]+/, " " ) @logged_in = (/<h2>Login error:<\/h2>/.match( data ) == nil) @logged_in = (/<h1 class="firstHeading">Login successful<\/h1>/.match( data ) != nil) if @logged_in # recheck notify( I18n.get( "wiki.login." + (@logged_in ? "success" : "error"), @username ) ) if notify return @logged_in end |
#logout ⇒ Object
298 299 300 301 302 303 304 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 298 def logout() @cookie = false if @logged_in @logged_in = false notify( I18n.get( "wiki.logout", @username ) ) end end |
#lyrics_from_url(request, url) ⇒ Object
244 245 246 247 248 249 250 251 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 244 def lyrics_from_url( request, url ) # we fetch and parse lyrics from raw content mode so we need the right url format if ! url.index( "&action=raw" ) artist, title = parse_song_url( url ) url = build_song_rawdata_url( artist, title, false ) end return super( request, url ) end |
#lyrics_page_valid?(request, page_body, page_url) ⇒ Boolean
236 237 238 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 236 def lyrics_page_valid?( request, page_body, page_url ) return ! page_error?( page_body ) end |
#parse_album_link(link) ⇒ Object
1287 1288 1289 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1287 def parse_album_link( link ) return self.class.parse_album_link( link ) end |
#parse_album_url(url) ⇒ Object
1313 1314 1315 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1313 def parse_album_url( url ) return self.class.parse_album_url( url ) end |
#parse_link(link) ⇒ Object
1110 1111 1112 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1110 def parse_link( link ) return self.class.parse_link( link ) end |
#parse_search_results(page_body, content_matches = false) ⇒ Object
825 826 827 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 825 def parse_search_results( page_body, content_matches=false ) self.class.parse_search_results( page_body, content_matches ) end |
#parse_song_link(link) ⇒ Object
1166 1167 1168 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1166 def parse_song_link( link ) return self.class.parse_song_link( link ) end |
#parse_song_url(url) ⇒ Object
1192 1193 1194 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1192 def parse_song_url( url ) return self.class.parse_song_url( url ) end |
#parse_suggestions(request, page_body, page_url) ⇒ Object
253 254 255 256 257 258 259 260 261 262 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 253 def parse_suggestions( request, page_body, page_url ) suggestions = [] parse_search_results( page_body, true ).each() do |result| artist, title = parse_song_url( result[@@SEARCH_RESULT_URL] ) if ! Strings.empty?( artist ) && ! Strings.empty?( title ) && /\ \([0-9]{4,4}\)$/.match( title ) == nil suggestions << Suggestion.new( artist, title, result[@@SEARCH_RESULT_URL] ) end end return suggestions end |
#parse_template(template) ⇒ Object
987 988 989 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 987 def parse_template( template ) return self.class.parse_template( template ) end |
#parse_url(url) ⇒ Object
1130 1131 1132 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1130 def parse_url( url ) return self.class.parse_url( url ) end |
#prepare_image_file(image_path, size_limit = 153600) ⇒ Object
1001 1002 1003 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 1001 def prepare_image_file( image_path, size_limit=153600 ) return self.class.prepare_image_file( image_path, size_limit ) end |
#restore_session(session_file) ⇒ Object
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 372 def restore_session( session_file ) values = { "username" => nil, "password" => nil, "cookie" => nil, "last_auth_check" => nil, "authorized" => nil } if XMLHash.read( session_file, values ) return restore_session_params( Strings.descramble( values["username"] ), Strings.descramble( values["password"] ), values["cookie"], values["last_auth_check"], values["authorized"] ) else notify( I18n.get( "wiki.session.restore.notfound" ) ) return false end end |
#restore_session_params(username, password, cookie, last_auth_check = 0, authorized = false) ⇒ Object
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 355 def restore_session_params( username, password, , last_auth_check=0, =false ) if ! Strings.empty?( ) && ! Strings.empty?( username ) @username = username @password = password @cookie = @last_auth_check = last_auth_check.to_i() @authorized = .to_s() == 'true' @logged_in = true notify( I18n.get( "wiki.session.restore.success", @username ) ) return true else notify( I18n.get( "wiki.session.restore.error", @username ) ) return false end end |
#save_session(session_file) ⇒ Object
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 392 def save_session( session_file ) if ! @logged_in notify( I18n.get( "wiki.session.save.error.notloggedin" ) ) return false end values = { "username" => Strings.scramble( @username ), "password" => Strings.scramble( @password ), "cookie" => @cookie, "last_auth_check" => @last_auth_check, "authorized" => @authorized } if XMLHash.write( session_file, values ) notify( I18n.get( "wiki.session.save.success", @username ) ) return true else notify( I18n.get( "wiki.session.save.error", @username ) ) return false end end |
#submit_album_page(album_data, image_path = nil, allow_page_overwrite = true, skip_initial_page_search = false, show_review_dialog = true, must_review = false) ⇒ Object
use skip_initial_page_search when you know the page doesn’t exists (i.e. when you have already searched it)
605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 605 def submit_album_page( album_data, image_path=nil, allow_page_overwrite=true, skip_initial_page_search=false, show_review_dialog=true, must_review=false ) show_review_dialog = true if must_review if ! allow_page_overwrite && ! skip_initial_page_search notify( I18n.get( "wiki.submitalbum.searchingpage", album_data.title, album_data.artist ) ) if find_album_page_url( album_data.artist, album_data.title, album_data.year ) notify( I18n.get( "wiki.submitalbum.pagefound", album_data.title, album_data.artist ) ) return nil, nil else notify( I18n.get( "wiki.submitalbum.nopagefound", album_data.title, album_data.artist ) ) end end page_data = { "site_name" => site_name(), "artist" => album_data.artist, "year" => album_data.year, "month" => album_data.month, "day" => album_data.day, "album" => cleanup_title_token( album_data.title ), "tracks" => build_tracks( album_data ), "reviewed" => false } page_data["album_art_name"] = find_album_art_name( page_data["artist"], page_data["album"], page_data["year"] ) if page_data["album_art_name"] == nil # album art not found, we'll attempt to upload it page_data["image_path"] = image_path.to_s() attempt_upload = true else attempt_upload = false end if show_review_dialog if ! GUI.show_submit_album_dialog( page_data ) notify( I18n.get( "wiki.submitalbum.cancelled" ) ) return nil, nil elsif must_review && ! page_data["reviewed"] notify( I18n.get( "wiki.submitalbum.mustreviewcontent" ) ) return nil, nil elsif page_data["artist"] != album_data.artist || page_data["album"] != album_data.title || page_data["year"] != album_data.year # the user changed one of the values that define the article name # check if the album parameters are "submitteable" content return nil, nil if ! submittable_album_params?( page_data["artist"], page_data["album"], page_data["year"] ) if ! allow_page_overwrite # check if there isn't already a page for the given values notify( I18n.get( "wiki.submitalbum.searchingpage", page_data["album"], page_data["artist"] ) ) if find_album_page_url( page_data["artist"], page_data["album"], page_data["year"] ) notify( I18n.get( "wiki.submitalbum.pagefound", page_data["album"], page_data["artist"] ) ) return nil, nil else notify( I18n.get( "wiki.submitalbum.nopagefound", page_data["album"], page_data["artist"] ) ) end end end else # check if the album parameters are "submitteable" content return nil, nil if ! submittable_album_params?( page_data["artist"], page_data["album"], page_data["year"] ) end page_url = build_album_url( page_data["artist"], page_data["album"], page_data["year"], false ) page_content = build_album_page( page_data["reviewed"], page_data["artist"], page_data["album"], page_data["year"], page_data["month"], page_data["day"], page_data["tracks"], page_data["album_art_name"] ? page_data["album_art_name"] : build_album_art_name( page_data["artist"], page_data["album"], page_data["year"] ) ) if attempt_upload && ! Strings.empty?( page_data["image_path"] ) upload_cover_image( page_data["image_path"], page_data["artist"], page_data["album"], page_data["year"] ) end summary = "#{page_data["reviewed"] ? "" : "autogen. "}album page (#{@@NAME}v#{@@VERSION})" if submit_page( page_url, page_content, summary ) notify( I18n.get( "wiki.submitalbum.success", page_data["album"], page_data["artist"] ) ) return page_url, page_data else notify( I18n.get( "wiki.submitalbum.error", page_url ) ) return nil, page_data end end |
#submit_page(page_url, page_content, summary = "", watch = true, retries = 1, auto_login = true) ⇒ Object
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 439 def submit_page( page_url, page_content, summary="", watch=true, retries=1, auto_login=true ) return false if (! logged_in? && ! auto_login) || ! (retries+1).times do begin next if ! logged_in? && ! login() # make sure we have the long url format page_url = build_url( parse_url( page_url ) ) # Try to get the edit token for url, can't continue without it: edit_params = fetch_page_edit_params( page_url ) if ! edit_params["logged_in"] # site says user is not logged in (session has expired), force relogin next if ! login( @username, @password, true ) # after being successfully logged in, refetch the edit page edit_params = fetch_page_edit_params( page_url ) next if ! edit_params["logged_in"] end next if ! edit_params["edit_token"] params = [ MultipartFormData.text_param( "wpTextbox1", page_content ), MultipartFormData.text_param( "wpSummary", summary ), MultipartFormData.text_param( "wpWatchthis", watch ? "on" : "off" ), MultipartFormData.text_param( "wpSave", "Save page" ), MultipartFormData.text_param( "wpSection", "" ), MultipartFormData.text_param( "wpStarttime", edit_params["start_time"].to_s() ), # the new revision time MultipartFormData.text_param( "wpEdittime", edit_params["edit_time"].to_s() ), # the previous revision time MultipartFormData.text_param( "wpEditToken", edit_params["edit_token"] ), ] headers = { "Keep-Alive" => "300", "Connection" => "keep-alive", "Referer" => "http://#{site_host()}#{page_url}&action=edit", "Cookie" => @cookie, } response, page_url = HTTP.fetch_page_post_form_multipart( "#{page_url}&action=submit", params, headers, -1 ) return true if response.code == "302" # we should have received a redirect code rescue TimeoutError end end return false end |
#submit_redirect_page(page_url, link, summary = nil) ⇒ Object
493 494 495 496 497 498 499 500 501 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 493 def submit_redirect_page( page_url, link, summary=nil ) if submit_page( page_url, "#REDIRECT #{link}", summary ) notify( I18n.get( "wiki.submitredirect.success", link ) ) return url else notify( I18n.get( "wiki.submitredirect.error", link ) ) return nil end end |
#submit_song_page(song_data, edit_url = nil, skip_initial_page_search = false, show_review_dialog = true, must_review = false) ⇒ Object
if edit_url is not nil, it’s assumed that we’re trying to overwrite (edit) a page, otherwise it’s assummed that we’re trying to create a new page and so overwritting won’t be allowed use skip_initial_page_search when you know the page doesn’t exists (i.e. when you have already searched it)
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 718 def submit_song_page( song_data, edit_url=nil, skip_initial_page_search=false, show_review_dialog=true, must_review=false ) show_review_dialog = true if must_review if ! edit_url && ! skip_initial_page_search notify( I18n.get( "wiki.submitsong.searchingpage", song_data.title, song_data.artist ) ) if find_song_page_url( song_data.artist, song_data.title ) notify( I18n.get( "wiki.submitsong.pagefound", song_data.title, song_data.artist ) ) return nil, nil else notify( I18n.get( "wiki.submitsong.nopagefound", song_data.title, song_data.artist ) ) end end page_data = { "site_name" => site_name(), "edit_mode" => edit_url != nil, "artist" => cleanup_title_token( song_data.artist ), "year" => song_data.year, "album" => cleanup_title_token( song_data.album.to_s() ), "title" => cleanup_title_token( song_data.title ), "lyrics" => Strings.cleanup_lyrics( song_data.lyrics.to_s() ), "instrumental" => song_data.instrumental?, "credits" => song_data.credits.join( "; " ), "lyricist" => song_data.lyricists.join( "; " ), "reviewed" => false } if show_review_dialog if ! GUI.show_submit_song_dialog( page_data ) notify( I18n.get( "wiki.submitsong.cancelled" ) ) return nil, nil elsif must_review && ! page_data["reviewed"] notify( I18n.get( "wiki.submitsong.mustreviewcontent" ) ) return nil, nil elsif page_data["artist"] != song_data.artist || page_data["title"] != song_data.title || page_data["lyrics"] != song_data.lyrics || page_data["instrumental"] != song_data.instrumental? # check if the song parameters are "submitteable" content return nil, nil if ! submittable_song_params?( page_data["artist"], page_data["title"], page_data["lyrics"], page_data["instrumental"] ) if ! edit_url && (page_data["artist"] != song_data.artist || page_data["title"] != song_data.title) # check if there isn't already a page for the given values notify( I18n.get( "wiki.submitsong.searchingpage", page_data["title"], page_data["artist"] ) ) if find_song_page_url( page_data["artist"], page_data["title"] ) notify( I18n.get( "wiki.submitsong.pagefound", page_data["title"], page_data["artist"] ) ) return nil, nil else notify( I18n.get( "wiki.submitsong.nopagefound", page_data["title"], page_data["artist"] ) ) end end end else # check if the song parameters are "submitteable" content return nil, nil if ! submittable_song_params?( page_data["artist"], page_data["title"], page_data["lyrics"], page_data["instrumental"] ) end page_url = edit_url ? edit_url : build_song_url( page_data["artist"], page_data["title"], false ) page_content = build_song_page( page_data["reviewed"], page_data["artist"], page_data["title"], page_data["album"], page_data["year"], page_data["credits"], page_data["lyricist"], page_data["lyrics"] ) summary = "#{page_data["reviewed"] ? "" : "autogen. "}song page (#{@@NAME}v#{@@VERSION})" if submit_page( page_url, page_content, summary ) notify( I18n.get( "wiki.submitsong.success", page_data["title"], page_data["artist"] ) ) return page_url, page_data else notify( I18n.get( "wiki.submitsong.error", page_url ) ) return nil, page_data end end |
#upload_cover_image(image_path, artist, album, year, watch = true) ⇒ Object
560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 560 def upload_cover_image( image_path, artist, album, year, watch=true ) album_art_name = build_album_art_name( artist, album, year ) album_art_desc = build_album_art_description( artist, album, year ) image_path, mime_type = prepare_image_file( image_path ) if Strings.empty?( image_path ) || Strings.empty?( mime_type ) notify( I18n.get( "wiki.uploadcover.error.convert" ) ) else notify( I18n.get( "wiki.uploadcover.uploading", album, artist ) ) if upload_file( image_path, album_art_name, mime_type, album_art_desc, watch ) notify( I18n.get( "wiki.uploadcover.success", album, artist ) ) else notify( I18n.get( "wiki.uploadcover.error", album, artist ) ) end end end |
#upload_file(src_file, dst_file, mime_type, description = "", watch = true, auto_login = true) ⇒ Object
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 503 def upload_file( src_file, dst_file, mime_type, description="", watch=true, auto_login=true ) if ! logged_in? return false if ! auto_login || ! login() end begin data = File.new( src_file ).read() rescue Exception return false end params = [ MultipartFormData.file_param( "wpUploadFile", File.basename( src_file ), mime_type, data ), MultipartFormData.text_param( "wpDestFile", dst_file ), MultipartFormData.text_param( "wpUploadDescription", description ), MultipartFormData.text_param( "wpWatchthis", watch ? "true" : "false" ), MultipartFormData.text_param( "wpUpload", "Upload file" ), ] headers = { "Keep-Alive" => "300", "Connection" => "keep-alive", "Referer" => "http://#{site_host()}/index.php?title=Special:Upload&wpDestFile=#{CGI.escape(dst_file)}", "Cookie" => @cookie, } begin response, page_url = HTTP.fetch_page_post_form_multipart( "http://#{site_host()}/index.php?title=Special:Upload", params, headers, -1 ) # we have to receive a redirect code return true if response.code == "302" # error, possibly an expired session: relogin and try again login( @username, @password, true ) response, page_url = HTTP.fetch_page_post_form_multipart( "http://#{site_host()}/index.php?title=Special:Upload", params, headers, -1 ) # again, we should have received a redirect return response.code == "302" rescue TimeoutError return false end end |
#version ⇒ Object
141 142 143 |
# File 'lib/wiki_lyrics/mediawikilyrics.rb', line 141 def version() return self.class.version() end |