How to get info from terminal in python - python-3.x

I use ytapi to get info from searching a YouTube video and I get a videoId in terminal. So i am trying to get that so from that i can open the YouTube video.
Here is what i've done:
from apiclient.discovery import build
youtube = build("youtube" , "v3" , developerKey = api_key)
req = youtube.search().list(q="google",part="id",type="video",fields="items/id")
res = req.execute()
print(res)
Output:
{'items': [{'id': {'kind': 'youtube#video', 'videoId': 'XKmsYB54zBk'}}, {'id': {'kind': 'youtube#video', 'videoId': 'd6nwLctqB3c'}}, {'id': {'kind': 'youtube#video', 'videoId': 'F95wcsJAz0c'}}, {'id': {'kind': 'youtube#video', 'videoId': 'p0ysH2Glw5w'}}, {'id': {'kind': 'youtube#video', 'videoId': 'GvmDVq3PtA8'}}]}
From that is there a way to get only the videoId so i can use it to open videos with
webbrowser.open("https://www.youtube.com/watch?v= " + videoId)

This is what you are looking for
from apiclient.discovery import build
youtube = build("youtube" , "v3" , developerKey = api_key)
req = youtube.search().list(q="google",part="id",type="video",fields="items/id")
res = req.execute()
urladd = res['items'][0]['id']['videoId']
webbrowser.open("https://www.youtube.com/watch?v= " + urladd)

Related

Unable to get correct delivery fee in Foodpanda via scrapy

I am getting delivery fee but for few restaurants, its slightly different than the one on website, say its $2.99 on website but spider is getting $3.09. I have checked latitude and longitude but its same in the url and spider. I have changed multiple values in headers and it changed the delivery fee for few restaurants but not for all. This is the sample url: https://www.foodpanda.sg/restaurants/new?lat=1.43035&lng=103.83684&vertical=restaurants and below is the code snippet:
api_url = 'https://disco.deliveryhero.io/listing/api/v1/pandora/vendors'
form_data = {
'country': 'sg',
'latitude': str(lat),
'longitude': str(lng),
'sort': '',
'language_id': str(self.language_id),
'vertical': 'restaurants',
'include': 'characteristics',
'dynamic_pricing': '0',
'configuration': 'Variant1',
'use_free_delivery_label': 'true',
'limit': '50',
'offset': '0'
}
headers = {
'X-FP-API-KEY': 'volo',
'dps-session-id': dps_session_id,
'x-disco-client-id': 'web',
}
return scrapy.FormRequest(
api_url,
method='GET',
headers=headers,
formdata=form_data,
meta={
'page': page,
'lat': lat,
'lng': lng,
'item_meta': item_meta,
'start_position': start_position
},
callback=self.parse_search
)
Any clue or insight is appreciated.

AttributeError: module 'google.cloud.dialogflow' has no attribute 'types'

I am building a telegram-bot and using Dialogflow in it, I am getting the following error :
2021-11-19 23:26:46,936 - __main__ - ERROR - Update '{'message': {'entities': [],
'delete_chat_photo': False, 'text': 'hi', 'date': 1637344606, 'new_chat_members': [],
'channel_chat_created': False, 'message_id': 93, 'photo': [], 'group_chat_created':
False, 'supergroup_chat_created': False, 'new_chat_photo': [], 'caption_entities': [],
'chat': {'id': 902424541, 'type': 'private', 'first_name': 'Akriti'},
'from': {'first_name': 'Akriti', 'id': 902424541, 'is_bot': False, 'language_code': 'en'}
}, 'update_id': 624230278}' caused error 'module 'google.cloud.dialogflow' has no
attribute 'types''
It appears there is some issue with the Dialogflow attribute "types", but I don't know what I am doing wrong.
Here is the code where I am using it:
import os
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "client.json"
from google.cloud import dialogflow
dialogflow_session_client = dialogflow.SessionsClient()
PROJECT_ID = "get-informed-ufnl"
from gnewsclient import gnewsclient
client = gnewsclient.NewsClient()
def detect_intent_from_text(text, session_id, language_code='en'):
session = dialogflow_session_client.session_path(PROJECT_ID, session_id)
text_input = dialogflow.types.TextInput(text=text, language_code=language_code)
query_input = dialogflow.types.QueryInput(text=text_input)
response = dialogflow_session_client.detect_intent(session=session, query_input=query_input)
return response.query_result
def get_reply(query, chat_id):
response = detect_intent_from_text(query, chat_id)
if response.intent.display_name == 'get_news':
return "get_news", dict(response.parameters)
else:
return "small_talk", response.fulfillment_text
def fetch_news(parameters):
client.language = parameters.get('language')
client.location = parameters.get('geo-country')
client.topic = parameters.get('topic')
return client.get_news()[:5]
topics_keyboard = [
['Top Stories', 'World', 'Nation'],
['Business', 'Technology', 'Entertainment'],
['Sports', 'Science', 'Health']
]
I figured it out, the problem lies in the import statement. The correct module name should be:
import google.cloud.dialogflow_v2 as dialogflow
I recommend to deactivate your current error handler or use one similar to this example such that you can see the full traceback of the exception :)
Disclaimer: I'm currently the maintainer of python-telegram-bot

Getting HttpError 401 while generating google Admob network report using python script

Below is the code that I am using to generate the Admob network report
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import os
base_path=os.path.dirname(os.path.realpath(__file__))
scopes=['https://www.googleapis.com/auth/admob.report']
key_file_location = base_path+'/config/service_account.json'
credentials = ServiceAccountCredentials.from_json_keyfile_name(key_file_location, scopes)
account_id='accounts/pub-XXXXXXXXXXXXXXXX'
network_report_filter = {
'dateRange': {
'startDate': {'year': 2020, 'month': 1, 'day': 1},
'endDate': {'year': 2020, 'month': 2, 'day': 10}
},
'dimensions': ['DATE', 'APP', 'COUNTRY'],
'metrics': ['CLICKS', 'ESTIMATED_EARNINGS'],
'dimensionFilters': [
{
'dimension': 'COUNTRY',
'matchesAny': {'values': [{'value': 'US', 'value': 'CN'}]}
}
],
'sortConditions': [
{'dimension':'APP', 'order': 'ASCENDING'},
{'metric':'CLICKS', 'order': 'DESCENDING'}
],
'localizationSettings': {
'currencyCode': 'USD',
'languageCode': 'en-US'
}
}
# Build the service object.
admob = build('admob', 'v1', credentials=credentials)
admob._resourceDesc=network_report_filter
accounts=admob.accounts()
network_report=accounts.networkReport().generate(parent=account_id)
data=network_report.execute()
It throws the below error
*** HttpError: https://admob.googleapis.com/v1/accounts/pub-XXXXXXXXXXXXXXXX/networkReport:generate?alt=json returned "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.">
I have generated the service account credentials with Admob API is enabled.
But not able to figure out why there is authentication error.
The main issue, that code above tries to use the service account to query the api. But, it's not supported. It could be queried with OAuth2.0 Client Id.
The steps to generate OAth2.0 Client ID:
Open the credential page of your project (https://console.cloud.google.com/apis/credentials?project=REPLACE_WITH_YOUR_PROJECT_ID);
Generate OAuth2.0 Client ID;
Download the generated json file;
Use it with code below;
The following works well for me:
Libs:
pip3 install --upgrade google-api-python-client --user
pip3 install --upgrade oauth2client --user
Code example:
import csv
import sys
from googleapiclient import discovery
from googleapiclient.http import build_http
from oauth2client import tools
from oauth2client.file import Storage
from oauth2client.client import OAuth2WebServerFlow
class AdMobAPI:
def __init__(self):
scope = 'https://www.googleapis.com/auth/admob.report'
name = 'admob'
version = 'v1'
flow = OAuth2WebServerFlow(client_id='<todo: replace with a client_id from the secret json>',
client_secret='<todo: replace with a secret from the secret json>',
scope=scope)
storage = Storage(name + '.dat')
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = tools.run_flow(flow, storage)
http = credentials.authorize(http=build_http())
self.admob = discovery.build(name, version, http=http)
# Convert to the list of dictionaries
def report_to_list_of_dictionaries(self, response):
result = []
for report_line in response:
if report_line.get('row'):
print(report_line)
row = report_line.get('row')
dm = {}
if row.get('dimensionValues'):
for key, value in row.get('dimensionValues').items():
if value.get('value') and value.get('displayLabel'):
dm.update({key: value.get('value')})
dm.update({key + '_NAME': value.get('displayLabel')})
else:
dm.update({key: next(filter(None, [value.get('value'), value.get('displayLabel')]))})
if row.get('metricValues'):
for key, value in row.get('metricValues').items():
dm.update({key: next(filter(None, [value.get('value'), value.get('microsValue'), value.get('integerValue')]))})
result.append(dm)
return result
def generate_report(self, publisher_id):
date_range = {'startDate': {'year': 2020, 'month': 4, 'day': 1},
'endDate': {'year': 2020, 'month': 4, 'day': 1}}
dimensions = ['DATE', 'APP', 'PLATFORM', 'COUNTRY']
metrics = ['ESTIMATED_EARNINGS', 'IMPRESSIONS', 'CLICKS',
'AD_REQUESTS', 'MATCHED_REQUESTS']
sort_conditions = {'dimension': 'DATE', 'order': 'DESCENDING'}
report_spec = {'dateRange': date_range,
'dimensions': dimensions,
'metrics': metrics,
'sortConditions': [sort_conditions]}
request = {'reportSpec': report_spec}
return self.admob.accounts().networkReport().generate(
parent='accounts/{}'.format(publisher_id),
body=request).execute()
api = AdMobAPI()
raw_report = api.generate_report('<todo: replace with publisher id, smth like pub-[0-9]+>')
report_as_list_of_dictionaries = api.report_to_list_of_dictionaries(raw_report)
# Convert to CSV
dict_writer = csv.DictWriter(sys.stdout, report_as_list_of_dictionaries[0].keys())
dict_writer.writeheader()
dict_writer.writerows(report_as_list_of_dictionaries)
Currently, google admob api does not support service accounts
for more details, see here enter link description here

Why is dashboard created using Dash not working?

I'm trying to create a Dashboard using Dash, a python package. It is either taking too much of time to execute or throws an error saying "error in loading dependencies".
I'm running it on Jupyter notebook on IE browser.
Could anyone explain me, where I'm going wrong?
Thanks all in advance.
I've tried running a sample code from "https://www.datacamp.com/community/tutorials/learn-build-dash-python"
import dash
import dash_core_components as dcc
import dash_html_components as html
import plotly.graph_objs as go
import pandas as pd
df= pd.read_excel("https://github.com/chris1610/pbpython/blob/master/data/salesfnnel.xlsx?raw=True")
mgr_options = df["Manager"].unique()
app = dash.Dash()
app.layout = html.Div([
html.H2("Sales Funnel Report"),
html.Div(
[
dcc.Dropdown(
id="Manager",
options=[{
'label': i,
'value': i
} for i in mgr_options],
value='All Managers'),
],
style={'width': '25%',
'display': 'inline-block'}),
dcc.Graph(id='funnel-graph'),
])
#app.callback(
dash.dependencies.Output('funnel-graph', 'figure'),
[dash.dependencies.Input('Manager', 'value')])
def update_graph(Manager):
if Manager == "All Managers":
df_plot = df.copy()
else:
df_plot = df[df['Manager'] == Manager]
pv = pd.pivot_table(
df_plot,
index=['Name'],
columns=["Status"],
values=['Quantity'],
aggfunc=sum,
fill_value=0)
trace1 = go.Bar(x=pv.index, y=pv[('Quantity', 'declined')], name='Declined')
trace2 = go.Bar(x=pv.index, y=pv[('Quantity', 'pending')], name='Pending')
trace3 = go.Bar(x=pv.index, y=pv[('Quantity', 'presented')], name='Presented')
trace4 = go.Bar(x=pv.index, y=pv[('Quantity', 'won')], name='Won')
return {
'data': [trace1, trace2, trace3, trace4],
'layout':
go.Layout(
title='Customer Order Status for {}'.format(Manager),
barmode='stack')
}
if __name__ == '__main__':
app.run_server(debug=True, use_reloader = False)
Error in loading dependencies.

Flask TypeError: argument of type 'NoneType' is not iterable

I am not sure why I am getting this TypeError:
File "C:/Users/PycharmProjects/REST/app.py", line 30, in
valid_book_object
if ("isbn" in book and "name" in book and "price" in book):
TypeError: argument of type 'NoneType' is not iterable
127.0.0.1 - - [12/Nov/2018 14:22:29] "POST /books HTTP/1.1" 500 -
Code:
from flask import Flask, jsonify, request
from test import *
app=Flask(__name__)
books=[
{'name': 'M',
'price': 6.75,
'isbn':123
},
{'name': 'G',
'price': 7.75,
'isbn':456
},
]
#GET /store
#app.route('/books') #first endpoint
def get_books():
return jsonify({'books': books})
# POST /books
#{'name': 'F',
#'price': 7.00,
#'isbn': 789
#},
def valid_book_object(book):
if ("isbn" in book and "name" in book and "price" in book):
return True
print("true")
else:
return False
print("false")
#app.route('/books', methods=['POST'])
def add_book():
#return jsonify(request.get_json())
request_data=request.get_json()
if(valid_book_object(request_data)):
books.insert(0, request_data)
return "True"
else:
return "False"
#GET /books/456
#app.route('/books/<int:isbn>') #second endpoint
def get_book_by_isbn(isbn):
return_value={}
for book in books:
if book["isbn"]==isbn:
return_value={
'name': book["name"],
'price':book["price"]
}
return jsonify(return_value)
app.run(port=5000)
You are not sending JSON data to /books route using POST method.
I tried your solution with postman and it worked. Also, if you want to use some route for GET and POST, don't split them. Use methods=['GET', 'POST']. Here is your code reformatted with PEP 8 standard:
from flask import Flask, jsonify, request
app = Flask(__name__)
books = [
{'name': 'M',
'price': 6.75,
'isbn': 123
},
{'name': 'G',
'price': 7.75,
'isbn': 456
}
]
# POST /books
# {
# "name": "F",
# "price": 7.00,
# "isbn": 789
# }
def valid_book_object(book):
if "isbn" in book and "name" in book and "price" in book:
return True
else:
return False
#app.route('/books', methods=['GET', 'POST'])
def add_book():
# If request is GET, just return JSON data of books.
if request.method == 'GET':
return jsonify({'books': books})
else:
# This is part if it is POST request
request_data = request.get_json()
if valid_book_object(request_data):
books.insert(0, request_data)
return "True"
else:
return "False"
# GET /books/456
#app.route('/books/<int:isbn>') # second endpoint
def get_book_by_isbn(isbn):
return_value = {}
for book in books:
if book["isbn"] == isbn:
return_value = {
'name': book["name"],
'price': book["price"]
}
return jsonify(return_value)
return 'No book with {} isbn value'.format(isbn)
if __name__ == '__main__':
app.run(port=5000)
And here is the output from postman (you can see True at the bottom, that is what you return if POST was successful):
If you use postman, be sure to select application/json content-type.
If you are using JQuery ajax method, please read this answer. But anyway, here is using JQuery (tested):
data = JSON.stringify({
name: "F",
price: 7.00,
isbn: 789
});
$.ajax({
url: '/books',
type: "POST",
data: data,
contentType: "application/json",
dataType: "json",
success: function(){
console.log('Post executed successfully');
}
})

Resources