电视客户端蟒蛇
tvrampage的Python项目详细描述
关于
PythonTvrage是Tvrage.com基于XML的API feeds的基于Python的面向对象客户端接口。
安装
从源安装:
$ python setup.py install
开始使用python tvarge的最简单方法是使用easy_install或pip:
$ easy_install -U python-tvrage
对于pip,必须用'pip install'替换'easy_install'。此外,您的平台可能需要使用sudo
文档
Tvrage软件包由三个模块组成
- feeds模块为tvarge的每个xml feeds提供一个包装函数
- api模块在这些服务之上提供了一个干净的、面向对象的界面
- tvrage.quickinfo模块是tvrage的quickinfo api的简单pythonic包装器。值将作为python字典返回,而不是作为电视节目和剧集的专用对象返回
获取XML数据
tvrage.feeds模块提供对tvrage.com数据feeds的基本和低级访问。对于更复杂的用例,建议使用面向对象模块Tvrage.api。 注意:Tvrage.feeds模块中的所有函数都将XML数据作为ElementTree对象返回。
使用常规搜索功能搜索电视节目(返回模糊搜索结果):
$ python >>> from tvrage import feeds >>> from xml.etree.ElementTree import tostring >>> >>> doctor_who = feeds.search('doctorwho2005') >>> for node in doctor_who: ... print(tostring(node)) ... <show> <showid>3332</showid> <name>Doctor Who (2005)</name> <link>http://www.tvrage.com/DoctorWho_2005</link> <country>UK</country> <started>2005</started> <ended>0</ended> <seasons>5</seasons> <status>Returning Series</status> <classification>Scripted</classification> <genres> <genre01>Action</genre01><genre02>Adventure</genre02><genre03>Sci-Fi</genre03> </genres> </show> <show> <showid>3331</showid> <name>Doctor Who</name> <link>http://www.tvrage.com/Doctor_Who_1963</link> <country>UK</country> <started>1963</started> <ended>1989</ended> <seasons>27</seasons> <status>Canceled/Ended</status> <classification>Scripted</classification> <genres> <genre01>Action</genre01><genre02>Adventure</genre02><genre03>Sci-Fi</genre03> </genres> </show>
使用完整搜索在搜索结果中包含有关节目的所有可用元数据:
>>> doctor_who = feeds.full_search('doctorwho2005') >>> print(tostring(doctor_who[0])) <show> <showid>3332</showid> <name>Doctor Who (2005)</name> <link>http://www.tvrage.com/DoctorWho_2005</link> <country>UK</country> <started>Mar/26/2005</started> <ended /> <seasons>5</seasons> <status>Returning Series</status> <runtime>50</runtime> <classification>Scripted</classification> <genres> <genre>Action</genre><genre>Adventure</genre><genre>Sci-Fi</genre> </genres> <network country="UK">BBC One</network> <airtime>19:00</airtime> <airday>Saturday</airday> <akas> <aka country="FR">Docteur Who</aka> <aka attr="Alternative Spelling" country="UK">Dr. Who</aka> <aka country="IS">Tímaflakk</aka> <aka attr="Fake Working Title" country="UK">Torchwood</aka> <aka country="BG">Доктор Кой</aka> <aka country="RU">Доктор Кто</aka> <aka country="IL">דוקטור הו</aka> <aka country="IN">डॉक्टर हू</aka> <aka country="CN">異世奇人</aka></akas> </show>
showinfofeed使用给定的showid检索有关一个节目的所有元数据。结果与完整搜索结果中的一个元素相同。
插播列表feed返回按季节排序的电视节目插播的所有元数据。可选的节点参数使函数返回所需的xml节点作为elementtree对象:
>>> doctor_who_eps = feeds.episode_list('3332', node='Episodelist') >>> print(tostring(doctor_who_eps[0])) <Season no="1"> <episode> <epnum>1</epnum> <seasonnum>01</seasonnum> <prodnum>101</prodnum> <airdate>2005-03-26</airdate> <link>http://www.tvrage.com/DoctorWho_2005/episodes/52117</link> <title>Rose</title></episode> <episode> <epnum>2</epnum> <seasonnum>02</seasonnum> <prodnum>102</prodnum> <airdate>2005-04-02</airdate> <link>http://www.tvrage.com/DoctorWho_2005/episodes/52118</link> <title>The End of the World</title></episode> ... </Season>
完整的节目信息提要结合了节目信息和插曲列表的结果。
使用对象
模块Tvrage.api为Tvrage.com的数据提要提供包装类。它包含以下类:节目,季和集。
使用电视节目对象:
$ python >>> import tvrage.api >>> doctor_who = tvrage.api.Show('doctorwho2005') >>> doctor_who.country 'UK' >>> doctor_who.current_season {1: Doctor Who (2005) 5x01 The Eleventh Hour, 2: Doctor Who (2005) 5x02 The Beast... } >>> doctor_who.ended 0 >>> doctor_who.episodes {1: {1: Doctor Who (2005) 1x01 Rose, 2: Doctor Who (2005) 1x02 The End of the World, ... }} >>> doctor_who.genres ['Action', 'Adventure', 'Sci-Fi'] >>> doctor_who.latest_episode Doctor Who (2005) 5x04 The Time of Angels (1) >>> doctor_who.next_episode Doctor Who (2005) 5x05 Flesh and Stone (2) >>> doctor_who.link 'http://www.tvrage.com/DoctorWho_2005' >>> doctor_who.name 'Doctor Who (2005)' >>> doctor_who.pilot Doctor Who (2005) 1x01 Rose >>> doctor_who.season(2) {1: Doctor Who (2005) 2x01 New Earth, 2: Doctor Who (2005) 2x02 Tooth and Claw, ... } >>> doctor_who.seasons 5 >>> doctor_who.shortname 'doctorwho2005' >>> doctor_who.showid '3332' >>> doctor_who.started 2005 >>> doctor_who.status 'Returning Series' >>> doctor_who.upcoming_episodes <generator object upcoming_episodes at 0x152f0a8>
season对象是一个python dict,具有其他属性:
>>> s4 = doctor_who.season(4) >>> s4.is_current False >>> s4.premiere Doctor Who (2005) 4x01 Partners in Crime >>> s4.finale Doctor Who (2005) 4x13 Journey's End (3) >>> s4.episode(3) Doctor Who (2005) 4x03 Planet of the Ood >>> s4.values() [Doctor Who (2005) 4x01 Partners in Crime, Doctor Who (2005) 4x02 The Fires of... ] >>> s4.keys() [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
插曲对象包含与特定插曲相关的所有信息:
>>> rose = doctor_who.season(1).episode(1) >>> rose.airdate datetime.date(2005, 3, 26) >>> rose.link 'http://www.tvrage.com/DoctorWho_2005/episodes/52117' >>> rose.number 1 >>> rose.prodnumber '101' >>> rose.season 1 >>> rose.show 'Doctor Who (2005)' >>> rose.title 'Rose'
有些剧集有详细的摘要信息。这些信息不是由xml提要提供的,因此必须通过web抓取从该集的概述页中提取。由于在整个赛季中提前加载所有这些网页会非常缓慢,因此只有在实际读取插曲.summary属性时才会加载摘要信息:
>>> nextff = tvrage.api.Show('flashforward').next_episode >>> nextff FlashForward 1x18 Goodbye Yellow Brick Road >>> nextff.summary # spoilers alert!... you have to try this one for your self ;)
使用quickinfo feed
模块tvrage.quickinfo提供了对tvrage的quickinfo feed的简单访问。
您可以单独获取有关电视节目的元数据:
>>> from tvrage import quickinfo >>> quickinfo.fetch('doctor who 2005') {'Status': 'Returning Series', 'Genres': ['Action', 'Adventure', 'Sci-Fi'], 'Network': 'BBC One (United Kingdom)', 'Classification': 'Scripted', 'Started': 'Mar/26/2005', 'Show Name': 'Doctor Who (2005)', 'Show URL': 'http://www.tvrage.com/DoctorWho_2005', 'Premiered': '2005', 'Airtime': 'Saturday at 07:00 pm', 'Ended': '', 'Show ID': '3332', 'Country': 'United Kingdom', 'Runtime': '50', 'Latest Episode': ['05x13', 'The Big Bang (2)', 'Jun/26/2010']}
或者您可以获取与节目元数据相结合的特定节目的信息:
>>> epinfo = quickinfo.fetch('doctor who 2005', ep='1x01') >>> epinfo {'Status': 'Returning Series', 'Genres': ['Action', 'Adventure', 'Sci-Fi'], ... >>> epinfo['Episode Info'] ['01x01', 'Rose', '26/Mar/2005'] >>> epinfo['Episode URL'] 'http://www.tvrage.com/DoctorWho_2005/episodes/52117'