Package Usage¶
Initialising a session¶
First thing to do is setting up a client.
A client represents one connection to PH,
and you can use multiple at the same time.
You can also specify a custom language
locale (en
by default). This will
affect searching preferences, video titles,
etc.
import phub
client = phub.Client(language = 'en')
Fetching a video¶
If you want to work with a specific video you know the URL of, you can initialise it like so:
import phub
client = phub.Client()
url = 'https://www.pornhub.com/view_video.php?viewkey=xxx'
video = client.get(url) # (1)!
Note that you can also load the video using the viewkey parameter in its URL.
video = client.get('xxx')
Accessing data¶
A Video
object has several properties you can use to fetch
data on a video. By default, no data is actually fetched until you call
a property for optimization purposes. Once fetched, data is cached for
each property. If you want to refresh the data, you will have to clear
the cache by calling Video.refresh()
.
Note
You can find out all the available properties
in the video docs or in the Video
class.
video = client.get('xxx')
print(f'The "{video.title}" has {video.likes.up} likes!')
Downloading a video¶
A video can be downloaded by using Video.download()
.
import phub
from phub.locals import Quality
client = phub.Client()
video = client.get('xxx')
video.download(path = 'my-video.mp4',
quality = Quality.BEST)
Note
Tip: You can set the path
parameter to be a directory for the video
to be downloaded in. The file name will automatically be the video id.
For more information on how to download, see downloading.
Logging¶
You can use Python logging library to debug your code and see what’s wrong with it or the API.
import phub
import logging
# Use whatever configuration you want
logging.BasicConfig(level = logging.INFO)
client = phub.Client()
...
Compatibility¶
Most of the PHUB objects have a dictify
method that allows
them to be converted to serialized objects.
import phub
client = phub.Client()
video = client.get('xxx')
data = video.dictify()
Result: {"name": "A cool video", ...}
This is done for compatibility and ease of use with other languages and applications, since you can easily run a small python script or local server:
import phub
import flask
client = phub.Client()
app = flask.Flask(__name__)
@app.route('/get')
def get():
try:
url = flask.request.args.get('video')
video = client.get(url)
res = { 'response': video.dictify() }
except Exception as err:
res = { 'error': repr(err) }
return flask.jsonify(res)
if __name__ == '__main__':
app.run()
For instance, this script will use flask to run a web server that can fetch video data:
$ curl <localhost>/get?video=abcdef1234
{
"name": "A cool video"
# etc.
}
Each dictify
method can take as argument a list[str]
of keys,
if you want to avoid fetching specific things.
Below is a list of all serializable PHUB objects, along with their keys. PHUB objects are keys that redirect to a PHUB object.
Object |
Default keys |
PHUB objects |
---|---|---|
|
|
|
|
|
|
|
/ |
|
|
|
|
|
/ |
|
|
/ |
|
|
/ |
By default, recursive keys will appear as repr
strings, unless
you allow repulsiveness with object.dictify(recursive = True)
.
Warning
Turning on recursion can make PHUB open more requests that you might actually need. Make sure you specify only the keys you need when using it.