Failed to download file using pafy - python

I am using Python 2.7 and pafy to download audio file from youtube
import pafy
video = pafy.new("https://www.youtube.com/watch?v=dcNlEn1LrrE")
print video.m4astreams
filename = video.m4astreams[0].download(quiet=False)
I get the following error:
Traceback (most recent call last):
File "E:\work\Python\2017\pafy\work_with_pafy.py", line 27, in <module>
filename = video.m4astreams[0].download(quiet=False)#.encode('utf-8')
File "c:\python27\lib\site-packages\pafy\backend_shared.py", line 586, in download
filename = self.generate_filename(meta=meta, max_length=256-len('.temp'))
File "c:\python27\lib\site-packages\pafy\backend_shared.py", line 458, in generate_filename
return xenc(filename)
File "c:\python27\lib\site-packages\pafy\util.py", line 63, in xenc
return utf8_replace(stuff) if not_utf8_environment else stuff
File "c:\python27\lib\site-packages\pafy\util.py", line 57, in utf8_replace
txt = txt.encode(sse, "replace").decode(sse)
TypeError: encode() argument 1 must be string, not None
Please Help!
Thanks in advance.

I have found the solution.
The problem is solved by replacing one string in util.py file C:\Python27\Lib\site-packages\pafy\util.py
I replaced that string in util.py:
txt = txt.encode(sse, "replace").decode(sse)
by this one:
txt = txt.encode('utf-8')
After that file successfully downloaded without any problems.

Related

pytube.exceptions.RegexMatchError: __init__: could not find match for ^\w+\W

so my issue is I run this simple code to attempt to make a pytube stream object...
from pytube import YouTube
yt = YouTube('https://www.youtube.com/watch?v=aQNrG7ag2G4')
stream = yt.streams.filter(file_extension='mp4')
And end up with the error in the title.
full error:
Traceback (most recent call last):
File ".\test.py", line 4, in <module>
stream = yt.streams.filter(file_extension='mp4')
File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 292, in streams
return StreamQuery(self.fmt_streams)
File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\__main__.py", line 184, in fmt_streams
extract.apply_signature(stream_manifest, self.vid_info, self.js)
File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\extract.py", line 409, in apply_signature
cipher = Cipher(js=js)
File "C:\Users\logan\AppData\Local\Programs\Python\Python38\lib\site-packages\pytube\cipher.py", line 33, in __init__
raise RegexMatchError(
pytube.exceptions.RegexMatchError: __init__: could not find match for ^\w+\W
Extra data:
python version: 3.8.10
pytube version: 11.0.2
As juanchosaravia suggested on https://github.com/pytube/pytube/issues/1199, in order to solve the problem, you should go in the cipher.py file and replace the line 30, which is:
var_regex = re.compile(r"^\w+\W")
With that line:
var_regex = re.compile(r"^\$*\w+\W")
After that, it worked again.
To go cipher.py in the pytube site packages
Change line 30
var_regex = re.compile(r"^\w+\W")
to
var_regex = re.compile(r"^$*\w+\W")
"^" means search beginning of the line
".*" means zero or more of any character
"$" means to end of line

How Do We Convert HTML to PDF using Python, Is there Any code please share it to me?

I have tried the Library called pytotree, But i didnt get any Answer
This is the code:
import pdftotree
file= open('C:/Users/chaitanya.naidu/Downloads/test.pdf', 'rb')
f = pdftotree.parse(file)
I am getting this error
Traceback (most recent call last):
File "<ipython-input-4-4a9a6b72801d>", line 1, in <module>
f = pdftotree.parse(file)
File "C:\Users\chaitanya.naidu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pdftotree\core.py", line 63, in parse
if not extractor.is_scanned():
File "C:\Users\chaitanya.naidu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pdftotree\TreeExtract.py", line 121, in is_scanned
self.parse()
File "C:\Users\chaitanya.naidu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pdftotree\TreeExtract.py", line 91, in parse
for page_num, layout in enumerate(analyze_pages(self.pdf_file)):
File "C:\Users\chaitanya.naidu\AppData\Local\Continuum\Anaconda3\lib\site-packages\pdftotree\utils\pdf\pdf_utils.py", line 117, in analyze_pages
with open(os.path.realpath(file_name), "rb") as fp:
File "C:\Users\chaitanya.naidu\AppData\Local\Continuum\Anaconda3\lib\ntpath.py", line 542, in abspath
path = os.fspath(path)
TypeError: expected str, bytes or os.PathLike object, not _io.BufferedReader
You can use pdfkit, example:
import pdfkit
pdfkit.from_url('http://google.com', 'out.pdf')
pdfkit.from_file('test.html', 'out.pdf')
pdfkit.from_string('Hello!', 'out.pdf')

gTTS Doesn't Save MP3 Files

I want to create an artificial intelligence program using Python. I found out that I need gTTS but it doesn't save mp3 files. Help me, please.
#gtts is imported
def talkToMe(audio, lgg = 'en'):
#print(audio)
tts = gTTS(text = audio, lang = lgg)
#tts.save('audio.mp3') #doesn't work
with open("audio.mp3") as fp: #doesn't work
tts.write_to_fp(fp)
os.system('mpg123\mpg123.exe audio.mp3')
Traceback (most recent call last):
File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 9, in <module>
talkToMe("hello")
File "C:\Users\zigzag\Desktop\gtts_test1\main.py", line 7, in talkToMe
tts.write_to_fp(fp)
File "B:\Python36\lib\site-packages\gtts\tts.py", line 187, in write_to_fp
part_tk = self.token.calculate_token(part)
File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "B:\Python36\lib\site-packages\gtts_token\gtts_token.py", line 62, in _get_token_key
a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'
from gtts import gTTS
#gtts is imported
audio = 'Hello , I am Tina, your digital assistant'
def talkToMe(audio, lgg = 'en'):
#print(audio)
tts = gTTS(text = audio, lang = lgg)
tts.save('audio.mp3') #doesn't work
return None
talkToMe(audio , lgg ='en')
save the above file as text_2_speech.py (as an e.g.).i have tried it and it is working fine. from command line type python -W ignore text_2_speech.py and then the file is saved as audio.mp3 in the current directory.

Phone Book Access Profile pabp linux

I try to execute the file 'pbapclient.py'. I downloaded the files from https://github.com/bmwcarit/pypbap , but the console prompt show me some errors. I want to print/save all my contact from my android phone(I've got htc one m9).I run antergos. I try for at least 4 month... thanks for the help.
This is the prompt
[mattia#LinuxMattia pypbap-master]$ python3 pbapclient.py
Traceback (most recent call last): File "pbapclient.py", line 22, in
<module> from PyOBEX import client File "/usr/lib/python3.7/site-
packages/PyOBEX/client.py", line 489, in <module>
class SyncClient(Client): File "/usr/lib/python3.7/site-
packages/PyOBEX/client.py", line 491, in SyncClient
def connect(self, header_list = (headers.Target("IRMC-SYNC"),)):
File "/usr/lib/python3.7/site-packages/PyOBEX/headers.py", line 33, in
__init__
self.data = self.encode(data)
File "/usr/lib/python3.7/site-packages/PyOBEX/headers.py", line 48, in
encode return struct.pack(">BH", self.code, len(data) + 3) + data
TypeError: can't concat str to bytes
Thanks for the help
The installation guide (https://github.com/bmwcarit/pypbap#installation) tells you that the program needs to be run with python2, however you were using python3.

Reading TDMS File with python nptdms, cannot open tdms file

I am having issues with getting basic function of the nptdms module working.
First, I am just trying to open a TDMS file and print the contents of specific channels within specific groups.
Using python 2.7 and the nptdms quick start here
Following this, I will be writing these specific pieces of data into a new TDMS file. Then, my ultimate goal is to be able to take a set of source files, open each, and write (append) to a new file. The source data files contain far more information that is needed, so I am breaking out the specifics into their own file.
The problem I have is that I cannot get past a basic error.
When running this code, I get:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 27, in <module>
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 94, in __init__
self._read_segments(f)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 119, in _read_segments
object._initialise_data(memmap_dir=self.memmap_dir)
File "C:\Anaconda2\lib\site-packages\nptdms\tdms.py", line 709, in _initialise_data
mode='w+b', prefix="nptdms_", dir=memmap_dir)
File "C:\Anaconda2\lib\tempfile.py", line 475, in NamedTemporaryFile
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
File "C:\Anaconda2\lib\tempfile.py", line 244, in _mkstemp_inner
fd = _os.open(file, flags, 0600)
OSError: [Errno 2] No such file or directory: 'r\\nptdms_yjfyam'
Here is my code:
from nptdms import TdmsFile
import numpy as np
import pandas as pd
#set Tdms file path
tdms_file = TdmsFile(r"C:\\Users\daniel.worts\Desktop\this_is_my_tdms_file.tdms","r")
# set variable for TDMS groups
group_nameone = '101'
group_nametwo = '752'
# set objects for TDMS channels
channel_dataone = tdms_file.object(group_nameone 'Payload_1')
channel_datatwo = tdms_file.object(group_nametwo, 'Payload_2')
# set data from channels
data_dataone = channel_dataone.data
data_datatwo = channel_datatwo.data
print data_dataone
print data_datatwo
Big thanks to anyone who may have encountered this before and can help point to what I am missing.
Best,
- Dan
edit:
Solved the read data issue by removing the 'r' argument from the file path.
Now I am having another error I can't trace when trying to write.
from nptdms import TdmsFile, TdmsWriter, RootObject, GroupObject, ChannelObject
import numpy as np
import pandas as pd
newfilepath = r"C:\\Users\daniel.worts\Desktop\Mined.tdms"
datetimegroup101_channel_object = ChannelObject('101', DateTime, data_datetimegroup101)
with TdmsWriter(newfilepath) as tdms_writer:
tdms_writer.write_segment([datetimegroup101_channel_object])
Returns error:
Traceback (most recent call last):
File "PullTDMSdataIntoNewFile.py", line 82, in <module>
tdms_writer.write_segment([datetimegroup101_channel_object])
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 68, in write_segment
segment = TdmsSegment(objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in __init__
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 88, in <genexpr>
paths = set(obj.path for obj in objects)
File "C:\Anaconda2\lib\site-packages\nptdms\writer.py", line 254, in path
self.channel.replace("'", "''"))
AttributeError: 'TdmsObject' object has no attribute 'replace'

Categories