tracking stuff
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
*.pyc
|
||||
|
||||
# Unit testing
|
||||
.tox
|
||||
|
||||
# Packages
|
||||
*.egg-info
|
||||
@@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
@@ -0,0 +1,2 @@
|
||||
include README.md
|
||||
recursive-include referer_parser/data *.json
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
# referer-parser Python library
|
||||
|
||||
This is the Python implementation of [referer-parser] [referer-parser], the library for extracting search marketing data from referer _(sic)_ URLs.
|
||||
|
||||
The implementation uses the shared 'database' of known referers found in [`referers.yml`] [referers-yml] (converted to a `referers.json` file,
|
||||
see below).
|
||||
|
||||
The Python version of referer-parser is maintained by [Don Spaulding] [donspaulding].
|
||||
|
||||
## Installation
|
||||
|
||||
$ pip install referer_parser
|
||||
|
||||
## Usage
|
||||
|
||||
Create a new instance of a Referer object by passing in the url you want to parse:
|
||||
|
||||
```python
|
||||
from referer_parser import Referer
|
||||
|
||||
referer_url = 'http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=en&client=safari'
|
||||
|
||||
r = Referer(referer_url)
|
||||
```
|
||||
|
||||
The `r` variable now holds a Referer instance. The important attributes are:
|
||||
|
||||
```python
|
||||
print(r.known) # True
|
||||
print(r.referer) # 'Google'
|
||||
print(r.medium) # 'search'
|
||||
print(r.search_parameter) # 'q'
|
||||
print(r.search_term) # 'gateway oracle cards denise linn'
|
||||
print(r.uri) # ParseResult(scheme='http', netloc='www.google.com', path='/search', params='', query='q=gateway+oracle+cards+denise+linn&hl=en&client=safari', fragment='')
|
||||
```
|
||||
|
||||
Optionally, pass in the current URL as well, to handle internal referers
|
||||
|
||||
```python
|
||||
from referer_parser import Referer
|
||||
|
||||
referer_url = 'http://www.snowplowanalytics.com/about/team'
|
||||
curr_url = 'http://www.snowplowanalytics.com/account/profile'
|
||||
|
||||
r = Referer(referer_url, curr_url)
|
||||
```
|
||||
|
||||
The attributes would be
|
||||
|
||||
```python
|
||||
print(r.known) # True
|
||||
print(r.referer) # None
|
||||
print(r.medium) # 'internal'
|
||||
print(r.search_parameter) # None
|
||||
print(r.search_term) # None
|
||||
print(r.uri) # ParseResult(scheme='http', netloc='www.snowplowanalytics.com', path='/about/team', params='', query='', fragment='')
|
||||
```
|
||||
|
||||
The `uri` attribute is an instance of ParseResult from the standard library's `urlparse` module.
|
||||
|
||||
## referers.json
|
||||
|
||||
Unlike the other ports, the Python version of referer-parser uses a `referers.json` file, generated from the main `referers.yml` file. This is for two reasons:
|
||||
|
||||
1. Python's standard library includes a JSON parser but not a YAML parser
|
||||
2. Loading from JSON in Python is significantly faster than loading from YAML
|
||||
|
||||
To support the `referers.json` file, the distribution process for Python looks like this:
|
||||
|
||||
$ ./sync_yaml.sh
|
||||
$ cd python/referer_parser && python build_json.py
|
||||
$ python setup.py sdist bdist_wininst upload
|
||||
|
||||
## Contributing
|
||||
|
||||
1. Fork it
|
||||
2. Create your feature branch (`git checkout -b my-new-feature`)
|
||||
3. Commit your changes (`git commit -am 'Add some feature'`)
|
||||
4. Push to the branch (`git push origin my-new-feature`)
|
||||
5. Create new Pull Request
|
||||
|
||||
## Copyright and license
|
||||
|
||||
The referer-parser Python library is copyright 2012-2013 Don Spaulding.
|
||||
|
||||
Licensed under the [Apache License, Version 2.0] [license] (the "License");
|
||||
you may not use this software except in compliance with the License.
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
[referer-parser]: https://github.com/snowplow/referer-parser
|
||||
[referers-yml]: https://github.com/snowplow/referer-parser/blob/master/referers.yml
|
||||
|
||||
[donspaulding]: https://github.com/donspaulding
|
||||
|
||||
[license]: http://www.apache.org/licenses/LICENSE-2.0
|
||||
@@ -0,0 +1,101 @@
|
||||
import os
|
||||
import json
|
||||
|
||||
try:
|
||||
from urlparse import urlparse, parse_qsl
|
||||
iteritems = lambda dikt: dikt.iteritems()
|
||||
text_type = unicode
|
||||
except ImportError: # urlparse was renamed urllib.parse in Python 3
|
||||
from urllib.parse import urlparse, parse_qsl
|
||||
iteritems = lambda dikt: dikt.items()
|
||||
text_type = str
|
||||
|
||||
def load_referers(json_file):
|
||||
referers_dict = {}
|
||||
with open(json_file) as json_content:
|
||||
for medium, conf_list in iteritems(json.load(json_content)):
|
||||
for referer_name, config in iteritems(conf_list):
|
||||
params = None
|
||||
if 'parameters' in config:
|
||||
params = list(map(text_type.lower, config['parameters']))
|
||||
for domain in config['domains']:
|
||||
referers_dict[domain] = {
|
||||
'name': referer_name,
|
||||
'medium': medium
|
||||
}
|
||||
if params is not None:
|
||||
referers_dict[domain]['params'] = params
|
||||
return referers_dict
|
||||
|
||||
JSON_FILE = os.path.join(os.path.dirname(__file__), 'data', 'referers.json')
|
||||
REFERERS = load_referers(JSON_FILE)
|
||||
|
||||
class Referer(object):
|
||||
def __init__(self, ref_url, curr_url=None, referers=REFERERS):
|
||||
self.known = False
|
||||
self.referer = None
|
||||
self.medium = 'unknown'
|
||||
self.search_parameter = None
|
||||
self.search_term = None
|
||||
self.referers = referers
|
||||
|
||||
ref_uri = urlparse(ref_url)
|
||||
ref_host = ref_uri.hostname
|
||||
self.known = (ref_uri.scheme in {'http', 'https'} and
|
||||
ref_host is not None)
|
||||
self.uri = ref_uri
|
||||
|
||||
if not self.known:
|
||||
return
|
||||
|
||||
if curr_url:
|
||||
curr_uri = urlparse(curr_url)
|
||||
curr_host = curr_uri.hostname
|
||||
if curr_host == ref_host:
|
||||
self.medium = 'internal'
|
||||
return
|
||||
|
||||
referer = self._lookup_referer(ref_host, ref_uri.path, True)
|
||||
if not referer:
|
||||
referer = self._lookup_referer(ref_host, ref_uri.path, False)
|
||||
if not referer:
|
||||
self.medium = 'unknown'
|
||||
return
|
||||
|
||||
self.referer = referer['name']
|
||||
self.medium = referer['medium']
|
||||
|
||||
if referer['medium'] == 'search':
|
||||
if 'params' not in referer or not referer['params']:
|
||||
return
|
||||
for param, val in parse_qsl(ref_uri.query):
|
||||
if param.lower() in referer['params']:
|
||||
self.search_parameter = param
|
||||
self.search_term = val
|
||||
|
||||
def _lookup_referer(self, ref_host, ref_path, include_path):
|
||||
referer = None
|
||||
try:
|
||||
if include_path:
|
||||
referer = self.referers[ref_host + ref_path]
|
||||
else:
|
||||
referer = self.referers[ref_host]
|
||||
except KeyError:
|
||||
if include_path:
|
||||
path_parts = ref_path.split('/')
|
||||
if len(path_parts) > 1:
|
||||
try:
|
||||
referer = self.referers[ref_host + '/' + path_parts[1]]
|
||||
except KeyError:
|
||||
pass
|
||||
if not referer:
|
||||
try:
|
||||
idx = ref_host.index('.')
|
||||
return self._lookup_referer(
|
||||
ref_host[idx + 1:],
|
||||
ref_path, include_path
|
||||
)
|
||||
except ValueError:
|
||||
return None
|
||||
else:
|
||||
return referer
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
import json
|
||||
|
||||
import yaml
|
||||
|
||||
def build_json():
|
||||
searches = yaml.load(open('./data/referers.yml'))
|
||||
with open('./data/referers.json', 'w') as fp:
|
||||
json.dump(searches, fp)
|
||||
|
||||
if __name__ == "__main__":
|
||||
build_json()
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,213 @@
|
||||
import unittest
|
||||
from referer_parser import Referer
|
||||
|
||||
class TestRefererParsing(unittest.TestCase):
|
||||
def check_equals(self, ref_obj, referer, term, medium):
|
||||
self.assertTrue(ref_obj.known)
|
||||
self.assertEqual(ref_obj.referer, referer)
|
||||
self.assertEqual(ref_obj.search_term, term)
|
||||
self.assertEqual(ref_obj.medium, medium)
|
||||
|
||||
def check_no_term(self, ref_obj, referer, medium):
|
||||
self.assertTrue(ref_obj.known)
|
||||
self.assertEqual(ref_obj.referer, referer)
|
||||
self.assertIsNone(ref_obj.search_term)
|
||||
self.assertEqual(ref_obj.medium, medium)
|
||||
|
||||
def test_google_minimal(self):
|
||||
""" Google search #1
|
||||
"""
|
||||
r = Referer('http://www.google.com/search')
|
||||
# r.known, r.referer, r.search_term, r.medium
|
||||
self.assertTrue(r.known)
|
||||
self.assertEqual(r.referer, 'Google')
|
||||
self.assertIsNone(r.search_term)
|
||||
self.assertEqual(r.medium, 'search')
|
||||
|
||||
def test_google_term(self):
|
||||
""" Google search #2
|
||||
"""
|
||||
r = Referer('http://www.google.com/search?q=gateway+oracle+cards+denise+linn&hl=\
|
||||
en&client=safari')
|
||||
self.check_equals(r, 'Google', 'gateway oracle cards denise linn', 'search')
|
||||
|
||||
def test_powered_by_google(self):
|
||||
""" Powered by Google
|
||||
"""
|
||||
r = Referer("""http://isearch.avg.com/pages/images.aspx?q=tarot+card+change&sap=\
|
||||
dsp&lang=en&mid=209215200c4147d1a9d6d1565005540b-b0d4f81a8999f5981f04537c5ec8468fd523459\
|
||||
3&cid=%7B50F9298B-C111-4C7E-9740-363BF0015949%7D&v=12.1.0.21&ds=AVG&d=7%2F23%2F2012+10%3\
|
||||
A31%3A08+PM&pr=fr&sba=06oENya4ZG1YS6vOLJwpLiFdjG91ICt2YE59W2p5ENc2c4w8KvJb5xbvjkj3ceMjny\
|
||||
TSpZq-e6pj7GQUylIQtuK4psJU60wZuI-8PbjX-OqtdX3eIcxbMoxg3qnIasP0ww2fuID1B-p2qJln8vBHxWztkp\
|
||||
xeixjZPSppHnrb9fEcx62a9DOR0pZ-V-Kjhd-85bIL0QG5qi1OuA4M1eOP4i_NzJQVRXPQDmXb-CpIcruc2h5FE9\
|
||||
2Tc8QMUtNiTEWBbX-QiCoXlgbHLpJo5Jlq-zcOisOHNWU2RSHYJnK7IUe_SH6iQ.%2CYT0zO2s9MTA7aD1mNjZmZ\
|
||||
DBjMjVmZDAxMGU4&snd=hdr&tc=test1""")
|
||||
self.check_equals(r, 'Google', 'tarot card change', 'search')
|
||||
|
||||
def test_google_img_search(self):
|
||||
""" Google Images search
|
||||
"""
|
||||
r = Referer("""http://www.google.fr/imgres?q=Ogham+the+celtic+oracle&hl=fr&safe=\
|
||||
off&client=firefox-a&hs=ZDu&sa=X&rls=org.mozilla:fr-FR:unofficial&tbm=isch&prmd=imvnsa&t\
|
||||
bnid=HUVaj-o88ZRdYM:&imgrefurl=http://www.psychicbazaar.com/oracles/101-ogham-the-celtic\
|
||||
-oracle-set.html&docid=DY5_pPFMliYUQM&imgurl=http://mdm.pbzstatic.com/oracles/ogham-the-\
|
||||
celtic-oracle-set/montage.png&w=734&h=250&ei=GPdWUIePCOqK0AWp3oCQBA&zoom=1&iact=hc&vpx=1\
|
||||
29&vpy=276&dur=827&hovh=131&hovw=385&tx=204&ty=71&sig=104115776612919232039&page=1&tbnh=\
|
||||
69&tbnw=202&start=0&ndsp=26&ved=1t:429,r:13,s:0,i:114&biw=1272&bih=826""")
|
||||
self.check_equals(r, 'Google Images', 'Ogham the celtic oracle', 'search')
|
||||
|
||||
def test_yahoo_search(self):
|
||||
""" Yahoo! search
|
||||
"""
|
||||
r = Referer("""http://es.search.yahoo.com/search;_ylt=A7x9QbwbZXxQ9EMAPCKT.Qt.?p=\
|
||||
BIEDERMEIER+FORTUNE+TELLING+CARDS&ei=utf-8&type=685749&fr=chr-greentree_gc&xargs=0&pstar\
|
||||
t=1&b=11""")
|
||||
self.check_equals(r, 'Yahoo!', 'BIEDERMEIER FORTUNE TELLING CARDS', 'search')
|
||||
|
||||
def test_yahoo_img_search(self):
|
||||
""" Yahoo! Images search
|
||||
"""
|
||||
r = Referer("""http://it.images.search.yahoo.com/images/view;_ylt=A0PDodgQmGBQpn\
|
||||
4AWQgdDQx.;_ylu=X3oDMTBlMTQ4cGxyBHNlYwNzcgRzbGsDaW1n?back=http%3A%2F%2Fit.images.search.\
|
||||
yahoo.com%2Fsearch%2Fimages%3Fp%3DEarth%2BMagic%2BOracle%2BCards%26fr%3Dmcafee%26fr2%3Dp\
|
||||
iv-web%26tab%3Dorganic%26ri%3D5&w=1064&h=1551&imgurl=mdm.pbzstatic.com%2Foracles%2Fearth\
|
||||
-magic-oracle-cards%2Fcard-1.png&rurl=http%3A%2F%2Fwww.psychicbazaar.com%2Foracles%2F143\
|
||||
-earth-magic-oracle-cards.html&size=2.8+KB&name=Earth+Magic+Oracle+Cards+-+Psychic+Bazaa\
|
||||
r&p=Earth+Magic+Oracle+Cards&oid=f0a5ad5c4211efe1c07515f56cf5a78e&fr2=piv-web&fr=mcafee&\
|
||||
tt=Earth%2BMagic%2BOracle%2BCards%2B-%2BPsychic%2BBazaar&b=0&ni=90&no=5&ts=&tab=organic&\
|
||||
sigr=126n355ib&sigb=13hbudmkc&sigi=11ta8f0gd&.crumb=IZBOU1c0UHU""")
|
||||
self.check_equals(r, 'Yahoo! Images', 'Earth Magic Oracle Cards', 'search')
|
||||
|
||||
def test_price_runner_search(self):
|
||||
""" PriceRunner search
|
||||
"""
|
||||
r = Referer("""http://www.pricerunner.co.uk/search?displayNoHitsMessage=1&q=wild\
|
||||
+wisdom+of+the+faery+oracle""")
|
||||
self.check_equals(r, 'PriceRunner', 'wild wisdom of the faery oracle', 'search')
|
||||
|
||||
def test_bing_img(self):
|
||||
""" Bing Images
|
||||
"""
|
||||
r = Referer("""http://www.bing.com/images/search?q=psychic+oracle+cards&view=det\
|
||||
ail&id=D268EDDEA8D3BF20AF887E62AF41E8518FE96F08""")
|
||||
self.check_equals(r, 'Bing Images', 'psychic oracle cards', 'search')
|
||||
|
||||
def test_ixquick(self):
|
||||
""" IXquick search
|
||||
"""
|
||||
r = Referer("""https://s3-us3.ixquick.com/do/search""")
|
||||
self.assertTrue(r.known)
|
||||
self.assertEqual(r.referer, 'IXquick')
|
||||
self.assertIsNone(r.search_term)
|
||||
self.assertEqual(r.medium, 'search')
|
||||
|
||||
def test_aol_search(self):
|
||||
""" AOL search
|
||||
"""
|
||||
r = Referer("""http://aolsearch.aol.co.uk/aol/search?s_chn=hp&enabled_terms=&s_i\
|
||||
t=aoluk-homePage50&q=pendulums""")
|
||||
self.check_equals(r, 'AOL', 'pendulums', 'search')
|
||||
|
||||
def test_ask_search(self):
|
||||
""" Ask search
|
||||
"""
|
||||
r = Referer("""http://uk.search-results.com/web?qsrc=1&o=1921&l=dis&q=pendulums&\
|
||||
dm=ctry&atb=sysid%3D406%3Aappid%3D113%3Auid%3D8f40f651e7b608b5%3Auc%3D1346336505%3Aqu%3D\
|
||||
pendulums%3Asrc%3Dcrt%3Ao%3D1921&locale=en_GB""")
|
||||
self.check_equals(r, 'Ask', 'pendulums', 'search')
|
||||
|
||||
def test_mailru_search(self):
|
||||
""" Mail.ru search
|
||||
"""
|
||||
r = Referer("""http://go.mail.ru/search?q=Gothic%20Tarot%20Cards&where=any&num=1\
|
||||
0&rch=e&sf=20""")
|
||||
self.check_equals(r, 'Mail.ru', 'Gothic Tarot Cards', 'search')
|
||||
|
||||
def test_yandex_search(self):
|
||||
""" Yandex search
|
||||
"""
|
||||
r = Referer("""http://images.yandex.ru/yandsearch?text=Blue%20Angel%20Oracle%20B\
|
||||
lue%20Angel%20Oracle&noreask=1&pos=16&rpt=simage&lr=45&img_url=http%3A%2F%2Fmdm.pbzstati\
|
||||
c.com%2Foracles%2Fblue-angel-oracle%2Fbox-small.png""")
|
||||
self.check_equals(r, 'Yandex Images', 'Blue Angel Oracle Blue Angel Oracle', 'search')
|
||||
|
||||
def test_twitter_redirect(self):
|
||||
""" Twitter redirect
|
||||
"""
|
||||
r = Referer("""http://t.co/chrgFZDb""")
|
||||
self.check_no_term(r, 'Twitter', 'social')
|
||||
|
||||
def test_fb_social(self):
|
||||
""" Facebook social
|
||||
"""
|
||||
r = Referer("""http://www.facebook.com/l.php?u=http%3A%2F%2Fwww.psychicbazaar.co\
|
||||
m&h=yAQHZtXxS&s=1""")
|
||||
self.check_no_term(r, 'Facebook', 'social')
|
||||
|
||||
def test_fb_mobile(self):
|
||||
""" Facebook mobile
|
||||
"""
|
||||
r = Referer("""http://m.facebook.com/l.php?u=http%3A%2F%2Fwww.psychicbazaar.com%\
|
||||
2Fblog%2F2012%2F09%2Fpsychic-bazaar-reviews-tarot-foundations-31-days-to-read-tarot-with\
|
||||
-confidence%2F&h=kAQGXKbf9&s=1""")
|
||||
self.check_no_term(r, 'Facebook', 'social')
|
||||
|
||||
def test_odnoklassniki(self):
|
||||
""" Odnoklassniki
|
||||
"""
|
||||
r = Referer("""http://www.odnoklassniki.ru/dk?cmd=logExternal&st._aid=Conversati\
|
||||
ons_Openlink&st.name=externalLinkRedirect&st.link=http%3A%2F%2Fwww.psychicbazaar.com%2Fo\
|
||||
racles%2F187-blue-angel-oracle.html""")
|
||||
self.check_no_term(r, 'Odnoklassniki', 'social')
|
||||
|
||||
def test_tumblr(self):
|
||||
""" Tumblr social #1
|
||||
"""
|
||||
r = Referer("http://www.tumblr.com/dashboard")
|
||||
self.check_no_term(r, 'Tumblr', 'social')
|
||||
|
||||
def test_tumblr_subdomain(self):
|
||||
""" Tumblr w subdomain
|
||||
"""
|
||||
r = Referer("http://psychicbazaar.tumblr.com/")
|
||||
self.check_no_term(r, 'Tumblr', 'social')
|
||||
|
||||
def test_yahoo_mail(self):
|
||||
""" Yahoo! Mail
|
||||
"""
|
||||
r = Referer("""http://36ohk6dgmcd1n-c.c.yom.mail.yahoo.net/om/api/1.0/openmail.a\
|
||||
pp.invoke/36ohk6dgmcd1n/11/1.0.35/us/en-US/view.html/0""")
|
||||
self.check_no_term(r, 'Yahoo! Mail', 'email')
|
||||
|
||||
def test_outlookcom_mail(self):
|
||||
""" Outlook.com mail
|
||||
"""
|
||||
r = Referer("http://co106w.col106.mail.live.com/default.aspx?rru=inbox")
|
||||
self.check_no_term(r, 'Outlook.com', 'email')
|
||||
|
||||
def test_orange_webmail(self):
|
||||
""" Orange Webmail
|
||||
"""
|
||||
r = Referer("""http://webmail1m.orange.fr/webmail/fr_FR/read.html?FOLDER=SF_INBO\
|
||||
X&IDMSG=8594&check=&SORTBY=31""")
|
||||
self.check_no_term(r, 'Orange Webmail', 'email')
|
||||
|
||||
def test_internal(self):
|
||||
r = Referer("http://www.snowplowanalytics.com/about/team",
|
||||
"http://www.snowplowanalytics.com/account/profile")
|
||||
self.assertTrue(r.known)
|
||||
self.assertEqual(r.medium, 'internal')
|
||||
self.assertIsNone(r.search_term)
|
||||
self.assertIsNone(r.referer)
|
||||
|
||||
def test_no_host(self):
|
||||
r = Referer("http:some/path")
|
||||
self.assertFalse(r.known)
|
||||
self.assertIsNone(r.uri.hostname)
|
||||
self.assertEqual(r.medium, 'unknown')
|
||||
self.assertIsNone(r.search_term)
|
||||
self.assertIsNone(r.referer)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import os
|
||||
from setuptools import setup, find_packages
|
||||
|
||||
setup_pth = os.path.dirname(__file__)
|
||||
readme_pth = os.path.join(setup_pth, 'README.md')
|
||||
|
||||
setup(
|
||||
name='referer-parser',
|
||||
version="0.3.1",
|
||||
long_description=open(readme_pth).read(),
|
||||
packages=find_packages(),
|
||||
include_package_data=True,
|
||||
package_data={'referer_parser': ['data/referers.json']},
|
||||
zip_safe=False,
|
||||
)
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
# Tox (http://tox.testrun.org/) is a tool for running tests
|
||||
# in multiple virtualenvs. This configuration file will run the
|
||||
# test suite on all supported python versions. To use it, "pip install tox"
|
||||
# and then run "tox" from this directory.
|
||||
|
||||
[tox]
|
||||
#envlist = py27, py33, pypy, jython
|
||||
envlist = py27, py33
|
||||
|
||||
[testenv]
|
||||
commands = nosetests referer_parser.test
|
||||
deps =
|
||||
nose
|
||||
Reference in New Issue
Block a user